Skip to content

Make hosted Gradio sessions identity-aware, release hand-off sessions and fix non-ASCII downloads - #21

Open
AtharvaTilewale wants to merge 4 commits into
RosettaCommons:mainfrom
AtharvaTilewale:fix/hosted-gradio-sessions
Open

AtharvaTilewale wants to merge 4 commits into
RosettaCommons:mainfrom
AtharvaTilewale:fix/hosted-gradio-sessions

Conversation

@AtharvaTilewale

Copy link
Copy Markdown
Contributor

Builds on #20. The first commit here is #20 (CI fix); please review the commits after it. GitHub will drop it from this diff once #20 merges.

What this fixes

  1. Gradio host fails in identity mode. With ANNOTATION_STUDIO_IDENTITY_HEADER set, every in-process StudioHandoff call returned 401, so Generate design and the generate_cif API could not run. The ASGI transport never forwarded the user's identity. The hand-off now forwards the configured header from the incoming gr.Request. That request came through the same trusted proxy, so the header is as trustworthy as the browser's own Studio requests. Results are owned by that user, so the iframe can open them and other users get 404.
  2. Hand-off sessions were never released. Each generation opened 1 + N sessions, and generate_cif opened one more, all left in the 64-slot store. The store evicts by global LRU, so after 64 opens another user's live session returned 404 and their unsaved edits were gone. Now generate_cif deletes its temporary session, and a tab's earlier designs are deleted when it generates again. The session it generated from is kept, even if it is itself an earlier design.
  3. Persistent sessions lost their revision. With ANNOTATION_STUDIO_SESSION_DIR, a reload restarted revision at 0. A client's stale expected_revision, such as a target-discard confirmation, could then match a different document state. The revision is now stored in the snapshot metadata. Old snapshots without it still load. Undo history stays memory-only, because persisting it would cost one CIF serialization per entry on every edit (60–100 ms each on the bundled examples).
  4. CIF download returned 500 for non-Latin-1 names, e.g. a Cyrillic file name (UnicodeEncodeError: 'latin-1'). The handler now emits an RFC 6266 header: a sanitized ASCII filename plus filename*=UTF-8''….
  5. The interaction-analysis cache was unbounded within a revision. It is now an 8-entry LRU.

Validation

  • unit suite: 389 passed, 19 skipped, 0 failed. ruff is clean.
  • New tests/test_backend_review_fixes.py fails on Fix CI: open rebuilt examples as Annotated CIF and protonate them #20's code and passes here. It uses a minimal stand-in GenerationProvider, so it does not depend on the RFD4 provider's controls. test_api.py gains a non-ASCII download test.
  • Checked against live uvicorn: the Cyrillic-name download returns 200 with the header above, and ASCII names are unchanged.

Heads-up: overlap with codex/generation-progress-layout

That unmerged branch edits the same lines in gradio.py. Merging the two produces 3 small conflicts, each resolved by keeping both sides (the new close() alongside your frame(..., example=), and headers alongside your validate_input). With that resolution the full suite passes: 403 passed, 0 failed. Happy to rebase this onto it once it lands.

Not addressed

Store eviction is still global LRU with no per-owner quota. That is a policy choice I left for you.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 08:03

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Copilot AI 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.

🟡 Changes recommended

The Gradio session bookkeeping and cleanup paths still have unresolved lifecycle and capacity issues.

3 open issues

Review effort: Lite

AtharvaTilewale added a commit to AtharvaTilewale/atomworks-studio that referenced this pull request Oct 22, 2026
Addresses review on RosettaCommons#21:
- results_by_client is now a bounded OrderedDict (MAX_TRACKED_CLIENTS); evicting
  the oldest tab releases its tracked sessions, so a public host cannot grow the
  registry without bound.
- A generation that fails partway (e.g. a later design fails to open) now closes
  the result sessions it already opened, instead of leaking them.
- When a tab generates from one of its own earlier designs, that design is kept
  in the registry so a later generation from a different source still releases
  it, rather than leaving it until global eviction.

Three regression tests, each failing before the change and passing after.
@AtharvaTilewale

Copy link
Copy Markdown
Contributor Author

Thanks — all three points addressed in the latest commit:

  • Unbounded results_by_client: it is now a bounded OrderedDict (MAX_TRACKED_CLIENTS); evicting the oldest tab releases its tracked sessions, so a public host can't grow the registry without bound. (gr.Blocks.unload can't identify the client in this Gradio version, so I bounded the structure rather than relying on a disconnect hook.)
  • Leaked sessions on partial failure: a generation that fails partway now closes the result sessions it already opened.
  • Retained source lost from the registry: when a tab generates from one of its own earlier designs, that design stays tracked so a later generation from a different source still releases it.

Three regression tests, each failing before the change and passing after.

Copilot AI 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.

🟡 Changes recommended

Stateless generation can leave sessions unreleased, and the terminal-OXT exemption is broader than intended.

1 open issue
3 resolved since last review

Review effort: Lite

@AtharvaTilewale
AtharvaTilewale force-pushed the fix/hosted-gradio-sessions branch from 49782f3 to 6bad979 Compare October 22, 2026 10:17
AtharvaTilewale added a commit to AtharvaTilewale/atomworks-studio that referenced this pull request Oct 22, 2026
Addresses review on RosettaCommons#21:
- results_by_client is now a bounded OrderedDict (MAX_TRACKED_CLIENTS); evicting
  the oldest tab releases its tracked sessions, so a public host cannot grow the
  registry without bound.
- A generation that fails partway (e.g. a later design fails to open) now closes
  the result sessions it already opened, instead of leaking them.
- When a tab generates from one of its own earlier designs, that design is kept
  in the registry so a later generation from a different source still releases
  it, rather than leaving it until global eviction.

Three regression tests, each failing before the change and passing after.
AtharvaTilewale added a commit to AtharvaTilewale/atomworks-studio that referenced this pull request Oct 22, 2026
Addresses the second review on RosettaCommons#21: a call to the exposed generate API without
a Gradio session_hash skipped the registry, so its result sessions stayed in
the store until Studio's global eviction. Such a call now gets its own key in
the same bounded registry, so its results are released in order when the
registry evicts it. The caller still receives the session ids it was returned.
- GET /sessions/{id}/cif returned 500 (UnicodeEncodeError: 'latin-1') when
  the uploaded file name was not Latin-1, e.g. a Cyrillic name. It now emits
  an RFC 6266 header: a sanitized ASCII filename plus filename*=UTF-8''.
- The per-document interaction-analysis cache was unbounded within a revision
  (keys include full selection tuples). It is now an 8-entry LRU.
- 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.
Addresses review on RosettaCommons#21:
- results_by_client is now a bounded OrderedDict (MAX_TRACKED_CLIENTS); evicting
  the oldest tab releases its tracked sessions, so a public host cannot grow the
  registry without bound.
- A generation that fails partway (e.g. a later design fails to open) now closes
  the result sessions it already opened, instead of leaking them.
- When a tab generates from one of its own earlier designs, that design is kept
  in the registry so a later generation from a different source still releases
  it, rather than leaving it until global eviction.

Three regression tests, each failing before the change and passing after.
Addresses the second review on RosettaCommons#21: a call to the exposed generate API without
a Gradio session_hash skipped the registry, so its result sessions stayed in
the store until Studio's global eviction. Such a call now gets its own key in
the same bounded registry, so its results are released in order when the
registry evicts it. The caller still receives the session ids it was returned.
@AtharvaTilewale
AtharvaTilewale force-pushed the fix/hosted-gradio-sessions branch from 6bad979 to 392bc03 Compare November 21, 2026 11:47
@AtharvaTilewale

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (d7b78e4, 0.0.12). The earlier red CI was GitHub expiring the Oct 22 runs after 30 days awaiting approval; no job had run. CI on the rebased branch is green. The only failing job is the non-blocking integration acceptance suite, which fails the same single check on main ("a plain mmCIF opens as an entry does"), 36/37 in both.

Conflicts resolved in document.py, main.py and gradio.py, keeping both sides: the persisted snapshot now carries revision alongside the new design state, and the hand-off fixes sit next to the new refold/card code.

One gap to flag: the new design-card mode (_create_card_demo) calls handoff.open/export without the identity header, so in ANNOTATION_STUDIO_IDENTITY_HEADER mode it would hit the same 401 this PR fixes for the classic UI. I've left it out because that code landed after this PR. Happy to extend the fix here, or in a follow-up, whichever you prefer.

This branch has not been deployed

No deployments
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.

2 participants