Skip to content

[BREAKING] Python: Ensure session isolation for FHA invocation impl#7158

Open
TaoChenOSU wants to merge 2 commits into
mainfrom
fix/ensure-session-isolation-for-fha-invocation
Open

[BREAKING] Python: Ensure session isolation for FHA invocation impl#7158
TaoChenOSU wants to merge 2 commits into
mainfrom
fix/ensure-session-isolation-for-fha-invocation

Conversation

@TaoChenOSU

@TaoChenOSU TaoChenOSU commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Motivation & Context

Currently, the light-weight Foundry Hosting Agent (FHA) invocation integration stores live AgentSession objects in a process-wide dictionary keyed only by the session_id, which could potentially allow cross-user invocation.

Description & Review Guide

This PR ensures that the sessions are key by the user id injected by the platform when the agent is hosted in FHA., eliminating the possibility that one user can interfere with another user's session even if the session id is exposed.

  • What are the major changes?
    • Agent sessions are now keyed by partition keys that are made up of the platform session id and user id, when hosted.
    • When not hosted, session will be isolated like before because user id is not available.
  • What is the impact of these changes?
    • Eliminates cross user interference
  • What do you want reviewers to focus on?
    • The derivation of the partition key

This is marked as breaking because the return type of the invocation handler has been changed.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

@TaoChenOSU TaoChenOSU self-assigned this Jul 16, 2026
Copilot AI review requested due to automatic review settings July 16, 2026 17:42
@TaoChenOSU TaoChenOSU added python Usage: [Issues, PRs], Target: Python foundry Usage: [Issues, PRs], Target: all Foundry integrations hosting Usage: [Issues, PRs], Target: all hosting related solutions labels Jul 16, 2026
@github-actions github-actions Bot changed the title Ensure session isolation for FHA invocation impl Python: Ensure session isolation for FHA invocation impl Jul 16, 2026
@giles17 giles17 added the documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs label Jul 16, 2026
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/foundry_hosting/agent_framework_foundry_hosting
   _invocations.py450100% 
TOTAL44469521288% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8995 33 💤 0 ❌ 0 🔥 2m 22s ⏱️

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Python Foundry Hosted Agents Invocations hosting implementation to derive session partitioning from the Foundry request context (session/user IDs) to improve session isolation in hosted scenarios, and adds tests and sample documentation updates around invocations + streaming.

Changes:

  • Add _partition_key() to compute a session cache key using Foundry request context (and fail fast when hosted protocol context is missing).
  • Update _handle_invoke() to key the in-memory session cache by the partition key rather than request.state.session_id.
  • Add unit + integration tests for invocations, and update the basic invocations sample README with a streaming curl example.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
python/samples/04-hosting/foundry-hosted-agents/invocations/01_basic/README.md Documents a streaming invocation example alongside the existing non-streaming example.
python/packages/foundry_hosting/agent_framework_foundry_hosting/_invocations.py Introduces partition-key session isolation based on Foundry request context; updates invocation handler behavior.
python/packages/foundry_hosting/tests/test_invocations.py Adds unit tests covering partition-key behavior and invocation handler behavior (streaming/non-streaming).
python/packages/foundry_hosting/tests/test_invocations_int.py Adds integration tests exercising the ASGI pipeline against a real Foundry endpoint (skipped unless env vars are provided).

Comment thread python/packages/foundry_hosting/tests/test_invocations.py Outdated
Comment thread python/packages/foundry_hosting/tests/test_invocations_int.py

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 5 | Confidence: 84% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by TaoChenOSU's agents

@TaoChenOSU TaoChenOSU changed the title Python: Ensure session isolation for FHA invocation impl [BREAKING] Python: Ensure session isolation for FHA invocation impl Jul 16, 2026
@giles17 giles17 added the breaking change Usage: [PRs], Target: all PRs that introduce changes that are not backward compatible label Jul 16, 2026
@TaoChenOSU
TaoChenOSU marked this pull request as ready for review July 16, 2026 18:46

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 5 | Confidence: 88% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by TaoChenOSU's agents

context = get_request_context()

# Fail fast if the service is on protocol v1.0.0
if self.config.is_hosted and context.call_id is None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

how does this determine the version, is call_id new for protocol v2?

raise RuntimeError(
"The hosted environment is running on protocol 1.0.0, but the agent requires protocol 2.0.0. "
"Please upgrade your agent protocol to 2.0.0 in `agent.manifest.yaml` or `agent.yaml`, or "
"downgrade the `agent-framework-foundry-hosting` package to `1.0.0a260625` or before to use 1.0.0."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is this even an option?

"The hosted environment is missing session_id or user_id in the request context. "
"Please ensure that the request is coming from a valid Foundry platform service."
)
return f"{context.session_id}:{context.user_id}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is the format for this agreed upon?

return StreamingResponse(content=error, status_code=400)
return Response(content=error, status_code=400)

session = self._sessions.setdefault(session_id, AgentSession(session_id=session_id))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what happens when the server stops? are these preserved?

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

Labels

breaking change Usage: [PRs], Target: all PRs that introduce changes that are not backward compatible documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs foundry Usage: [Issues, PRs], Target: all Foundry integrations hosting Usage: [Issues, PRs], Target: all hosting related solutions python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants