Skip to content

OAuth continuation fix#468

Closed
rodrigobr-msft wants to merge 14 commits into
mainfrom
users/robrandao/oauth-continuation
Closed

OAuth continuation fix#468
rodrigobr-msft wants to merge 14 commits into
mainfrom
users/robrandao/oauth-continuation

Conversation

@rodrigobr-msft

Copy link
Copy Markdown
Contributor

This pull request introduces improvements to how OAuth continuation activities are handled and queued, adds better logging for user authorization flows, and refines activity logging in transcripts. The main changes focus on making authentication flows more robust and observable, especially in scenarios involving asynchronous operations and error handling.

OAuth Continuation Handling and Robustness:

  • Refactored the handling of OAuth continuation activities in agent_application.py to use a new _queue_auth_continuation method. This now queues the continuation asynchronously, improving reliability and decoupling the flow from the current turn context. Errors during replay are now logged via a dedicated callback. [1] [2]
  • Added new methods _queue_auth_continuation, _replay_auth_continuation, and _log_auth_continuation_failure to encapsulate the queuing, replay, and error logging of authentication continuations. This also includes proper handling for adapters with claims support.
  • Imported asyncio to enable asynchronous task creation for continuation activities.

User Authorization Flow Logging:

  • Improved logging in the OAuth user authorization handler to log when the sign-in flow completes successfully, and sends an appropriate invoke response to the client.

Transcript Logging Improvements:

  • Updated the transcript logger to exclude unset fields when serializing activities to JSON, reducing unnecessary data in logs.

Copilot AI review requested due to automatic review settings July 15, 2026 17:00

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 refactors how OAuth continuation activities are replayed by queueing the replay asynchronously, improves logging and handling of OAuth sign-in completion responses, and reduces transcript log noise by omitting unset fields during JSON serialization.

Changes:

  • Queue OAuth continuation replays via a new _queue_auth_continuation() / _replay_auth_continuation() flow with failure logging callback.
  • Send an explicit invoke_response when the OAuth sign-in flow completes successfully.
  • Serialize transcript activities with exclude_unset=True (currently applied to console transcript logging).

Reviewed changes

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

File Description
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py Adds async queuing/replay for OAuth continuation activities plus failure logging.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/oauth/_handlers/_user_authorization.py Adds a success log + invoke response when the sign-in flow completes.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/storage/transcript_logger.py Reduces console transcript JSON verbosity by excluding unset fields.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings July 15, 2026 17:05

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 3 out of 3 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings July 15, 2026 17:24

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 6 out of 6 changed files in this pull request and generated 4 comments.

Copilot AI review requested due to automatic review settings July 15, 2026 17:31
@rodrigobr-msft
rodrigobr-msft marked this pull request as ready for review July 15, 2026 17:34
@rodrigobr-msft
rodrigobr-msft requested a review from a team as a code owner July 15, 2026 17:34

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 5 out of 5 changed files in this pull request and generated 4 comments.

Comment on lines +323 to +324
:param context: The context object for the current turn.
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext
Comment on lines +341 to +346
return _AuthInterceptResult(
should_skip_turn=context.activity.type
== ActivityTypes.invoke,
should_replay=True,
continuation_activity=None,
)
Comment on lines +233 to +240
elif flow_state.tag == _FlowStateTag.COMPLETE:
logger.info("Sign-in flow completed successfully.")
await context.send_activity(
Activity(
type=ActivityTypes.invoke_response,
value=InvokeResponse(status=200),
)
)
Comment thread tests/hosting_core/app/_oauth/test_authorization.py Outdated
Copilot AI review requested due to automatic review settings July 15, 2026 17:37

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 5 out of 5 changed files in this pull request and generated 6 comments.

Comment thread tests/hosting_core/app/_oauth/test_authorization.py
Comment thread tests/hosting_core/app/_oauth/test_authorization.py
Comment thread tests/hosting_core/app/_oauth/test_authorization.py
Comment thread tests/hosting_core/app/_oauth/test_authorization.py Outdated
Copilot AI review requested due to automatic review settings July 15, 2026 18:02

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 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread tests/hosting_core/app/_oauth/test_authorization.py Outdated
Copilot AI review requested due to automatic review settings July 15, 2026 18:07

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 5 out of 5 changed files in this pull request and generated 4 comments.

Comment thread tests/hosting_core/app/_oauth/test_authorization.py Outdated
Copilot AI review requested due to automatic review settings July 15, 2026 18:22

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 5 out of 5 changed files in this pull request and generated 5 comments.

Comment on lines +1023 to +1029
if not context.identity:
logger.error(
"Cannot replay turn because context.identity is not set. Ensure that the adapter is configured to set the identity."
)
raise ApplicationError(
"Cannot replay turn because context.identity is not set. Ensure that the adapter is configured to set the identity."
)
__replay_turn,
)

context.activity = act
Comment on lines +1049 to +1056
def __log_task_result(task: asyncio.Task):
try:
task.result()
except Exception as e:
logger.error(
f"Error occurred while replaying the turn.",
exc_info=True,
)
Comment thread tests/hosting_core/app/_oauth/test_authorization.py
Copilot AI review requested due to automatic review settings July 15, 2026 18:27

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 5 out of 5 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings July 15, 2026 18:33

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 5 out of 5 changed files in this pull request and generated 6 comments.

Comment on lines +1041 to +1045
await self._adapter.continue_conversation_with_claims(
context.identity,
act,
__replay_turn,
)
Comment thread tests/hosting_core/app/_oauth/test_authorization.py
Comment thread tests/hosting_core/app/_oauth/test_authorization.py
Comment on lines +718 to +721
res = await authorization._on_turn_auth_intercept(context, auth_handler_id)

assert continuation_activity == old_activity
assert intercepts
assert res.continuation_activity == old_activity
assert res.should_skip_turn
Copilot AI review requested due to automatic review settings July 15, 2026 18:38

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 5 out of 5 changed files in this pull request and generated 4 comments.

assert intercepts
assert res.continuation_activity == old_activity
assert res.should_skip_turn
assert not res.should_replay
Comment on lines +1031 to +1037
async def __replay(act: Activity):

await self._adapter.continue_conversation_with_claims(
context.identity,
act,
__replay_turn,
)
Comment on lines +1039 to +1046
def __log_task_result(task: asyncio.Task):
try:
task.result()
except Exception as e:
logger.error(
f"Error occurred while replaying the turn.",
exc_info=True,
)
Copilot AI review requested due to automatic review settings July 15, 2026 18:42

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 5 out of 5 changed files in this pull request and generated 4 comments.

Comment on lines 339 to +343
if not sign_in_state:
# flow just completed, no continuation activity
return False, None
is_invoke = context.activity.type == ActivityTypes.invoke
return _AuthInterceptResult(
should_skip_turn=is_invoke,
Comment on lines +1025 to +1027
for key, val in context.turn_state.items():
if replay_context.turn_state.get(key, None) is None:
replay_context.turn_state[key] = val
Comment on lines +1039 to +1046
def __log_task_result(task: asyncio.Task):
try:
task.result()
except Exception as e:
logger.error(
f"Error occurred while replaying the turn.",
exc_info=True,
)
Comment on lines +1018 to +1022
async def _handle_turn_skip(
self, context: TurnContext, turn_state: StateT, result: _AuthInterceptResult
):
"""Handle the case where the turn should be skipped due to an auth intercept result."""

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