Skip to content

Restore durable Responses request context - #48605

Merged
Shiva S (Shivakishore14) merged 3 commits into
mainfrom
users/spasumarthy/fix-responses-durable-request-context
Aug 18, 2026
Merged

Shiva S (Shivakishore14) merged 3 commits into
mainfrom
users/spasumarthy/fix-responses-durable-request-context

Conversation

@shanmukha1200

Copy link
Copy Markdown
Member

Summary

  • rebind reconstructed user, call, and session identity into get_request_context() while durable Responses handlers execute
  • cover foreground durable, streaming durable, and recovered-task execution
  • restore parity between the explicit ResponseContext.platform_context and the ambient AgentServer request context

This fixes the root cause observed in microsoft-foundry/foundry-samples-pr#910, where the hosted request contained identity and durable storage received it, but developer handlers saw None through get_request_context().

Validation

  • py -3.12 -m pytest tests\unit tests\contract tests\e2e\test_recovery_reconstruction.py -q (1156 passed, 3 skipped)
  • azpysdk pyright .
  • Black check and git diff --check

Rebind reconstructed user, call, and session identity while durable response handlers execute, including recovered tasks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: bdc32ee9-9e8f-4a1c-ba27-bdfa318afa1e
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
9 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
9 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

Restores ambient request identity during durable Responses handler execution.

Changes:

  • Rebinds user, call, and session identity for durable handlers.
  • Covers synchronous, streaming, and recovered execution.
  • Documents the fix in the changelog.

Reviewed changes

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

File Description
CHANGELOG.md Records the request-context fix.
_orchestrator.py Adds and applies durable context rebinding.
test_streaming_request_context.py Tests stored synchronous and streaming handlers.
test_recovery_reconstruction.py Tests reconstructed identity and ambient context.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 18, 2026 03:40

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

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

Suppressed comments (1)

sdk/agentserver/azure-ai-agentserver-responses/azure/ai/agentserver/responses/hosting/_orchestrator.py:110

  • _iter_with_winddown resumes this iterator through asyncio.wait_for(aiter.__anext__(), ...) after cancellation (_orchestrator.py:206), which executes __anext__ in a copied task context. If the handler then completes or times out, this finally tries to reset a token created in the parent context and raises ValueError; the cancellation path can be recorded as a handler failure and the parent task retains the bound identity. Bind and reset around each individual __anext__ call so every token is consumed in the same task/context that created it.
    finally:
        if token is not None:
            reset_request_context(token)

@github-actions

Copy link
Copy Markdown
Contributor
[Pilot] PR Pipeline Failure Analysis

A CI pipeline failed on this pull request. Here is an automated analysis of what went wrong and how to get the build green.

What failed

Two jobs failed in build 6712729:

  • Build Analyze (validation): Pylint exited with code 16 for azure-ai-agentserver-responses. Three docstring violations were reported in azure/ai/agentserver/responses/hosting/_orchestrator.py at line 87 (function _iter_handler_with_request_context):

    • C4739 – missing params in docstring: create_fn, parsed, context, cancellation_signal, agent_session_id
    • C4741 – missing return description in docstring
    • C4742 – missing return type in docstring
  • Build Test Ubuntu2404_314 (test): One test failed:

    • tests.e2e.resilience_contract.test_row_1_path_a.test_row_1_path_a[stream=False]

Recommended next steps

  • Fix the pylint docstring violations in sdk/agentserver/azure-ai-agentserver-responses/azure/ai/agentserver/responses/hosting/_orchestrator.py, function _iter_handler_with_request_context (line 87): add :param entries for create_fn, parsed, context, cancellation_signal, and agent_session_id, and add :return: / :rtype: sections. See the [Azure SDK docstring guidelines]((azure.github.io/redacted)
  • Investigate test_row_1_path_a[stream=False] – review the job log to determine whether this is a real regression or a flaky test. If flaky, re-run the pipeline; if a real failure, fix the underlying code.
  • See the CI troubleshooting guide: https://aka.ms/ci-fix
  • Push new commits to address the failures; this comment updates automatically on the next failing run.
Raw pipeline analysis (azsdk ci analyze)
Analyzing pipeline https://github.com/Azure/azure-sdk-for-python/pull/48605...
Build: 6712729 Project: public
PipelineUrl: https://dev.azure.com/azure-sdk/public/_build/results?buildId=6712729

Failed Tests:
  - tests.e2e.resilience_contract.test_row_1_path_a.test_row_1_path_a[stream=False]
    (platform: Ubuntu2404_314)

Failed Tasks (pylint - azure-ai-agentserver-responses):
  azure/ai/agentserver/responses/hosting/_orchestrator.py:87:
    [C4739] docstring-missing-param: create_fn, parsed, context, cancellation_signal, agent_session_id
    [C4741] docstring-missing-return
    [C4742] docstring-missing-rtype
  pylint check completed with exit code 16 (FAIL)

Failing checks on the pull request:
  python - pullrequest [FAILURE]
  python - pullrequest (Build Analyze) [FAILURE]
  python - pullrequest (Build Test Ubuntu2404_314) [FAILURE]

Copilot detected the failing pipeline and generated the analysis above. To have it attempt a fix automatically, reply with @copilot please fix the failing pipeline on this PR.

Generated by Pipeline Analysis - Next Steps · 36.3 AIC · ⌖ 8.84 AIC · ⊞ 6.6K · ◷

Bind ambient context independently for each handler iterator advancement and preserve empty platform identity values.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: bdc32ee9-9e8f-4a1c-ba27-bdfa318afa1e
Copilot AI review requested due to automatic review settings August 18, 2026 04:23

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

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

Suppressed comments (2)

sdk/agentserver/azure-ai-agentserver-responses/tests/contract/test_streaming_request_context.py:132

  • Constructing TestClient without entering it skips lifespan startup, and the orchestrator deliberately degrades such test clients to its non-durable fallback. This streaming test therefore does not verify ambient context inside a durable streaming task. Enter the client context before issuing the request so the resilient task manager is initialized.
        client = _build_capturing_client(captured, resilient=True, storage_dir=tmp_path / "stream")

        with client.stream(
            "POST",
            "/responses",

sdk/agentserver/azure-ai-agentserver-responses/tests/contract/test_streaming_request_context.py:124

  • This test never enters TestClient as a context manager, so Starlette's lifespan does not run. _start_resilient_background explicitly handles this case by catching TaskManagerNotInitialized and using the non-durable in-process fallback (_orchestrator.py:3971-3979, 4054-4070). Consequently, this does not cover the durable foreground execution named by the PR and can pass via inherited request context. Start the client lifespan so the request actually runs through the resilient task subsystem.

This issue also appears on line 128 of the same file.

    def test_stored_sync_handler_sees_call_id_and_user_id(self, tmp_path: Path) -> None:
        captured: dict[str, Any] = {}
        client = _build_capturing_client(captured, resilient=True, storage_dir=tmp_path / "sync")

@Shivakishore14
Shiva S (Shivakishore14) merged commit c24957f into main Aug 18, 2026
22 checks passed
@Shivakishore14
Shiva S (Shivakishore14) deleted the users/spasumarthy/fix-responses-durable-request-context branch August 18, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hosted Agents sdk/agentserver/*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants