@hasna/switcher 0.2.13 → 0.2.14
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 +12 -0
- package/dist/cli/index.js +1470 -91
- package/dist/gateway-client.d.ts +84 -0
- package/dist/gateway-service.d.ts +60 -0
- package/dist/gateway-supervisor.d.ts +73 -0
- package/dist/harness-process.d.ts +1 -0
- package/dist/harness-types.d.ts +3 -1
- package/dist/inference-gateway.d.ts +25 -2
- package/dist/launcher.d.ts +11 -1
- package/dist/mcp/index.js +2 -2
- package/dist/process-table.d.ts +28 -0
- package/dist/serve/index.js +2 -2
- package/docs/GATEWAY-SERVICE.md +179 -0
- package/hasna.contract.json +1 -1
- package/openapi.json +1 -1
- package/package.json +4 -3
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type GatewayHost, type RoutingEvent } from "./inference-gateway";
|
|
2
|
+
import { type CommandRunner } from "./gateway-supervisor";
|
|
3
|
+
export type GatewayStatus = {
|
|
4
|
+
service: "switcher-gateway";
|
|
5
|
+
protocol: number;
|
|
6
|
+
version: string;
|
|
7
|
+
pid: number;
|
|
8
|
+
port: number;
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
launches: number;
|
|
11
|
+
supervisor: string;
|
|
12
|
+
};
|
|
13
|
+
type Registration = {
|
|
14
|
+
id: string;
|
|
15
|
+
token: string;
|
|
16
|
+
baseUrl: string;
|
|
17
|
+
};
|
|
18
|
+
type Release = {
|
|
19
|
+
retained: boolean;
|
|
20
|
+
holders: number;
|
|
21
|
+
events?: RoutingEvent[];
|
|
22
|
+
eventsDropped?: number;
|
|
23
|
+
};
|
|
24
|
+
/** HTTP over the owner-only Unix socket of the gateway service. */
|
|
25
|
+
export declare class GatewayControlClient {
|
|
26
|
+
readonly socket: string;
|
|
27
|
+
readonly timeoutMs: number;
|
|
28
|
+
constructor(socket: string, timeoutMs?: number);
|
|
29
|
+
private request;
|
|
30
|
+
status(): Promise<GatewayStatus>;
|
|
31
|
+
launches(): Promise<{
|
|
32
|
+
data: unknown[];
|
|
33
|
+
}>;
|
|
34
|
+
register(body: unknown): Promise<Registration>;
|
|
35
|
+
attach(id: string, pid: number): Promise<{
|
|
36
|
+
holders: number;
|
|
37
|
+
}>;
|
|
38
|
+
release(id: string): Promise<Release>;
|
|
39
|
+
revoke(id: string): Promise<{
|
|
40
|
+
revoked: boolean;
|
|
41
|
+
}>;
|
|
42
|
+
stop(): Promise<{
|
|
43
|
+
stopping: boolean;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
46
|
+
/** The running gateway's status, or undefined when none runs. An untrusted socket is an error, never "stopped". */
|
|
47
|
+
export declare function gatewayStatus(env?: NodeJS.ProcessEnv): Promise<GatewayStatus | undefined>;
|
|
48
|
+
/** True when the running gateway is not this CLI's version or protocol, so `switcher gateway install` should restart it. */
|
|
49
|
+
export declare const gatewayOutdated: (status: GatewayStatus) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* supervise: false never installs a login item (for example when this CLI is not the
|
|
52
|
+
* PATH-linked switcher, such as a development checkout); a detached service is used.
|
|
53
|
+
*/
|
|
54
|
+
export type EnsureGatewayOptions = {
|
|
55
|
+
env?: NodeJS.ProcessEnv;
|
|
56
|
+
command: string[];
|
|
57
|
+
runner?: CommandRunner;
|
|
58
|
+
waitMs?: number;
|
|
59
|
+
supervise?: boolean;
|
|
60
|
+
warn?: (message: string) => void;
|
|
61
|
+
};
|
|
62
|
+
export type EnsuredGateway = {
|
|
63
|
+
client: GatewayControlClient;
|
|
64
|
+
status: GatewayStatus;
|
|
65
|
+
started: "existing" | "launchd" | "systemd" | "detached";
|
|
66
|
+
};
|
|
67
|
+
/** Reuse the running gateway or start it: under launchd/systemd for the default home, otherwise detached. */
|
|
68
|
+
export declare function ensureGatewayService(options: EnsureGatewayOptions): Promise<EnsuredGateway>;
|
|
69
|
+
export type ServiceGatewayHost = GatewayHost & {
|
|
70
|
+
attach: (pid: number) => void;
|
|
71
|
+
retainedState: () => boolean;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Register each launch gateway with the persistent service. If registration fails,
|
|
75
|
+
* the launch still runs on a per-launch gateway and says so.
|
|
76
|
+
*/
|
|
77
|
+
export declare function createServiceGatewayHost(client: GatewayControlClient, context: {
|
|
78
|
+
holderPid: number;
|
|
79
|
+
stateDir?: string;
|
|
80
|
+
credentialEnv?: string;
|
|
81
|
+
onRoutingEvent?: (event: RoutingEvent) => void;
|
|
82
|
+
warn?: (message: string) => void;
|
|
83
|
+
}): ServiceGatewayHost;
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type ProviderInput } from "./domain";
|
|
2
|
+
import type { ProviderRequestTiming } from "./provider-request";
|
|
3
|
+
import { type EnvironmentDigestProbe, type ProcessReader } from "./process-table";
|
|
4
|
+
/** Version of the control protocol between `switcher launch` and the gateway service. */
|
|
5
|
+
export declare const GATEWAY_PROTOCOL = 1;
|
|
6
|
+
export type GatewayPaths = {
|
|
7
|
+
root: string;
|
|
8
|
+
socket: string;
|
|
9
|
+
launches: string;
|
|
10
|
+
config: string;
|
|
11
|
+
log: string;
|
|
12
|
+
};
|
|
13
|
+
/** Every path the gateway service owns for one Switcher home. */
|
|
14
|
+
export declare function gatewayPaths(env?: NodeJS.ProcessEnv): GatewayPaths;
|
|
15
|
+
/**
|
|
16
|
+
* Check the control socket before a client sends anything to it: launches carry the
|
|
17
|
+
* provider credential. Its directory must be a private directory owned by this user,
|
|
18
|
+
* and the socket itself a socket owned by this user. A missing socket means the
|
|
19
|
+
* gateway is not running.
|
|
20
|
+
*/
|
|
21
|
+
export declare function assertTrustedControlSocket(socket: string): Promise<void>;
|
|
22
|
+
/** The default home gets one stable, per-user loopback port. */
|
|
23
|
+
export declare function defaultGatewayPort(uid?: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* The account's home from the passwd database. Bun's os.userInfo().homedir follows
|
|
26
|
+
* $HOME, so it cannot tell a synthetic test home from the real one.
|
|
27
|
+
*/
|
|
28
|
+
export declare function accountHome(): string | undefined;
|
|
29
|
+
/** True only for the account's real home and its default ~/.hasna/switcher; unknown means no. */
|
|
30
|
+
export declare function isDefaultSwitcherHome(env?: NodeJS.ProcessEnv, home?: string | undefined): boolean;
|
|
31
|
+
export declare function gatewaySettings(env?: NodeJS.ProcessEnv): {
|
|
32
|
+
port: number | undefined;
|
|
33
|
+
graceMs: number;
|
|
34
|
+
scanMs: number;
|
|
35
|
+
exitIdleMs: number;
|
|
36
|
+
};
|
|
37
|
+
export type GatewayServiceOptions = {
|
|
38
|
+
env?: NodeJS.ProcessEnv;
|
|
39
|
+
port?: number;
|
|
40
|
+
graceMs?: number;
|
|
41
|
+
scanMs?: number;
|
|
42
|
+
exitWhenIdleMs?: number;
|
|
43
|
+
supervisor?: string;
|
|
44
|
+
processTable?: ProcessReader;
|
|
45
|
+
environmentProbe?: EnvironmentDigestProbe;
|
|
46
|
+
now?: () => number;
|
|
47
|
+
timing?: ProviderRequestTiming;
|
|
48
|
+
log?: (line: string) => void;
|
|
49
|
+
resolveCredential?: (provider: ProviderInput) => Promise<string | undefined>;
|
|
50
|
+
};
|
|
51
|
+
export type GatewayService = {
|
|
52
|
+
url: string;
|
|
53
|
+
port: number;
|
|
54
|
+
socket: string;
|
|
55
|
+
pid: number;
|
|
56
|
+
close: () => Promise<void>;
|
|
57
|
+
closed: Promise<void>;
|
|
58
|
+
scan: () => Promise<void>;
|
|
59
|
+
};
|
|
60
|
+
export declare function startGatewayService(options?: GatewayServiceOptions): Promise<GatewayService>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export declare const LAUNCHD_LABEL = "com.hasna.switcher.gateway";
|
|
2
|
+
export declare const SYSTEMD_UNIT = "switcher-gateway.service";
|
|
3
|
+
export type Supervisor = "launchd" | "systemd" | "none";
|
|
4
|
+
export type CommandResult = {
|
|
5
|
+
code: number;
|
|
6
|
+
stdout: string;
|
|
7
|
+
stderr: string;
|
|
8
|
+
};
|
|
9
|
+
export type CommandRunner = (argv: string[]) => Promise<CommandResult>;
|
|
10
|
+
export declare function serviceEnvironment(env?: NodeJS.ProcessEnv): Record<string, string>;
|
|
11
|
+
/** A login unit outlives the installing shell: its temporary directory comes from launchd or systemd, not that shell. */
|
|
12
|
+
export declare function unitEnvironment(env?: NodeJS.ProcessEnv): Record<string, string>;
|
|
13
|
+
/**
|
|
14
|
+
* A login unit runs its command at every login and after every crash, so each file it
|
|
15
|
+
* runs must be one only this user (or root) can change: the file, its real path and
|
|
16
|
+
* every parent directory owned by this user or root and not writable by group or
|
|
17
|
+
* others, and nothing under a temporary directory.
|
|
18
|
+
*/
|
|
19
|
+
export declare function assertSafeServiceCommand(command: string[], temporaryRoots?: string[]): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* The OS supervisor that keeps the gateway running across logins and crashes.
|
|
22
|
+
* Only the user's real default home is supervised; tests and custom homes use a
|
|
23
|
+
* detached service that exits when idle, so they never register a login item.
|
|
24
|
+
*/
|
|
25
|
+
export declare function supervisorFor(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform, home?: string | undefined): Supervisor;
|
|
26
|
+
/** launchd creates missing log directories itself, so its log stays outside the private Switcher home. */
|
|
27
|
+
export declare function launchdLogPath(home: string): string;
|
|
28
|
+
export declare function unitPath(supervisor: Exclude<Supervisor, "none">, env?: NodeJS.ProcessEnv, home?: string): string;
|
|
29
|
+
/** A per-user LaunchAgent: started at login, restarted if it crashes, stopped only on purpose. */
|
|
30
|
+
export declare function renderLaunchdPlist(command: string[], env?: NodeJS.ProcessEnv, home?: string): string;
|
|
31
|
+
/** A systemd user unit with the same semantics. Starting at boot without a login needs `loginctl enable-linger`. */
|
|
32
|
+
export declare function renderSystemdUnit(command: string[], env?: NodeJS.ProcessEnv): string;
|
|
33
|
+
export declare const defaultRunner: CommandRunner;
|
|
34
|
+
export type InstallResult = {
|
|
35
|
+
supervisor: Exclude<Supervisor, "none">;
|
|
36
|
+
path: string;
|
|
37
|
+
changed: boolean;
|
|
38
|
+
domain?: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Install (or refresh) and start the gateway under the user's supervisor.
|
|
42
|
+
* verifyCommand checks the files the unit will run (assertSafeServiceCommand); tests with
|
|
43
|
+
* fixture paths replace it.
|
|
44
|
+
*/
|
|
45
|
+
export declare function installGatewayService(options: {
|
|
46
|
+
command: string[];
|
|
47
|
+
env?: NodeJS.ProcessEnv;
|
|
48
|
+
supervisor?: Supervisor;
|
|
49
|
+
runner?: CommandRunner;
|
|
50
|
+
home?: string;
|
|
51
|
+
verifyCommand?: (command: string[]) => Promise<void>;
|
|
52
|
+
}): Promise<InstallResult>;
|
|
53
|
+
/** Stop the supervised gateway and remove its unit. Persisted launches stay for a later start. */
|
|
54
|
+
export declare function uninstallGatewayService(options?: {
|
|
55
|
+
env?: NodeJS.ProcessEnv;
|
|
56
|
+
supervisor?: Supervisor;
|
|
57
|
+
runner?: CommandRunner;
|
|
58
|
+
home?: string;
|
|
59
|
+
}): Promise<{
|
|
60
|
+
removed: boolean;
|
|
61
|
+
supervisor: "none";
|
|
62
|
+
path?: undefined;
|
|
63
|
+
} | {
|
|
64
|
+
removed: boolean;
|
|
65
|
+
supervisor: "systemd" | "launchd";
|
|
66
|
+
path: string;
|
|
67
|
+
}>;
|
|
68
|
+
export declare function unitInstalled(env?: NodeJS.ProcessEnv, home?: string | undefined): Promise<{
|
|
69
|
+
supervisor: Supervisor;
|
|
70
|
+
path?: string;
|
|
71
|
+
log?: string;
|
|
72
|
+
installed: boolean;
|
|
73
|
+
}>;
|
package/dist/harness-types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Model, Provider, Profile } from "./domain";
|
|
2
2
|
import type { CompiledModelPolicy, ModelPolicy } from "./model-policy";
|
|
3
3
|
import type { NativeModelPolicy } from "./native-model-policy";
|
|
4
|
-
import type { RoutingEvent } from "./inference-gateway";
|
|
4
|
+
import type { GatewayHost, RoutingEvent } from "./inference-gateway";
|
|
5
5
|
import type { ReasoningEffort } from "./reasoning";
|
|
6
6
|
import type { NativeState } from "./native-state";
|
|
7
7
|
export type HarnessId = Profile["harness"];
|
|
@@ -28,6 +28,8 @@ export type HarnessLaunchInput = {
|
|
|
28
28
|
reasoning?: ReasoningEffort;
|
|
29
29
|
dangerouslyBypassApprovalsAndSandbox?: boolean;
|
|
30
30
|
sharedState?: NativeState;
|
|
31
|
+
/** Hosts the launch's inference gateway; a per-launch listener when absent. */
|
|
32
|
+
gatewayHost?: GatewayHost;
|
|
31
33
|
};
|
|
32
34
|
export type PreparedLaunch = {
|
|
33
35
|
executable: string;
|
|
@@ -13,15 +13,38 @@ export type RoutingEvent = {
|
|
|
13
13
|
reason?: string;
|
|
14
14
|
upstreamStatus?: number;
|
|
15
15
|
};
|
|
16
|
-
type GatewayInput = HarnessLaunchInput & {
|
|
16
|
+
export type GatewayInput = HarnessLaunchInput & {
|
|
17
17
|
compiledPolicy: CompiledModelPolicy;
|
|
18
18
|
catalogPath: string;
|
|
19
19
|
onRoutingEvent?: (event: RoutingEvent) => void;
|
|
20
20
|
};
|
|
21
|
+
/** A started gateway endpoint as a native harness sees it. */
|
|
22
|
+
export type GatewayLease = {
|
|
23
|
+
baseUrl: string;
|
|
24
|
+
token: string;
|
|
25
|
+
cleanup: () => Promise<void>;
|
|
26
|
+
retainsState?: () => boolean;
|
|
27
|
+
};
|
|
28
|
+
/** Where a launch's inference gateway runs: its own listener, or a persistent service. */
|
|
29
|
+
export type GatewayHost = {
|
|
30
|
+
open: (input: GatewayInput) => Promise<GatewayLease>;
|
|
31
|
+
};
|
|
32
|
+
export type InferenceHandlerOptions = {
|
|
33
|
+
/** Resolve the provider credential when a request needs it; replaces input.credential. */
|
|
34
|
+
credential?: () => Promise<string | undefined>;
|
|
35
|
+
};
|
|
36
|
+
export type InferenceHandler = {
|
|
37
|
+
bodyLimit: number;
|
|
38
|
+
closing: () => boolean;
|
|
39
|
+
handle: (request: Request, server: Bun.Server<unknown>) => Promise<Response>;
|
|
40
|
+
close: () => Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
export declare const MAX_REQUEST_BODY_BYTES: number;
|
|
43
|
+
/** A launch's credential boundary and model policy, independent of the listener that serves it. */
|
|
44
|
+
export declare function createInferenceHandler(input: GatewayInput, timing?: ProviderRequestTiming, options?: InferenceHandlerOptions): InferenceHandler;
|
|
21
45
|
/** A per-launch credential boundary: the native client never receives the provider key. */
|
|
22
46
|
export declare function createInferenceGateway(input: GatewayInput, timing?: ProviderRequestTiming): {
|
|
23
47
|
baseUrl: string;
|
|
24
48
|
token: string;
|
|
25
49
|
cleanup: () => Promise<void>;
|
|
26
50
|
};
|
|
27
|
-
export {};
|
package/dist/launcher.d.ts
CHANGED
|
@@ -6,9 +6,19 @@ import type { ChatGPTInstallation, ClaudeDesktopInstallation } from "./desktop-a
|
|
|
6
6
|
import type { ReasoningEffort } from "./reasoning";
|
|
7
7
|
import { type NativeState } from "./native-state";
|
|
8
8
|
import type { RoutingEvent } from "./inference-gateway";
|
|
9
|
+
import { type GatewayControlClient } from "./gateway-client";
|
|
9
10
|
export { childEnvironment } from "./harness-environment";
|
|
10
11
|
export type LaunchBackend = "direct" | "ori";
|
|
11
|
-
export type
|
|
12
|
+
export type GatewayMode = "local" | "service";
|
|
13
|
+
/** service: register with the persistent per-user gateway so detached descendants keep working. */
|
|
14
|
+
/** gatewaySupervise: false keeps a non-PATH switcher (a development checkout) from installing the login service. */
|
|
15
|
+
export type LaunchGatewayOptions = {
|
|
16
|
+
gateway?: GatewayMode;
|
|
17
|
+
gatewayCommand?: string[];
|
|
18
|
+
gatewaySupervise?: boolean;
|
|
19
|
+
connectGateway?: () => Promise<GatewayControlClient>;
|
|
20
|
+
};
|
|
21
|
+
export type LaunchOptions = LaunchGatewayOptions & {
|
|
12
22
|
desktop?: ChatGPTInstallation;
|
|
13
23
|
claudeDesktop?: ClaudeDesktopInstallation;
|
|
14
24
|
shareNativeState?: boolean;
|
package/dist/mcp/index.js
CHANGED
|
@@ -4049,7 +4049,7 @@ function canonicalPolicyJSON(value) {
|
|
|
4049
4049
|
return JSON.stringify(value, (_key, item) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item);
|
|
4050
4050
|
}
|
|
4051
4051
|
// package.json
|
|
4052
|
-
var version = "0.2.
|
|
4052
|
+
var version = "0.2.14";
|
|
4053
4053
|
|
|
4054
4054
|
// src/version.ts
|
|
4055
4055
|
var VERSION = version;
|
|
@@ -7080,7 +7080,7 @@ var openapi_default = {
|
|
|
7080
7080
|
openapi: "3.0.3",
|
|
7081
7081
|
info: {
|
|
7082
7082
|
title: "Switcher API",
|
|
7083
|
-
version: "0.2.
|
|
7083
|
+
version: "0.2.14",
|
|
7084
7084
|
description: "Authenticated provider/profile/catalog control plane. Launches run locally; the API never returns provider credentials."
|
|
7085
7085
|
},
|
|
7086
7086
|
security: [
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A process identity that survives PID reuse: the PID plus its start time.
|
|
3
|
+
* `comm` is read on Linux only, to recognise a `systemd --user` subreaper.
|
|
4
|
+
*/
|
|
5
|
+
export type ProcessEntry = {
|
|
6
|
+
pid: number;
|
|
7
|
+
ppid: number;
|
|
8
|
+
start: string;
|
|
9
|
+
comm?: string;
|
|
10
|
+
};
|
|
11
|
+
export type ProcessTable = Map<number, ProcessEntry>;
|
|
12
|
+
export type ProcessReader = () => Promise<ProcessTable>;
|
|
13
|
+
/** Read every visible process's parent and start time. Unsupported platforms fail closed. */
|
|
14
|
+
export declare function readProcessTable(): Promise<ProcessTable>;
|
|
15
|
+
/** Every live process descended from one of the roots, excluding the excluded subtrees. */
|
|
16
|
+
export declare function descendants(table: ProcessTable, roots: Iterable<number>, exclude?: Iterable<number>): ProcessEntry[];
|
|
17
|
+
/**
|
|
18
|
+
* True for a process that was reparented away from its creator: to init or launchd
|
|
19
|
+
* (PID 1), or on Linux to a `systemd --user` subreaper.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isOrphan(entry: ProcessEntry, table: ProcessTable): boolean;
|
|
22
|
+
export type EnvironmentDigestProbe = (pids: number[], digest: string) => Promise<number[]>;
|
|
23
|
+
/**
|
|
24
|
+
* The PIDs among `pids` whose environment carries a launch token with this SHA-256
|
|
25
|
+
* digest. Token-shaped values are hashed and dropped; no value is returned or kept.
|
|
26
|
+
* Only the user's own processes are readable. Unsupported platforms find nothing.
|
|
27
|
+
*/
|
|
28
|
+
export declare function processesHoldingTokenDigest(pids: number[], digest: string): Promise<number[]>;
|
package/dist/serve/index.js
CHANGED
|
@@ -68,7 +68,7 @@ function canonicalPolicyJSON(value) {
|
|
|
68
68
|
return JSON.stringify(value, (_key, item) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item);
|
|
69
69
|
}
|
|
70
70
|
// package.json
|
|
71
|
-
var version = "0.2.
|
|
71
|
+
var version = "0.2.14";
|
|
72
72
|
|
|
73
73
|
// src/version.ts
|
|
74
74
|
var VERSION = version;
|
|
@@ -922,7 +922,7 @@ var openapi_default = {
|
|
|
922
922
|
openapi: "3.0.3",
|
|
923
923
|
info: {
|
|
924
924
|
title: "Switcher API",
|
|
925
|
-
version: "0.2.
|
|
925
|
+
version: "0.2.14",
|
|
926
926
|
description: "Authenticated provider/profile/catalog control plane. Launches run locally; the API never returns provider credentials."
|
|
927
927
|
},
|
|
928
928
|
security: [
|
|
@@ -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.
|
package/hasna.contract.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"name": "switcher",
|
|
5
5
|
"class": "cli-with-store",
|
|
6
6
|
"contractVersion": "v1",
|
|
7
|
-
"kitVersion": "1.3.
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hasna/switcher",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
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,7 +78,7 @@
|
|
|
77
78
|
"@types/pg": "8.23.1"
|
|
78
79
|
},
|
|
79
80
|
"dependencies": {
|
|
80
|
-
"@hasna/contracts": "1.3.
|
|
81
|
+
"@hasna/contracts": "1.3.8",
|
|
81
82
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
82
83
|
"jsonc-parser": "3.3.1",
|
|
83
84
|
"yaml": "2.9.0",
|