Profile organisation affinity¶
Automatic login swaps prefer capacity in the live login's organisation, and make a confirmed crossing only when capacity forces it. Crossing organisations can make earlier thinking unavailable on the next turn. The visible conversation remains available and the session can continue. Swapping back does not restore earlier thinking.
This policy applies to the rotator, auto-switch and nested rerun selection, including unread candidates, overflow and fallback paths. It adds restrictions to the existing profile rules; it grants no swap authority.
Contents¶
- Organisation identity
- Selection rules
- Configuration and operator commands
- Warnings
- Swap record
- Session delivery
- Compatibility and platform limits
Organisation identity¶
The resolver follows the actual hosted login using existing account and hosting
metadata, before the swap changes that metadata. A profile directory's name
does not establish its organisation. Account identity comparison, including
Account::same(), does not establish organisation affinity either.
Organisation discovery does not inspect credentials. Existing credential ownership checks remain separate and authoritative.
Auto-switch reads a login's organisation from its expected record
(.auto-switch.d/expected/<profile>.json) through the same reader the swap
guard uses. A record that is missing, unsafe, malformed or without an org
entry reads as no record, so the organisation is unknown. The rotator takes
the organisation from its trusted identity reads; its swap guard compares
each read with the same expected record, organisation included, and refuses
the swap on a mismatch. Wherever a record exists, the two producers therefore
agree for every swap that goes ahead.
| Source ID | Destination ID | Affinity |
|---|---|---|
| Present and valid | Present, valid and exactly equal | Same |
| Present and valid | Present, valid and unequal | Crossing |
| Either missing, null, malformed or otherwise unresolved | Any | Unresolved |
Two unknown IDs never establish affinity. IDs must be validated, nonempty strings; display names and directory names are not substitutes.
Earlier organisation-blind selection ranked eligible accounts by capacity
without using organisation affinity. Account records already stored an org
alongside account, but that metadata did not restrict ranking. This allowed
threshold, plan-room and upgrade moves across organisations. These findings
follow account metadata and selection behavior, without credential inspection.
Selection rules¶
First apply existing eligibility and capacity checks. Then restrict candidates by affinity, before applying the existing ranking within the permitted set. Only a confirmed crossing, where both organisations are known and differ, is restricted. An unknown organisation on either side is not a crossing.
Each decision first works out why the session may move
(organisation_affinity::Need::of), in one place for all three selectors:
| Live login | Need |
|---|---|
| Paid overflow login, blocked or not | Leave overflow (cost relief) |
| Plan login that needs capacity relief | Capacity relief |
| Plan login with capacity | Upgrade |
The need comes from the live reading as measured. The rotator's projected or substituted live figure (live reading freshness) is decided with that same need, never one worked out on the forecast, so a projected or substituted swap never makes a confirmed crossing, nor makes a failed-probe crossing sooner. A crossing waits for the reading as measured.
| Need and final swap purpose | Permitted candidates |
|---|---|
| Any, with a known source and an eligible Same target that has capacity | Same targets only |
| Any, otherwise | Same and Unresolved targets |
| Capacity relief or leave overflow, with no eligible Same target that has capacity | Crossing targets as well |
| Upgrade | Never a Crossing target |
| Leaving overflow through its room fallback, with no eligible Same target that has room | Crossing targets as well |
The table is the policy of auto-switch and nested rerun selection. The rotator applies its own rule, described below, to the same inputs.
Leaving a paid overflow login counts as cost relief whether or not the overflow login is blocked. Staying keeps spending money until the login is blocked; crossing costs earlier thinking on the next turn, and the visible conversation remains. The policy takes the crossing: a session on overflow returns to a plan login in another organisation once no plan login in its own organisation has capacity, and the swap carries the confirmed-crossing warning. A same-organisation plan login with capacity is always taken first.
In auto-switch, the plan-room pool applies the same rules with room in place of strict capacity. Auto-switch's room test is both windows below 100%. A Same target restricts the pool only when the plan-room fallback, offered that target alone, would pick it. A Same login that has no room therefore never excludes an Unresolved login that has some. From a plan login the plan-room pool never crosses. From an overflow login it is the way off paid spend, including the best-headroom fallback used when the overflow login is blocked, so it may cross on the same terms as the selection pool. A blocked overflow login therefore moves to a plan login with room in another organisation rather than raising an alert and staying.
Rotator rule¶
The rotator does not use the capacity and plan-room pools. It chooses its target by the target rule, which finds the eligible logins that qualify against the live login, and it applies organisation affinity to those logins in this order, taking the first group that has a qualifying login:
- logins in the live login's organisation, whatever their tier or room;
- logins whose organisation is unresolved;
- logins in another organisation, a confirmed crossing.
The third group is available only when the live login has no room now (a window at 100%, a spend limit or an active recorded block), is an overflow profile, or its probe failed. A live login that still has room never crosses, and an upgrade never crosses. A live login with no room is a login that cannot run work, and staying on it stops work for certain, whereas a crossing costs earlier thinking and leaves the visible conversation in place. So a login at 100% moves to a login in another organisation when nothing in its own organisation, and nothing unresolved, qualifies, and the move carries the confirmed-crossing warning. A live login whose probe failed is treated as before: its only crossing is the failed-probe swap, which waits for three failures in a row, and a substituted figure never makes that crossing sooner.
After the swap is authorised, the rotator compares the two organisations
that authorisation already read, with no further read, and refuses a crossing
the rule did not allow, so an unresolved login that turns out to be in another
organisation cannot make a silent crossing. A swap
with an unknown organisation on either side goes ahead and carries the
Unresolved warning. organisation_affinity is not changed by this rule, so
auto-switch is unaffected.
A better headroom score in another organisation does not override available
same-organisation capacity. A plan login at or above --session-at is not
such capacity while a move onto a credit profile can be made: it does not
stop the rotator from reaching a credit profile whose organisation is
unresolved or, when the third group is available, one in another
organisation, so the rotator moves onto that credit profile with the matching
warning. Such a plan login still keeps the API key unused; see
Plan logins near their session limit.
Missing metadata never keeps a session where it is: a session on a paid overflow login whose organisation is unknown still
returns to a plan login once one recovers. Before this policy existed it did,
and an unknown organisation does not change that. Such a swap carries the
Unresolved warning.
Overflow and fallback selection use the final purpose of the move. A fallback cannot turn a preference-only move into capacity necessity or into a confirmed crossing. Unread auto-switch candidates receive the same checks; absence of a reading does not establish capacity.
Authorisation, exclusions, leases, spend limits, pins, freshness, gain and capacity rules still apply. Organisation affinity cannot make an otherwise ineligible target eligible. Before completing a swap, execution re-reads the source and target organisations under the execution locks and refuses the swap if either changed since selection.
The auto-switch swap and nested rerun share one policy function
(organisation_affinity::pools), called once per decision with the need from
Need::of, which returns both the selection pool and the plan-room pool.
Auto-switch reaches it through one helper (auto_switch/affinity.rs). The
rotator does not call it: it uses its own single pool
(rotate/affinity.rs, affinity_pool::pool), which is the rule above.
Selection examples¶
These are policy examples, using profile names from the rotation guide:
| Situation | Result |
|---|---|
work needs capacity; spare is Same and eligible; personal is Crossing with more headroom |
Select within Same capacity, preserving its existing ranking |
work needs capacity; no Same target has capacity; personal passes all relief checks |
Crossing may proceed, with a confirmed-crossing warning |
work requests an upgrade; personal is the only better target and is Crossing |
No upgrade |
work requests an upgrade; personal is the only better target and its ID is unknown |
Upgrade, with a risk warning |
work is a paid overflow login with no recorded organisation; plan login spare recovers |
Back to spare, with a risk warning |
work is a paid overflow login in organisation B; plan login spare (A) recovers and nothing in B has capacity |
Back to spare, with a confirmed-crossing warning |
work is a paid overflow login in B; spare (B) and personal (A) both have capacity, personal with more headroom |
Back to spare |
work is a blocked overflow login in B; no plan login has capacity; personal (A) has room (for the rotator, an eligible login) |
Moves to personal, with a confirmed-crossing warning |
| Auto-switch's plan-room fallback from a plan login finds only Crossing targets | No plan-room swap |
Rotator: work (A) has room and is past --session-at; only third (B) qualifies |
Stay, with a stay note; a login with room never crosses |
Rotator: work (A) is at 100% of its session; spare (A) and third (B) are eligible |
Move to spare, even if third has more room; no crossing |
Rotator: work (A) is at 100% of its session; spare (A) is above its week ceiling and third (B) is eligible |
Move to third, with a confirmed-crossing warning |
Rotator: work (A) is at 100% of its session; spare (A) is a plan login at or above --session-at, no plan login is below it, and credit (B) is a usable credit profile |
Move to credit, with a confirmed-crossing warning; a plan login near its session limit is not room for a credit profile |
Rotator: work (A) reads 63%, projected to 76%; spare (A) has room on the projection; third (B) has more room |
Projected swap to spare |
Rotator: work (A) reads 63%, projected to 76%; only third (B) has room |
Stay: a projected move never crosses. The move to third is made in the cycle in which work has no room |
Configuration and operator commands¶
The affinity policy is automatic and has no separate opt-out or organisation override. Existing thresholds, exclusions, pins and overflow settings keep their meaning but cannot bypass affinity restrictions. No new tuning flag is required.
The existing operator commands remain the entry points:
| Command | Relevant behavior |
|---|---|
amplihack profile rotate --dry-run |
Previews selection under the affinity policy without moving a login or writing a swap record |
amplihack profile rotate |
Applies the policy, writes an affinity: line to the rotate log and records the organisation outcome in the swap record |
amplihack profile rotate status |
Prints the last swap's warning (last swap: ...) when that swap was a crossing or unresolved |
amplihack profile auto-switch status |
Prints the last auto-switch crossing (last crossing: ...), with both profiles, both organisations and the warning |
Auto-switch makes its swaps inside the status-line wrapper that Claude Code
runs after each status-line update, not in any profile auto-switch
subcommand. When such a swap is a crossing or unresolved, the wrapper:
- appends the
affinity:line below to the rotate log; - writes
<profile-root>/.auto-switch.d/affinity/last-crossing.jsonwithat,dir,from,to,from_org,to_organdaffinity, replacing any earlier record; - writes
auto-switch in <dir> from <profile> to <profile>: <warning>on its own stderr. Claude Code does not display a status-line command's stderr, so this copy is for manual runs only.
A swap within one organisation writes none of these.
Rotate log line¶
The rotator and auto-switch write the same rotate-log line after a swap that is a crossing or unresolved:
<from> and <to> are profile names. No organisation ID is written.
A profile with no expected record (.auto-switch.d/expected/<profile>.json)
has an unknown organisation, so every swap to or from it is unresolved and
writes this line, the stderr line, the last-crossing.json record (for
auto-switch) and the warning in the next resume. This is intended: affinity
cannot be established without the record. Test fixtures that do not set up
expected records see these lines; a fixture that should swap silently records
one organisation for both profiles.
For setup and command examples, see Rotate Claude Logins and Switch Accounts Automatically.
Warnings¶
Rotate logs, rotate status, auto-switch status and session delivery use the same text:
| Outcome | Warning text |
|---|---|
| Crossing | Cross-organisation swap: earlier thinking becomes unavailable on the next turn. The visible conversation remains and the session can continue. |
| Unresolved | Organisation affinity is unresolved: earlier thinking may become unavailable on the next turn. The visible conversation remains and the session can continue. |
| Same | None |
The warning text contains no credentials, tokens, organisation IDs or
conversation content. The swap record and the last crossing line of
auto-switch status show organisation IDs; neither reaches a session.
Swap record¶
The rotator's swap record (<profile-root>/.auto-switch.d/rotate/swap.json,
see profile-command) carries three fields for this
feature:
| Field | Meaning |
|---|---|
from_org |
The outgoing login's organisation ID, or absent when unknown |
to_org |
The incoming login's organisation ID, or absent when unknown |
affinity |
same, crossing or unresolved, derived from the two IDs |
A record whose affinity disagrees with its IDs, or whose IDs are not valid,
is rejected as a whole. The record is overwritten by each swap; it describes
the latest swap only. The rotate log keeps the history.
Session delivery¶
The warning reaches a session through the limit-resume hook, with the
resume that follows the swap. When the hook decides to resume a stopped
session, it reads the organisation outcome from the swap record that woke
it, and from nothing else:
- On the exit-2 wake, the hook writes the warning on stderr, on the line before the resume message. Claude Code passes exit-2 stderr to the session.
- On the tmux route, the warning text replaces the word
continuein the keys it sends, followed byContinue.
A stop that does not end in a resume (rotator down, auto-switch off, no swap
with room, the session moved on) gets no warning. A session that keeps
running through a swap is not told by the hook; the swap is in the rotate log,
rotate status and, for an auto-switch swap, auto-switch status, and
Claude Code shows its own notice when it cannot use thinking from another
organisation.
The hook keeps no per-session warning state. Each call decides from the swap
record alone, so delivery does not depend on which process is the hook's
parent. Claude Code runs hook commands through /bin/sh -c; where /bin/sh
is dash, the shell does not exec the command, and the hook's parent is the
short-lived shell, not the Claude Code process. A design that identified the
session by the hook's parent would lose its state when the shell exits. The
test shell_wrapped_hook_delivers_the_warning_from_the_swap_record_alone
(bins/amplihack-hooks/tests/limit_resume_binary.rs) runs the real binary
through /bin/sh -c twice to cover this.
The same shell wrapper once stopped the tmux route: the pane check compared the pane's terminal with the hook parent's standard input, and on such hosts the parent is the shell, whose standard input is the payload pipe. The check now walks up the hook's own ancestry to the nearest process whose standard input is a terminal, which passes the shell and reaches Claude Code, so the route is available on those hosts too (issue #2695). The warning goes out on whichever route the resume takes.
Warning delivery does not change judge() or resume authorisation. A
failure to write the warning to stderr still exits 2. The tmux route sends
the fixed warning text with send-keys -l and never puts record contents
into a command.
Auto-switch does not write the rotator's swap record and does not wake sessions. It prints the warning on its own stderr when it makes a crossing or unresolved swap.
Compatibility and platform limits¶
Legacy swap records remain readable. Absent organisation fields mean unknown
historical information, and a record without an affinity field produces no
warning. New records always carry affinity.
Affinity does not remove existing platform restrictions on rotation or session discovery. See the rotation guide's limits. Waiting for a swap and the tmux route are Linux-only. Restoring earlier thinking and measuring the quality of later work are outside this feature.
Selection rationale¶
In auto-switch, strict capacity relief and plan-room fallback use separate candidate pools. The existing selectors define capacity using the configured thresholds; affinity does not introduce a second percentage limit. Same-organisation room above a threshold cannot hide a different organisation's eligible capacity relief. When no strict relief target is selected, existing plan-room policy can still choose an eligible, confirmed same-organisation target with room.
The rotator has one pool. Every eligible login that qualifies is in it, and
organisation decides only the order in which the groups are tried, with
crossings last and only when the live login cannot run work. This replaces a
rule under which the rotator's
plan-room swap never crossed
organisations and never moved to a login at or above --session-at. Under
that rule a login at 100% stayed where it was whenever every login with room
was in another organisation or at or above --session-at.