Two Accounts in One Process: Hermes Stops Lending the Launch Profile's Modal Login to Sandboxes

A colleague hands you their laptop for the weekend: one Hermes install, two profiles, and a single gateway process serving both. The work profile is wired to the company Modal account; the demo profile exists to show customers what the agent can do, and it is supposed to live somewhere cheap and disposable. On Sunday the Modal invoice arrives, and every sandbox that demo created is billed to work. Nothing in the logs looks broken, because from the sandbox’s side nothing was: it was launched with a perfectly valid token, just the wrong profile’s.
That mismatch is what #156051 closes, with a follow-up in #157991. Both merged on 2026-11-19 at 21:44 and 21:59 UTC — after the v0.23.3 tag was cut at 20:51 UTC, so they are not in v0.23.3 and will ship in the next release. Track main to use them now.
Three words worth defining first
A profile is an independent identity under one Hermes install: its own config, its own runtime directory, its own credentials, its own bots. You can run one profile, or five, without them knowing about each other.
A multiplexed gateway is the thing that makes running five of them cheap. Instead of one gateway process per profile, a single process hosts the adapters for several profiles and routes each incoming request to the profile it came from — same binary, one memory footprint, but every request still executes inside the profile that owns it. That routing is exactly the boundary this bug crossed.
A sandbox is where commands actually run when the agent needs a machine of its own: an isolated VM on Modal, created through the Modal SDK, doing work on your behalf and on your bill. Creating one requires a credential, and the credential is the whole story here.
Symptom: a sandbox that bills the wrong account
With a single profile, the launch account and the serving account are the same by construction, so nothing can go wrong in a visible way. Turn on multiplexing and they are no longer the same: the gateway process is started once, by whichever profile was up first, but the tool call that reaches the Modal backend may belong to a different profile entirely.
The direct Modal backend used to build its sandbox through the SDK’s process-wide default client. That default client is assembled from the process environment — MODAL_TOKEN_ID / MODAL_TOKEN_SECRET as the gateway saw them at startup — plus the machine-level login stored in ~/.modal.toml. In other words, it belonged to whoever launched the process, or to whoever last ran a Modal login on that box, not to whoever the request was for.
The consequences are all quiet ones:
- Billing and quota land on the wrong account, which is how you find out — weeks later, on an invoice.
- Audit trails point at the wrong identity, so “which profile created this VM” has no trustworthy answer after the fact.
- Blast radius: the token that a sandbox holds is the launching account’s token, so revoking credentials for one profile can break another profile’s running work, and a compromise in the demo path inherits the work account’s reach.
- Status lies too. The selector and readiness reporting could show the direct Modal path as available to a profile that had no business borrowing that machine login at all.
Root cause: the default client is process-wide, not profile-wide
The bug is a one-line mismatch between two scopes. Routing happens per request; client selection happened per process. Everything else follows from that.
The PR body states it as a three-line diagram, which is about as compact as a security bug gets:
Before: routed B -> process default SDK client -> launch account A
After: routed B -> B's complete scoped token pair -> B's SDK client
routed B -> missing / incomplete B token pair -> refuse before any SDK request
The fix lives in tools/environments/modal.py. _routed_token_pair() reads MODAL_TOKEN_ID and MODAL_TOKEN_SECRET through the secret scope bound to the profile serving the request — and only when serves_routed_profile() is true. Under multiplexing there is no fallback to some other profile’s scope; if the pair is missing or incomplete, the function raises a ValueError before a single SDK request goes out, and the error text is blunt about it: another profile’s Modal token and the shared ~/.modal.toml login will never be used by this one.
The selected client is then passed to both halves of sandbox creation, the App lookup and the Sandbox create call, so the object that resolves the app and the object that starts the VM cannot disagree about whose credentials they are.
| Setup | Before | After |
|---|---|---|
Single profile, logged in via ~/.modal.toml |
Direct Modal worked off the machine login | Unchanged — env var and config file resolution still respect ~/.modal.toml |
| Multiplexed, both profiles hold their own tokens | Sandbox billed to the launch profile | Each profile’s own token pair, its own client |
| Multiplexed, the serving profile is missing a token | Sandbox billed to the launch profile | Refused before any SDK request, with an error naming the missing pair |
Concurrency, caching, and hot rotation
Once the credential is per-profile, the obvious follow-up question is what happens when twenty tool calls from the same profile want a sandbox at the same time — and what happens when you rotate a token while sandboxes are still running.
The current client is cached per profile home, keyed by the pair (home, token pair). Building it is deduplicated rather than serialized: for one home+token pair there is at most a single in-flight build, shared across worker loops, so fifty concurrent launches from the same profile produce one client and forty-nine waiters. Those waiters are wrapped in asyncio.shield, which matters more than it sounds — cancelling one caller (a timeout, a user interrupt) must not cancel the build that everyone else is still waiting on.
The cache is also careful about staleness. A failed build is evicted instead of being cached as a permanent failure, and an older build that finishes late can never overwrite a newer cached client — otherwise a slow, doomed attempt from a rotated-away token could quietly restore the wrong credentials.
Rotation itself is deliberately non-destructive: the new token pair simply forms a new cache entry, and the previous client is not closed while existing sandboxes are still using it. Rotating MODAL_TOKEN_SECRET should not kill a job that is halfway through.
Two smaller details matter for operations. All of this initializes inside the existing 300-second startup window, so a slow Modal endpoint does not silently extend how long the gateway looks unhealthy. And a failure to build the routed client — or a caller timeout while waiting for it — does not fall through to the snapshot path; only a genuine snapshot restore failure goes back to the base image as before. A credential problem is reported as a credential problem.
The follow-up: readiness has to answer the same question
#157991 cleans up the half of the bug that lived outside the constructor. has_direct_modal_credentials() used to run its check before the routing check, and an unscoped read counted as “ready”. In a multiplexed deployment, an unbound caller would therefore be told selector=direct on the strength of the launch token; the constructor would then refuse, because that path issues no SDK call — but “ready” and “constructor” were answering different questions, and the readiness answer was the misleading one.
Now the routing check runs first and the unbound read is treated as not-ready. selector=None, and the caller gets the #156051 refusal explaining that both tokens must be set in this profile. Two decisions, one answer.
How to check your own deployment
If you run more than one profile behind a gateway, spend two minutes on this:
- Both tokens, same place. In the profile’s own
.envor secret source, setMODAL_TOKEN_IDandMODAL_TOKEN_SECRETtogether. One without the other is exactly the case that now refuses loudly — and the case that used to leak silently. - Confirm the pair is there and paired. Print one masked, without echoing the other:
# A profile's Modal credentials must come as a pair
printf '%s\n' "$MODAL_TOKEN_ID" | sed 's/./*/g'
- Check whose account that ID is. A token that is present but belongs to the wrong organization passes every local check and still bills the wrong place.
- Stop depending on the machine login for routed profiles.
~/.modal.tomlis fine for a single-profile box; for multiplexed profiles it is now a source of confusion rather than a fallback. - Wait for the next release on a stable build. Both changes merged at 21:44 and 21:59 UTC on 2026-11-19, after v0.23.3 was cut at 20:51 UTC, so v0.23.3 and older do not have them.
Update (2026-11-25): these changes shipped in v0.23.4 (November 24) —
hermes updatebrings them in. The “not in v0.23.3 / onmainonly” wording above is a snapshot from publication time.
The general shape of this class of bug is worth remembering: an isolation boundary is only as good as its narrowest scope, and a process-wide default is the narrowest thing in the world when several identities share one process. For where per-profile routing decisions get policed, see the gateway profile allowlist; for how backend routing itself evolved, the gateway backend migration report traces the same “who owns this request” question through a different layer. And if you are wiring credentials per profile, the hermes profile reference covers creating and inspecting profiles while the hermes config reference covers where each profile’s settings and secrets live.