Repository navigation
Make hosted Gradio sessions identity-aware, release hand-off sessions and fix non-ASCII downloads #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AtharvaTilewale
wants to merge
4
commits into
RosettaCommons:main
Choose a base branch
from
AtharvaTilewale:fix/hosted-gradio-sessions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Make hosted Gradio sessions identity-aware, release hand-off sessions and fix non-ASCII downloads #21
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
37e0834
Fix CIF download for non-ASCII names and bound the interaction cache
AtharvaTilewale 444ff45
Make hosted Gradio sessions identity-aware and restart-safe
AtharvaTilewale 46bb37c
Bound the Gradio result registry and release sessions on all paths
AtharvaTilewale 392bc03
Release result sessions of stateless generate calls too
AtharvaTilewale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make hosted Gradio sessions identity-aware and restart-safe
- Forward the proxy-authenticated identity header on in-process Studio hand-off calls. In ANNOTATION_STUDIO_IDENTITY_HEADER mode every hand-off was a 401, so generation could not run, and results are now owned by the user whose iframe opens them. - Release hand-off sessions: generate_cif deletes its temporary input session, and a browser tab's earlier designs are deleted when it generates again (the session it generated from is kept). Unreleased sessions filled the 64-slot store and evicted other users' live sessions. - Persist the document revision in session snapshots so a client's stale expected_revision cannot match a different state after a restart. Undo history is still memory-only.
- Loading branch information
commit 444ff456cb26940c455f422a5abf4b651e0cef0e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """Regressions for hosted-mode session handling: identity hand-off, session release, restart safety.""" | ||
|
|
||
| import asyncio | ||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| from annotation_studio import main as studio_main | ||
| from annotation_studio.main import DocumentStore, app | ||
| from atomworks_studio_editing.document import StudioDocument | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| FIXTURE = Path(__file__).parent / "fixtures" / "inverse_folding_2kl8.cif" | ||
| HEADER = "X-Forwarded-User" | ||
|
|
||
|
|
||
| def test_persistent_store_keeps_the_revision_across_reloads(tmp_path) -> None: | ||
| store = DocumentStore(tmp_path) | ||
| session_id = store.add(StudioDocument.from_cif(FIXTURE.read_text(), "input")) | ||
| for value in (1, 0): | ||
| store.call(session_id, lambda document, value=value: document.set_charge([0], value=value), persist=True) | ||
| assert store.get(session_id).revision == 2 | ||
|
|
||
| reloaded = DocumentStore(tmp_path).get(session_id) | ||
| # Restarting at 0 would let a client's stale expected_revision match a different document. | ||
| assert reloaded.revision == 2 | ||
| assert reloaded.to_cif() == store.get(session_id).to_cif() | ||
|
|
||
|
|
||
| class _SamplesProvider: | ||
| """Minimal GenerationProvider: the hand-off is under test, not a model's controls.""" | ||
|
|
||
| title = "Hand-off test" | ||
| default_example = None | ||
| warning_checks = frozenset({"missing_atoms"}) # as the RFD4 provider: incomplete residues warn | ||
|
|
||
| def controls(self): | ||
| import gradio as gr | ||
|
|
||
| return {"samples": gr.Number(value=1, precision=0)} | ||
|
|
||
| def validate(self, options): | ||
| return {"samples": int(options.get("samples") or 1)} | ||
|
|
||
| def validate_input(self, snapshot, options): | ||
| return None | ||
|
|
||
| def example_options(self, example): | ||
| return {} | ||
|
|
||
|
|
||
| def _demo_callbacks(): | ||
| pytest.importorskip("gradio") | ||
| from annotation_studio.gradio import create_demo | ||
|
|
||
| def generate(cif, options): | ||
| return [cif] * options["samples"] | ||
|
|
||
| demo = create_demo(app, provider=_SamplesProvider(), generate_fn=generate) | ||
| return demo, {event.fn.__name__: event.fn for event in demo.fns.values() if event.fn} | ||
|
|
||
|
|
||
| def test_gradio_hand_off_works_in_identity_mode_and_releases_sessions(monkeypatch) -> None: | ||
| monkeypatch.setattr(studio_main, "IDENTITY_HEADER", HEADER) | ||
| demo, callbacks = _demo_callbacks() | ||
| alice = SimpleNamespace(headers={HEADER: "alice"}, session_hash="alice-tab") | ||
| client = TestClient(app) | ||
|
|
||
| def status(session_id: str, user: str) -> int: | ||
| return client.get(f"/api/v1/sessions/{session_id}", headers={HEADER: user}).status_code | ||
|
|
||
| async def run(): | ||
| cif = FIXTURE.read_text() | ||
| sessions_before = len(studio_main.store._documents) | ||
| # Without forwarding the proxy identity, every in-process hand-off call was a 401. | ||
| direct = await callbacks["generate_cif"](cif, {"samples": 1}, alice) | ||
| assert len(direct["designs"]) == 1 | ||
| # The temporary API session is released instead of occupying one of the 64 store slots. | ||
| assert len(studio_main.store._documents) == sessions_before | ||
|
|
||
| source = client.post("/api/v1/sessions", files={"file": ("input.cif", cif.encode())}, headers={HEADER: "alice"}) | ||
| source_id = source.json()["session_id"] | ||
| values = (2,) | ||
| first = await callbacks["generate"](source_id, *values, request=alice) | ||
| first_results = [key for key in first[2] if key != source_id] | ||
| assert len(first_results) == 2 | ||
| # Results belong to the authenticated user, so the iframe can open them and others cannot. | ||
| assert all(status(key, "alice") == 200 for key in first_results) | ||
| assert all(status(key, "bob") == 404 for key in first_results) | ||
|
|
||
| second = await callbacks["generate"](source_id, *values, request=alice) | ||
| assert all(status(key, "alice") == 404 for key in first_results), "earlier designs must be released" | ||
| assert all(status(key, "alice") == 200 for key in second[2]) | ||
| assert status(source_id, "alice") == 200, "the input session must survive" | ||
|
|
||
| try: | ||
| asyncio.run(run()) | ||
| finally: | ||
| demo.close() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.