Skip to content

feat: support redirection#293

Merged
drbh merged 4 commits intomainfrom
enable-redirection
Mar 2, 2026
Merged

feat: support redirection#293
drbh merged 4 commits intomainfrom
enable-redirection

Conversation

@drbh
Copy link
Collaborator

@drbh drbh commented Feb 19, 2026

This PR adds a concept of redirection via a REDIRECT.toml file on the main branch.

The kernel tool will now first check for a redirection file, read and apply the redirection if it exists. The core use case is enable a way to manually redirect users from old repos to newer kernel repos with a custom message.

Note: redirections are single hop only, its not possible to redirect to another redirect

example file

destination = "kernels-community/activation"
message = "This kernel has been moved to kernels-community/activation. Please update your dependencies to use the new location."

this example file is currently hosted https://huggingface.co/drbh/test-kernel-redirect

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@danieldk danieldk left a comment

Choose a reason for hiding this comment

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

Really useful functionality! I would make it more general though, because as it is now, we would have to deprecate this again if we add archived/deprecation in the future.


from kernels.compat import tomllib

REDIRECT_FILENAME = "REDIRECT.toml"
Copy link
Member

Choose a reason for hiding this comment

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

I think we should have a more generic name than that, so that we can use the same file for multiple signaling purposes.

Maybe kernel-status.toml?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point, updated in latest and renamed the file to status rather than redirect

Comment on lines +13 to +16
@dataclass
class RedirectInfo:
destination: str
message: str
Copy link
Member

Choose a reason for hiding this comment

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

Redirect should be one of a union (so that we can also have e.g. deprecated/unmaintained in the future).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this makes sense, I've updated in latest for the following

@dataclass
class Redirect:
    kind: str  # must be "redirect"
    destination: str
    revision: str

    ...

message: str


def parse_redirect_file(content: str) -> RedirectInfo:
Copy link
Member

Choose a reason for hiding this comment

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

I think we should have this similar to https://github.com/huggingface/kernels/blob/main/kernels/src/kernels/metadata.py where these are methods.

Maybe we should consider switching to pydantic for these deserialization/ validation purposes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point, I've updated to follow a pattern like metadata, and added a kind key so we know which type to deserialize the file to.

pydantic could be nice to add, however i've left that out of this PR and can be followed up in the future to clean things up

Comment on lines +48 to +50
RED_COLOR = "\033[91m"
RESET_COLOR = "\033[0m"
msg = f"\n{RED_COLOR}{msg}{RESET_COLOR}"
Copy link
Member

Choose a reason for hiding this comment

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

Do these work well in Windows shells, I vaguely remember ASCII sequences giving issues and you need a library to make them work on *nix and Windows.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm I did a bit of research and I think they should work on any windows version >=10 however I've removed for now to simplify

Comment on lines +54 to +56
if "@" in redirect.destination:
return redirect.destination.rsplit("@", 1)
return redirect.destination, "main"
Copy link
Member

Choose a reason for hiding this comment

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

@ in the string data seems very ad-hoc if we have a structured data format like TOML?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point, revision is now explicitly on the Redirect class now

backend: str | None = None,
variant_locks: dict[str, VariantLock] | None = None,
user_agent: str | dict | None = None,
follow_redirects: bool = True,
Copy link
Member

Choose a reason for hiding this comment

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

I would leave this out. We already have plenty of arguments and we are probably going to add framework soon. I think we should just go for doing the right thing by default. If a kernel developer sets a repo to redirect, they probably really want to redirect.

Copy link
Collaborator Author

@drbh drbh Feb 25, 2026

Choose a reason for hiding this comment

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

agreed, updated in latest

@dataclass
class RedirectInfo:
destination: str
message: str
Copy link
Member

Choose a reason for hiding this comment

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

I would leave out a message for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated in latest

Copy link
Member

@danieldk danieldk left a comment

Choose a reason for hiding this comment

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

Looks great!

@drbh drbh merged commit 74a19b0 into main Mar 2, 2026
48 of 54 checks passed
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.

3 participants