credential_pool_admin.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """Locked credential-pool administration and target resolution."""
  2. from __future__ import annotations
  3. import time
  4. from dataclasses import replace
  5. from typing import Any, Optional, Tuple, TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from agent.credential_pool import PooledCredential
  8. def _cleared_status_copy(entry: PooledCredential) -> PooledCredential:
  9. from agent.credential_pool import _CLEAR_STATUS
  10. # The reset marker lets a live pool in another process tell "reset after my cooldown" from
  11. # "never had a status" — both read as bare None on disk (#89415).
  12. return replace(entry, **_CLEAR_STATUS, model_cooldowns=None, status_cleared_at=time.time(),
  13. extra={k: v for k, v in entry.extra.items() if k != "failure_reason"})
  14. class CredentialNotSavedError(RuntimeError):
  15. """``add_entry`` wrote the pool but the store does not hold the new row."""
  16. class CredentialPoolAdminMixin:
  17. def reset_status(self, credential_id: str) -> Optional[PooledCredential]:
  18. """Clear only the target's local error state, preserving sibling cooldowns."""
  19. with self._lock:
  20. entry = self._find(lambda e: e.id == credential_id)
  21. if entry is None:
  22. return None
  23. cleared = _cleared_status_copy(entry)
  24. self._replace_entry(entry, cleared)
  25. self._persist(status_cleared_ids=[cleared.id])
  26. return cleared
  27. def reset_statuses(self) -> int:
  28. """Clear exhaustion state on every entry. Returns how many were cleared.
  29. ``failure_reason`` lives in ``extra``, not a dataclass field, so it is
  30. stripped explicitly. The persist declares the cleared ids because the
  31. disk-recency merge reads a cleared ``last_status_at`` (None -> epoch 0)
  32. as a stale snapshot and would copy a still-binding cooldown back.
  33. """
  34. from agent.credential_pool import _CLEAR_STATUS
  35. with self._lock:
  36. stale = [
  37. e for e in self._entries
  38. if e.last_status or e.last_status_at or e.last_error_code or e.failure_reason or e.model_cooldowns
  39. ]
  40. if stale:
  41. stale_ids = {e.id for e in stale}
  42. self._entries = [
  43. _cleared_status_copy(e) if e.id in stale_ids else e
  44. for e in self._entries
  45. ]
  46. self._persist(status_cleared_ids=list(stale_ids))
  47. return len(stale)
  48. def remove_index(self, index: int) -> Optional[PooledCredential]:
  49. with self._lock:
  50. if index < 1 or index > len(self._entries):
  51. return None
  52. removed = self._entries.pop(index - 1)
  53. self._entries = [replace(e, priority=p) for p, e in enumerate(self._entries)]
  54. self._persist(removed_ids=[removed.id])
  55. if self._current_id == removed.id:
  56. self._current_id = None
  57. return removed
  58. def move_entry(self, credential_id: str, priority: int) -> Optional[PooledCredential]:
  59. """Place an entry at a clamped zero-based position and persist contiguous priorities."""
  60. from agent.credential_pool import _normalize_pool_priorities
  61. with self._lock:
  62. entry = self._find(lambda e: e.id == credential_id)
  63. if entry is None:
  64. return None
  65. others = [e for e in self._entries if e.id != credential_id]
  66. others.insert(max(0, min(int(priority), len(others))), entry)
  67. entries = [replace(e, priority=p) for p, e in enumerate(others)]
  68. # Apply load-time ordering now so the reported position survives reload.
  69. _normalize_pool_priorities(self.provider, entries)
  70. self._entries = sorted(entries, key=lambda e: e.priority)
  71. self._persist()
  72. return self._find(lambda e: e.id == credential_id)
  73. def resolve_target(self, target: Any) -> tuple[Optional[int], Optional[PooledCredential], Optional[str]]:
  74. raw = str(target or "").strip()
  75. if not raw:
  76. return None, None, "No credential target provided."
  77. with self._lock:
  78. for idx, entry in enumerate(self._entries, start=1):
  79. if entry.id == raw:
  80. return idx, entry, None
  81. label_matches = [
  82. (idx, entry)
  83. for idx, entry in enumerate(self._entries, start=1)
  84. if entry.label.strip().lower() == raw.lower()
  85. ]
  86. if len(label_matches) == 1:
  87. return label_matches[0][0], label_matches[0][1], None
  88. if len(label_matches) > 1:
  89. return None, None, f'Ambiguous credential label "{raw}". Use the numeric index or entry id instead.'
  90. if raw.isdigit():
  91. index = int(raw)
  92. if 1 <= index <= len(self._entries):
  93. return index, self._entries[index - 1], None
  94. return None, None, f"No credential #{index}."
  95. return None, None, f'No credential matching "{raw}".'
  96. def add_entry(self, entry: PooledCredential) -> PooledCredential:
  97. from agent.credential_pool import (
  98. _borrowed_single_use_pool_root, _next_priority, _profile_owns_pool_provider,
  99. write_credential_pool,
  100. )
  101. from hermes_cli import auth as auth_mod
  102. from hermes_cli.auth_oauth_grants import SINGLE_USE_REFRESH_POOL_PROVIDERS
  103. with self._lock:
  104. entry = replace(entry, priority=_next_priority(self._entries))
  105. self._entries.append(entry)
  106. borrowed_ids = self._borrowed_root_ids
  107. # Decided now, not from load_pool()'s snapshot: a profile with no
  108. # rows of its own borrows from root even when root has none, and
  109. # ``_persist`` would send the new row to the update-only root merge,
  110. # which drops it (#103694).
  111. profile_borrows = (
  112. self.provider in SINGLE_USE_REFRESH_POOL_PROVIDERS
  113. and not _profile_owns_pool_provider(self.provider)
  114. and _borrowed_single_use_pool_root() is not None
  115. )
  116. if borrowed_ids or profile_borrows:
  117. # ``hermes -p <profile> auth add <single-use provider>``: a fresh
  118. # login inside the profile is the profile's OWN credential, so
  119. # it goes to the profile store, never to root. Only that row is
  120. # written: the rest of this pool is root's (its rows, or its
  121. # ``providers.<id>`` login seeded through the fallback), and a
  122. # copy would fork that single-use refresh token (#100339). Rows
  123. # the profile seeds from its own sources come back on the next
  124. # load. Once the profile owns rows, the root fallback for this
  125. # provider is shadowed.
  126. self._entries = [entry]
  127. written = write_credential_pool(
  128. self.provider, [entry.to_dict()],
  129. token_bases=self._persisted_token_pairs,
  130. )
  131. self._persisted_token_pairs = auth_mod._token_pairs_by_id(written)
  132. self._borrowed_root_ids = set()
  133. else:
  134. self._persist()
  135. # Callers print "Added" on return; a row the store did not keep
  136. # must fail loudly instead.
  137. if not any(isinstance(row, dict) and row.get("id") == entry.id
  138. for row in auth_mod.read_credential_pool(self.provider)):
  139. self._entries = [e for e in self._entries if e.id != entry.id]
  140. raise CredentialNotSavedError(
  141. f"The {self.provider} credential was not saved: {auth_mod._auth_file_path()} "
  142. f"does not contain it after the write, so nothing was added. Check that the file "
  143. f"is writable, then run the command again."
  144. )
  145. return entry