Skip to content

fix: remember the Hugging Face token between uploads - #492

Open
Yi-111-a wants to merge 5 commits into
p-e-w:masterfrom
Yi-111-a:fix/remember-upload-token
Open

Yi-111-a wants to merge 5 commits into
p-e-w:masterfrom
Yi-111-a:fix/remember-upload-token

Conversation

@Yi-111-a

@Yi-111-a Yi-111-a commented Oct 9, 2026 •

Copy link
Copy Markdown

fix: remember the Hugging Face token between uploads

Summary

The upload action of the interactive action loop obtained the Hugging Face access token with huggingface_hub.get_token() on every iteration, and only fell back to an interactive prompt if that returned nothing.

Since Heretic deliberately avoids huggingface_hub.login() (it would write the token to disk, which is undesirable on rented or shared GPU servers), get_token() usually returns None unless the user has set HF_TOKEN or a token file. That means a token entered at the prompt was used for exactly one upload: uploading a second model in the same session asked for the token again.

Cache a token entered at the prompt in a local variable for the duration of the action loop, so a second upload reuses it.

Token precedence and recovery

On every upload the token is resolved as huggingface_hub.get_token() or upload_token:

  1. A token configured via HF_TOKEN or the HF token file always wins and is never cached.
  2. Otherwise the token remembered from an earlier prompt is reused.
  3. Otherwise the user is prompted.

A prompted token is remembered only after huggingface_hub.whoami() succeeds. If the Hub rejects the token (HTTP 401), the cache is cleared and the error goes to the action-loop handler, so the next upload attempt prompts again. Transient failures (network errors, rate limits, server errors) leave a cached token in place, so retrying the upload does not require re-entering a token that may still be valid. A configured token that fails validation is used again on the next attempt, which is the same as before this change; the user recovers by fixing HF_TOKEN or the token file.

Why this doesn't weaken the security posture

The cache is a plain local variable in run():

  • it is never written to disk,
  • it is never stored in settings, so it cannot be exported in a saved configuration,
  • it is discarded as soon as the action loop ends, and
  • huggingface_hub.get_token() is consulted first on every upload, so a token configured via HF_TOKEN or the token file keeps taking precedence over anything cached.

Related

Fixes #142.

The upload action re-read the token from huggingface_hub.get_token() on every
iteration of the action loop. When no token is stored on disk (the default,
since Heretic deliberately avoids huggingface_hub.login()), the user was
therefore prompted for it again on every single upload.

Cache the token in memory for the duration of the loop so that a subsequent
upload reuses the one that was just entered, without ever persisting it to
disk or exposing it through the settings.

Fixes p-e-w#142
Repository owner deleted a comment from seydou1234coulibaly-web Oct 13, 2026

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upload_token is assigned before huggingface_hub.whoami(token) validates it. If the user mistypes a token, whoami raises and the outer action-loop handler prints the error, but the invalid token remains cached. Selecting Upload again then takes upload_token or huggingface_hub.get_token() and never prompts for a replacement, so the user cannot recover within the session. Could the cache be updated only after a successful whoami (and cleared on an authentication failure)?

@Yi-111-a

Yi-111-a commented Nov 3, 2026

Copy link
Copy Markdown
Author

Fixed in e240b4e (e240b4e).

The token is now only remembered after huggingface_hub.whoami(token) succeeds, and on an authentication failure the cache is cleared (upload_token = None) before the error propagates to the action-loop handler, so the next "Upload the model to Hugging Face" attempt re-prompts for a replacement token instead of silently reusing the invalid one.

@seydou1234coulibaly-web

Copy link
Copy Markdown

Correction : mémoriser le jeton Hugging Face entre les chargements

Résumé

L' uploadaction de la boucle d'actions interactives obtenait le jeton d'accès Hugging Face à huggingface_hub.get_token()chaque itération, et ne revenait à une invite interactive que si celle-ci ne renvoyait rien.

Comme Heretic évite délibérément huggingface_hub.login()d'écrire le jeton sur le disque (ce qui est indésirable sur les serveurs GPU loués ou partagés), la get_token()fonction renvoie généralement une valeur nulle, Nonesauf si l'utilisateur a défini un HF_TOKENjeton ou un fichier de jeton. Cela signifie qu'un jeton saisi à l'invite n'a été utilisé que pour un seul chargement : le chargement d'un deuxième modèle dans la même session nécessite une nouvelle saisie du jeton.

Mettez le jeton en cache dans une variable locale pendant toute la durée de la boucle d'actions, afin qu'un deuxième téléchargement puisse le réutiliser.

Pourquoi cela n'affaiblit pas la posture de sécurité

Le cache est une simple variable locale dans run():

  • Il n'est jamais écrit sur disque.
  • Il n'est jamais stocké dans settings, il ne peut donc pas être exporté dans une configuration enregistrée.
  • Elle est rejetée dès que la boucle d'action se termine, et
  • huggingface_hub.get_token()est toujours consulté en premier lors de chaque téléchargement, de sorte qu'un jeton configuré via HF_TOKENou le fichier de jeton continue de primer sur tout élément mis en cache.

En rapport

Corrige le problème n° 142 .

Excellente analyse et merci pour cette PR.
La mise en cache du jeton dans une variable locale résout parfaitement le problème de friction lors du chargement de plusieurs modèles, tout en respectant notre contrainte de non-persistance sur le disque. Les arguments de sécurité sont clairs et validés (priorité à HF_TOKEN, pas d'écriture disque, cycle de vie limité à la fonction run()).
C'est validé de mon côté pour corriger l'issue #142. Beau travail !

Comment thread src/heretic/main.py
if settings.upload_repo_private is None
else (
"Private"
if settings.upload_repo_private

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All starter

The previous change correctly validated before caching on failure, but
dropped the successful-path assignment, so a valid token was never
remembered across uploads in the same session.
@Yi-111-a

Yi-111-a commented Nov 4, 2026

Copy link
Copy Markdown
Author

Follow-up at c4f37da5f0f04c4bff666dee2959140b762e853a: the earlier change validated with whoami before caching on failure, but dropped the successful-path upload_token = token. A valid token is now assigned only after whoami succeeds (and still cleared on auth failure), so mistypes do not stick and good tokens are still remembered for the session.

@agammann

Copy link
Copy Markdown

Thanks for the follow-up. At c4f37da, a manually entered token is cached only after whoami succeeds, and the cache is cleared if validation fails, addressing the mistyped prompted-token case I raised. I reviewed the diff but did not run it locally.

One documentation detail: the description says get_token() takes precedence on every upload, but upload_token or huggingface_hub.get_token() gives a cached token precedence. Also, if get_token() returns an invalid configured token, clearing the cache alone will cause the next attempt to use that configured token again. Could the intended precedence and recovery behavior be clarified, with tests for prompted and configured-token cases? AI-assisted review.

@Yi-111-a

Copy link
Copy Markdown
Author

Thanks, you were right that the code and the description disagreed. Fixed in 8dbbb69 by making the code match the documented intent:

  • The token is now resolved as huggingface_hub.get_token() or upload_token, so a token from HF_TOKEN or the token file always takes precedence over a cached one.
  • Only a token entered at the prompt is cached, and only after whoami succeeds. On a validation failure the cache is cleared, so the next attempt prompts again.
  • A configured token that fails validation is used again on the next attempt, as it was before this PR. The fix there is to correct HF_TOKEN or the token file, since a configured token shouldn't be silently overridden by a prompt.

I've updated the description with a short precedence and recovery section. On tests: this logic sits inline in the interactive action loop in run(), and the repo's tests/ suite covers model reproducibility rather than the CLI loop, so a unit test would mean pulling the token resolution out into a helper first. I'm happy to do that if the maintainer wants it, but I kept this PR minimal for now. ruff check and ruff format --check pass.

@agammann

Copy link
Copy Markdown

Thanks for clarifying the configured-token precedence. One follow-up: at 8dbbb69, the except Exception also clears an already validated prompted token when whoami fails for a transient network reason, so the next upload requires re-entry even though the token may still be valid. I reproduced that with a synthetic ConnectionError against the validation block in an offline, isolated probe; I did not exercise the full interactive loop. Could authentication failures clear the cached token while transient errors leave it intact? AI-assisted testing.

Only an authentication failure (HTTP 401) clears the remembered token.
Network errors, rate limits and server errors leave it in place.
@Yi-111-a

Copy link
Copy Markdown
Author

Good catch, thanks. Fixed in 43d285f: the handler now catches only HfHubHTTPError and clears the cached token only when the Hub returns 401 (invalid token). Network errors, 429 rate limits and 5xx responses leave the cached token in place, so retrying the upload reuses it. Either way the error still goes to the action-loop handler as before.

I checked the three cases (401, 503, httpx.ConnectError) against the handler logic in a small isolated probe, not the full interactive loop. I also updated the precedence/recovery section of the PR description.

@agammann

Copy link
Copy Markdown

Thanks for the update. I rechecked 43d285f using an isolated offline probe of the actual validation block with stubbed Hub exceptions: 401 clears the cached token, while 429, 503, a response-less HTTP error, and a synthetic connection error leave it intact. That addresses the transient-error case I raised. This was AI-assisted testing of the block, not the full interactive upload loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hugging Face token not preserved between uploads when entered on prompt

4 participants