feat: expose per-turn state (ctx.state, App(state=...)) and wire into dispatch - #554
Open
Lily Du (lilyydu) wants to merge 2 commits into
Open
feat: expose per-turn state (ctx.state, App(state=...)) and wire into dispatch#554Lily Du (lilyydu) wants to merge 2 commits into
ctx.state, App(state=...)) and wire into dispatch#554Lily Du (lilyydu) wants to merge 2 commits into
Conversation
Lily Du (lilyydu)
force-pushed
the
lilyydu/02-state-api
branch
from
August 11, 2026 17:35
4219bfb to
be573f4
Compare
Lily Du (lilyydu)
marked this pull request as ready for review
August 11, 2026 17:35
Contributor
There was a problem hiding this comment.
Pull request overview
This PR wires the previously introduced per-turn state layer into the microsoft_teams.apps dispatch lifecycle and exposes the opt-in public API (App(state=...), ctx.state) so application handlers can read/write conversation/user state across turns (aligned with the C# SDK behavior).
Changes:
- Add
App(state=...)opt-in via newstateoption onAppOptions/InternalAppOptionsand resolve it to aTurnStateLoader. - Load per-turn state onto
ctx.statebefore handler dispatch and persist+seal it at the end of each turn; add tests covering disabled/enabled/sealed/error-save behavior. - Add a new
examples/statesample app and register it in the workspace lockfile.
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| uv.lock | Registers the new examples/state workspace member. |
| packages/apps/tests/test_state.py | Adds unit tests for create_state_loader resolution logic and warning behavior. |
| packages/apps/tests/test_app_process.py | Adds integration tests for ctx.state lifecycle (disabled/enabled/persist/seal/error). |
| packages/apps/src/microsoft_teams/apps/state/loader.py | Introduces create_state_loader(...) factory for App(state=...) resolution. |
| packages/apps/src/microsoft_teams/apps/state/init.py | Re-exports create_state_loader on the apps.state package surface. |
| packages/apps/src/microsoft_teams/apps/routing/activity_context.py | Adds ActivityContext.state field for per-turn state access. |
| packages/apps/src/microsoft_teams/apps/options.py | Adds state option to app configuration with documentation. |
| packages/apps/src/microsoft_teams/apps/app.py | Resolves and passes state_loader into ActivityProcessor. |
| packages/apps/src/microsoft_teams/apps/app_process.py | Wires load/save/seal of per-turn state into the dispatch lifecycle. |
| packages/apps/src/microsoft_teams/apps/init.py | Re-exports state types on the public microsoft_teams.apps surface (currently missing create_state_loader). |
| examples/state/src/main.py | Adds a runnable example demonstrating per-conversation and per-user state usage. |
| examples/state/README.md | Documents how to run the new state example and expected behavior. |
| examples/state/pyproject.toml | Declares the example app project dependencies and workspace source. |
Suppressed comments (2)
packages/apps/src/microsoft_teams/apps/app_process.py:322
- Because
_persist_turn_state()is awaited in afinally, any exception while saving state can override an exception raised by the handler/middleware, changing the error that callers observe. Consider catching persistence failures in thefinally(and logging / emittingon_error) so they don’t mask the primary failure.
finally:
await self._persist_turn_state(activityCtx)
packages/apps/src/microsoft_teams/apps/init.py:53
create_state_loaderis not included in__all__, so it won’t be exported frommicrosoft_teams.appseven if imported. Add it to the export list if it’s meant to be part of the public surface.
"StateOptions",
"TurnState",
"TurnStateContainer",
"TurnStateSealedError",
"to_threaded_conversation_id",
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Lily Du (lilyydu)
force-pushed
the
lilyydu/02-state-api
branch
from
August 13, 2026 20:24
be573f4 to
bcd0f15
Compare
Lily Du (lilyydu)
force-pushed
the
lilyydu/02-state-api
branch
from
August 13, 2026 21:23
4082aa4 to
f53df4e
Compare
This was referenced Aug 13, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This is PR 2 of 5 in the multi-connection OAuth stack. PR #553 lays out the state foundation, whereas this PR uses it - it exposes the public API, adds the
App(state=...)opt-in, and wires load/save around every turn in the activity processor. Behavior mirrors the C#Microsoft.Teams.Apps.Stateintegration so the SDKs stay aligned.When state is disabled (the default), everything short-circuits: the loader is
None,ctx.state is None, and there is no per-turn overhead.What changed
Opt-in (
options.py) — newstate: Optional[Union[bool, StateOptions]]onAppOptions/InternalAppOptions.None/False→ off;True→ app's shared storage;StateOptions(...)→ custom prefix/TTL/store.Resolution (
state/loader.py) — newcreate_state_loader(state, fallback_storage)factory: the wholebool | StateOptions | None → Optional[TurnStateLoader]decision in one place; warns when the resolved store is in-memoryLocalStorage.Wiring (
app.py) — resolvesself._state_loaderat construction, passes it toActivityProcessor.Dispatch lifecycle (
app_process.py) —_load_turn_statebefore the try-block,_persist_turn_statein afinally(saves even on handler error, then seals).Public surface —
ActivityContext.state; re-exportsStateOptions,TurnState,TurnStateContainer,TurnStateSealedError,create_state_loader.Example —
examples/state/with a per-conversation counter + per-user name.