Inside a hosted TurnScope, bind_state of a plain Scope with the same tenant,
agent, actor, owner and administration right passed the refusal and then
installed the plain Scope, so the body ran with no session, turn id or fence.
bind_state now keeps the bound turn for such a bind, as scope_for_profile does
when re-entering the bound profile. A nested TurnScope is still bound as given,
so a newer turn carries its newer fence; local scopes are unchanged.
Refs #146503
Who administers an agent is decided where the signed claim is made (in Hermes Cloud: a personal
agent's owner, and the org's owners and admins for an agent owned by the org or a group), so the
scope carries only the result. A personal agent its owner shared runs a teammate's turn as that
teammate, unable to administer it.
A Scope now says who owns the agent and what the activity may do on it, and a TurnScope says
which claim the turn runs under:
- Scope gains keyword-only owner (Principal for a personal agent, Group for a shared one) and
role ("owner" | "manager" | "member"). A personal agent is used only by its owner, so its only
role is "owner"; a group agent's roles are "manager" and "member". can_administer is true for
owners and managers. A hosted scope must name both; a local scope defaults to the local person
as owner with role "owner", so every local caller is unchanged.
- TurnScope gains fence: Fence(epoch, generation), the owner epoch and runtime generation the
admission claim stamped. bind_turn(session, turn, fence) requires it; claim_fence(row) reads it
off a claimed session_admissions row. The authority's turn path, the owner-side cron run and
the managed worker bind the real fence.
- Inside a hosted scope a bind must keep tenant, agent, actor, owner and role; changing any of
them raises ScopeSwitchRefused.
- bind_profile_scope / scope_for_profile drop the unused turn= tuple: a turn is marked only with
bind_turn, which has the fence.
Tests that executed a never-claimed admission row now claim it first, as the drain does
(tests/gateway/session_claims.py); hand-built cron rows carry the claim's columns.
A managed ${VAR} in terminal: resolved against whatever was bound at the bind (or the launch
environment), so entering profile B from A froze A's value into B's policy. The policy is now built
with the target's home and secrets in place, as a standalone load of that profile would read them.
Two regressions from the merge with main:
- Hydrating a profile's secret sources reads that profile's `secrets:` config through
get_hermes_home(). Main's per-turn loader moved the home first; bind_state hydrated before
installing anything, so entering profile B from inside A hydrated B from A's configuration (and
the launch profile, entered from a routed one, from the routed one's). The home now moves for
the hydration and secret-mapping reads alone.
- A deferred terminal policy captured the caller's env_overlay dict by reference, so a caller that
changed its mapping after the switch changed the installed policy at its first read. The bind
copies it.
Round-3 review findings:
- bind_profile_scope decided 'this is the launch profile' from the live
HERMES_HOME, so a host that pins the launch home and mirrors a served profile
into HERMES_HOME gave that profile the launch profile's env-only credentials.
It now uses the pinned identity (_is_process_home), as owner_secret_scope does;
an unstamped set_secret_scope names the same routing home.
- get_hermes_home_override read hermes_scope while another thread's first import
was still running it (AttributeError). A module without current_state yet is
treated as absent: nothing can be bound before it finishes importing.
- Folding only merged the top two changes, so one token variable reassigned in a
loop still grew the chain (the live token sat on top of the dead ones). Every
run of dead same-owner changes now folds, so depth follows live tokens.
- test_bot_roster_worker_session (new on main) imports SessionActor.
Round-2 review findings:
- A raw terminal mapping could still replace the bound profile's policy with
another profile's. Raw policies now go through bind_terminal_policy(policy,
profile_home=...): inside a bound scope only the bound agent's own policy is
accepted. The four callers pass the session's owning profile.
- A partial bind (home_override=False) inherited the home override from an
earlier change, so resetting that change took it away. A bind now copies the
pieces it keeps, so it is whole on its own.
- bind_state switched directly between hosted agents and tenants. Inside a
hosted scope only a bind of the same tenant and agent is allowed.
- Setters whose tokens are discarded grew the state chain without bound. Changes
that can no longer be reset fold together on the next push.
- A cross-profile install_profile_terminal_scope dropped its env_overlay.
- HX016 flagged a bare 'from . import set_secret_scope', whose module it cannot
know.
- The TUI heap-trim ordering test observes the whole-profile release.
Review found three ways around the guards in the first commit: set_terminal_scope
was unguarded (four production callers), set_secret_scope(None) cleared secrets
alone so get_secret fell back to the launch profile, and a stale or out-of-order
reset token moved one piece back while the others stayed. Guards around three
separate context variables leave every unguarded path able to split them.
The four pieces are now one immutable ScopeState in one context variable. Every
setter is a change to that value; a reset removes its own change and replays the
later ones, dropping those made inside a scope the result no longer has, so any
reset sequence lands on a state whole binds produce. Leaving a hosted agent by
any reset other than the bind that entered it refuses.
- set_terminal_scope narrows a bound local scope's own policy; clearing it, or
setting it in a hosted scope, refuses. set_secret_scope(None) inside a scope
refuses.
- bind_profile_scope and bind_state refuse a local bind inside a hosted scope.
- The TUI turn holds one token instead of four; housekeeping still runs under
the session's whole profile scope and the release resets it at once.
- HX016 sees relative imports and no longer flags a local object named like a
setter's module.
- The historical updater reads the override through get_hermes_home_override
when hermes_constants no longer exposes the raw context variable.
Profile state lived in three independent globals (home override, secret
scope, terminal policy) that every entry point set separately, so they could
describe different profiles. 85 call sites set them by hand.
hermes_scope.Scope(tenant, agent, actor) is now the single value, in one
contextvar; actor is Principal(id) or System(reason), and TurnScope adds
session and turn. agent/runtime_scope.py is the one module that turns a scope
into profile state: bind_profile_scope binds and restores all of it together.
The process's own home keeps its launch semantics; any other home is hydrated
from that profile's sources and stamped as routed.
The legacy setters keep their signatures. Inside a bound scope, a call naming
another profile moves the whole scope (local) or raises ScopeSwitchRefused
(hosted). Gateway turns, cron, the TUI, model switch, kanban spawn and the
managed worker bind once at entry. HX016 blocks new direct setter calls;
existing ones migrate when their function is next edited.