MingaClaudeBridge.Route.Probe (minga_claude_bridge v0.1.0)

Copy Markdown View Source

Starts Claude Code without a prompt to read its initialize answer: the account, the models, and the version. No model request is made.

The probe session is not linked to the caller and stops when the caller exits, so a caller killed at a deadline leaves no claude process behind.

Sign-in state

Claude Code 2.1.330 answers initialize whether or not it is signed in. Signed out, account holds only "tokenSource": "none" and "apiProvider". Signed in through claude auth login, account carries subscriptionType (for example "Claude Pro") and the email. signed_in?/1 treats a missing account or "tokenSource": "none" as signed out. This was checked with an empty CLAUDE_CONFIG_DIR and no token, which never touches the user's own login.

Summary

Functions

Measures rawMaxTokens with get_context_usage (with detail: "summary", which uses local estimates and calls no API) after set_model, for each id in model_ids, until deadline. Returns model id to tokens; models not measured before the deadline, or that fail, are left out.

Returns the plan label for a signed-in account, never the email: the account's subscriptionType, else usage_label (the plan named by the last get_usage answer, see MingaClaudeBridge.Route.PlanUsage.plan_label/1), else "Claude Code".

Runs one readiness probe with the route credential's env and stops it. Returns {:ok, info} (see MingaClaudeBridge.Session.info/1) as soon as Claude Code answers initialize, or {:error, reason}, where reason is the transport start error (for example :claude_not_found), the session's death reason (for example {:unsupported_version, v}), or :timeout.

Returns true when Claude Code's initialize account reports a sign-in.

Starts a probe session owned by the caller and waits for initialize. Returns {:ok, session, info}; the caller stops the session (stop/1), and the session also stops when the caller exits.

Stops a probe session. Safe when it already stopped.

Reads plan usage through get_usage (with skip_behaviors: true) on an idle probe session, until deadline (a System.monotonic_time(:millisecond) value). The answer holds the plan's rate-limit windows and subscription_type. Returns nil on any failure.

Functions

context_windows(session, model_ids, deadline)

@spec context_windows(GenServer.server(), [String.t()], integer()) :: %{
  required(String.t()) => pos_integer()
}

Measures rawMaxTokens with get_context_usage (with detail: "summary", which uses local estimates and calls no API) after set_model, for each id in model_ids, until deadline. Returns model id to tokens; models not measured before the deadline, or that fail, are left out.

plan_label(account, usage_label \\ nil)

@spec plan_label(map(), String.t() | nil) :: String.t()

Returns the plan label for a signed-in account, never the email: the account's subscriptionType, else usage_label (the plan named by the last get_usage answer, see MingaClaudeBridge.Route.PlanUsage.plan_label/1), else "Claude Code".

run(config, credential_env)

@spec run(MingaClaudeBridge.Route.Config.t(), %{optional(String.t()) => String.t()}) ::
  {:ok, MingaClaudeBridge.Session.State.info()} | {:error, term()}

Runs one readiness probe with the route credential's env and stops it. Returns {:ok, info} (see MingaClaudeBridge.Session.info/1) as soon as Claude Code answers initialize, or {:error, reason}, where reason is the transport start error (for example :claude_not_found), the session's death reason (for example {:unsupported_version, v}), or :timeout.

signed_in?(account)

@spec signed_in?(map() | nil) :: boolean()

Returns true when Claude Code's initialize account reports a sign-in.

start_ready(config, credential_env, key_prefix)

@spec start_ready(
  MingaClaudeBridge.Route.Config.t(),
  %{optional(String.t()) => String.t()},
  String.t()
) ::
  {:ok, pid(), MingaClaudeBridge.Session.State.info()} | {:error, term()}

Starts a probe session owned by the caller and waits for initialize. Returns {:ok, session, info}; the caller stops the session (stop/1), and the session also stops when the caller exits.

stop(session)

@spec stop(GenServer.server()) :: :ok

Stops a probe session. Safe when it already stopped.

usage(session, deadline)

@spec usage(GenServer.server(), integer()) :: map() | nil

Reads plan usage through get_usage (with skip_behaviors: true) on an idle probe session, until deadline (a System.monotonic_time(:millisecond) value). The answer holds the plan's rate-limit windows and subscription_type. Returns nil on any failure.

This and context_windows/3 never run on the availability path; see MingaClaudeBridge.Route.