agentleFS
MicrosoftKnown publishermicrosoft/FeatureManagement-Python/.github/skills/sync-async-pattern/SKILL.md

sync-async-pattern

Skill26 starsChanged 9 months agoMITView on GitHub

Guide for implementing sync/async mirrored code in this project. Use when adding new classes, methods, or feature filters that need both sync and async versions, or when modifying existing sync code that has an async counterpart in featuremanagement/aio/.

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 3 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every source file MUST start with:

    line 30
  2. Very specific:

    Keep the same class name (e.g., both are FeatureManager).

    line 46
  3. Very specific:

    Add async to method definitions: def evaluate(...) → async def evaluate(...)

    line 51
  4. Very specific:

    Add await to calls that invoke filters, callbacks, or accessors.

    line 52
  5. Very specific:

    Use this pattern for any new filter whose evaluate does not perform I/O.

    line 70
  6. Distinctive:

    When adding a new public class:

    line 100
  7. Very specific:

    Add to sync __init__.py with __all__ entry.

    line 101
  8. Very specific:

    If it has an async version, add to async __init__.py with __all__ entry.

    line 102
  9. Distinctive:

    Not every sync test needs an async counterpart (e.g., refresh and telemetry tests are sync-only).

    line 114

≈ “Common” is an estimate: at least 1 in 5 of the 383,637 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Sync/Async Mirroring Pattern

This project maintains parallel sync and async implementations. Every change to sync code in featuremanagement/ must be mirrored in featuremanagement/aio/, and vice versa.

Directory mapping

SyncAsync
featuremanagement/_featuremanager.pyfeaturemanagement/aio/_featuremanager.py
featuremanagement/_featurefilters.pyfeaturemanagement/aio/_featurefilters.py
featuremanagement/_defaultfilters.pyfeaturemanagement/aio/_defaultfilters.py
featuremanagement/__init__.pyfeaturemanagement/aio/__init__.py

Shared code that does NOT have an async counterpart:

  • featuremanagement/_featuremanagerbase.py — base class used by both sync and async FeatureManager
  • featuremanagement/_models/ — data models imported by both
  • featuremanagement/_time_window_filter/ — time window logic (no I/O, used as-is)
  • featuremanagement/azuremonitor/ — telemetry (no async version)

Copyright header

Every source file MUST start with:

# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------

Followed by a module-level docstring.

How to convert sync to async

Classes

  • Keep the same class name (e.g., both are FeatureManager). Users disambiguate by import path.
  • Both sync and async FeatureManager inherit from FeatureManagerBase.

Methods

  • Add async to method definitions: def evaluate(...) → async def evaluate(...)
  • Add await to calls that invoke filters, callbacks, or accessors.

Default filters (composition pattern)

Async default filters do NOT duplicate logic. They wrap the sync implementation:

from .._defaultfilters import TimeWindowFilter as SyncTimeWindowFilter

class TimeWindowFilter(FeatureFilter):
    def __init__(self):
        self._filter = SyncTimeWindowFilter()

    @FeatureFilter.alias("Microsoft.TimeWindow")
    async def evaluate(self, context, **kwargs):
        return self._filter.evaluate(context, **kwargs)

Use this pattern for any new filter whose evaluate does not perform I/O.

Callbacks and accessors

The async FeatureManager supports BOTH sync and async callbacks. Use inspect.iscoroutinefunction to detect and handle both:

import inspect

if inspect.iscoroutinefunction(self._on_feature_evaluated):
    await self._on_feature_evaluated(result)
else:
    self._on_feature_evaluated(result)

Imports

  • Sync files import from ._models, ._featurefilters, etc.
  • Async files import from .._models, .._featurefilters, etc. (one level up from aio/).

__init__.py exports

Sync (featuremanagement/__init__.py)

Exports everything: FeatureManager, filters, all models, __version__, and defines __all__.

Async (featuremanagement/aio/__init__.py)

Exports ONLY async-specific classes: FeatureManager, FeatureFilter, TimeWindowFilter, TargetingFilter. Does NOT re-export models or __version__ — users import those from the sync package.

When adding a new public class:

  1. Add to sync __init__.py with __all__ entry.
  2. If it has an async version, add to async __init__.py with __all__ entry.

Test file naming

Sync testAsync counterpart
tests/test_feature_manager.pytests/test_feature_manager_async.py
tests/test_feature_variants.pytests/test_feature_variants_async.py
tests/test_default_feature_flags.pytests/test_default_feature_flags_async.py
  • Async test files append _async to the sync filename.
  • Async tests use pytest-asyncio with @pytest.mark.asyncio on test functions.
  • Not every sync test needs an async counterpart (e.g., refresh and telemetry tests are sync-only).

Checklist for adding new code

  1. Write the sync implementation in featuremanagement/.
  2. Write the async mirror in featuremanagement/aio/ following the patterns above.
  3. Export from both __init__.py files if public.
  4. Write sync tests in tests/test_*.py.
  5. Write async tests in tests/test_*_async.py.
  6. Run all validation: pylint featuremanagement, black featuremanagement, mypy featuremanagement, pytest tests.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.