Skip to content

Add ExecPlan for contextvar lifecycle (2.4.2)#232

Draft
leynos wants to merge 1 commit intomainfrom
implement-contextvar-lifecycle-jkbrbw
Draft

Add ExecPlan for contextvar lifecycle (2.4.2)#232
leynos wants to merge 1 commit intomainfrom
implement-contextvar-lifecycle-jkbrbw

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Feb 20, 2026

Summary

  • Introduces a new ExecPlan document to implement a contextvar-based lifecycle for correlation IDs (2.4.2).
  • Outlines the end-to-end plan: alignment, tests, implementation, documentation, and validation gates.
  • Serves as the blueprint prior to code changes, ensuring traceability to roadmap/design/docs references.

What changed

  • Added docs/execplans/2-4-2-contextvar-lifecycle.md with a comprehensive executable plan covering:
    • Purpose and big picture
    • Constraints, tolerances, risks, progress, surprises, decision log, and outcomes
    • Plan of work (Stages A–E) including alignment, tests, implementation, documentation, and validation
    • Concrete steps, validation/exits, and acceptance criteria
    • Context and orientation for contextvar-based correlation ID lifecycle in middleware
    • Expected touchpoints and validation commands

This PR contains only the new ExecPlan document; no code changes are introduced yet. It is intended to guide subsequent implementation and testing.

Plan of work (highlights)

  • Stage A: align source-of-truth references (roadmap/design/docs paths)
  • Stage B: add failing tests for lifecycle (unit and behavioural)
  • Stage C: implement lifecycle-safe middleware logic (process_request/process_response and cleanup on failures)
  • Stage D: document behaviour and decisions (design doc, user guide)
  • Stage E: run quality gates (fmt, typecheck, lint, tests, markdown checks)

Validation plan

  • Commands to run from repository root (as outlined in the ExecPlan):
    • set -o pipefail && make check-fmt 2>&1 | tee /tmp/2-4-2-check-fmt.log
    • set -o pipefail && make typecheck 2>&1 | tee /tmp/2-4-2-typecheck.log
    • set -o pipefail && make lint 2>&1 | tee /tmp/2-4-2-lint.log
    • set -o pipefail && make test 2>&1 | tee /tmp/2-4-2-test.log
    • Optional: set -o pipefail && make markdownlint 2>&1 | tee /tmp/2-4-2-markdownlint.log

Acceptance criteria (post-implementation)

  • Contextvar lifecycle correctly sets correlation ID at request start and resets on response end
  • Reset token is stored and used to ensure deterministic cleanup
  • Cleanup occurs on both success and failure paths
  • Public API and middleware behavior remain stable aside from lifecycle semantics
  • All unit and behavioural tests pass; all quality gates succeed
  • Design doc and user guide reflect final behavior; roadmap item 2.4.2 marked complete

Risks and considerations

  • The ExecPlan assumes discovery of the target Python middleware paths; path alignment may be required before implementation
  • Changes may require updating multiple docs (design, user guide, roadmap) to stay in sync

Notes for reviewers

  • This PR does not modify code yet; please review the completeness and realism of the lifecycle plan
  • If you have known target file paths or existing middleware/context storage modules, you can provide alignment input to refine Stage A in the plan

◳ Generated by DevBoxer


ℹ️ Tag @devboxerhub to ask questions and address PR feedback

📎 Task: https://www.devboxer.com/task/f181792c-6ada-414d-819f-016cad2701fe

Summary by Sourcery

Documentation:

  • Document the end-to-end execution plan for implementing a contextvar-based correlation ID lifecycle, including stages, validation steps, and acceptance criteria.

… IDs

Add a new ExecPlan document defining the lifecycle-safe contextual storage
and cleanup of correlation IDs using Python contextvars, aligned with roadmap
item 2.4.2 and design-doc section 3.3.4. The plan includes stages for test
coverage, implementation, documentation updates, and quality gates to ensure
correctness and isolation of correlation ID handling in middleware request
processing.

Co-authored-by: devboxerhub[bot] <devboxerhub[bot]@users.noreply.github.com>
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Feb 20, 2026

Reviewer's Guide

Adds a new ExecPlan markdown document describing the end-to-end implementation and validation plan for introducing a contextvar-based correlation ID lifecycle in middleware, without changing any code yet.

Sequence diagram for contextvar-based correlation ID lifecycle in middleware

sequenceDiagram
    actor Client
    participant Framework
    participant Middleware
    participant Request
    participant Response
    participant CorrelationContextVar
    participant CorrelationContextStore

    Client->>Framework: incoming HTTP request
    Framework->>Middleware: process_request(request)
    Middleware->>CorrelationContextVar: set(correlation_id)
    CorrelationContextVar-->>Middleware: token
    Middleware->>CorrelationContextStore: store_token(request_id, token)
    Middleware-->>Framework: request processed

    Framework->>Framework: route and execute view/handler
    alt handler succeeds
        Framework->>Middleware: process_response(request, response)
        Middleware->>CorrelationContextStore: get_token(request_id)
        CorrelationContextStore-->>Middleware: token
        Middleware->>CorrelationContextVar: reset(token)
        Middleware->>CorrelationContextStore: clear_token(request_id)
        Middleware-->>Framework: response processed
        Framework-->>Client: HTTP response
    else handler fails with exception
        Framework->>Middleware: process_response(request, error_response)
        Middleware->>CorrelationContextStore: get_token(request_id)
        CorrelationContextStore-->>Middleware: token
        Middleware->>CorrelationContextVar: reset(token)
        Middleware->>CorrelationContextStore: clear_token(request_id)
        Middleware-->>Framework: error response processed
        Framework-->>Client: HTTP error response
    end
Loading

Class diagram for planned contextvar correlation ID middleware components

classDiagram
    class CorrelationContextVar {
        <<ContextVar>>
        +str name
        +str default
        +set(str value) token
        +get() str
        +reset(token token) void
    }

    class CorrelationContextStore {
        +store_token(str request_id, token token) void
        +get_token(str request_id) token
        +clear_token(str request_id) void
    }

    class CorrelationIdMiddleware {
        +CorrelationContextVar correlation_var
        +CorrelationContextStore context_store
        +process_request(Request request) void
        +process_response(Request request, Response response) Response
        -str generate_correlation_id(Request request)
    }

    class Request {
        +str id
        +dict state
    }

    class Response {
        +int status_code
        +dict headers
    }

    CorrelationIdMiddleware --> CorrelationContextVar : uses
    CorrelationIdMiddleware --> CorrelationContextStore : uses
    CorrelationContextStore --> Request : keys_by id
    CorrelationIdMiddleware --> Request : reads_writes state
    CorrelationIdMiddleware --> Response : attaches_headers
Loading

Flow diagram for ExecPlan stages A–E for contextvar lifecycle work

flowchart TD
    A[Stage A: Align references
    - Locate roadmap 2.4.2
    - Locate design section 3.3.4
    - Locate middleware and context storage modules
    - Update ExecPlan paths] --> B[Stage B: Add failing tests
    - Unit tests for set/token/reset/failure cleanup
    - Behavioural tests for visibility/clearing/isolation]

    B --> C[Stage C: Implement middleware lifecycle
    - process_request sets ContextVar and stores token
    - process_response resets using stored token
    - Wire cleanup into failure paths]

    C --> D[Stage D: Update documentation
    - Design doc lifecycle rationale
    - Users guide behaviour and API details
    - Mark roadmap item 2.4.2 complete]

    D --> E[Stage E: Run validation gates
    - make check-fmt
    - make typecheck
    - make lint
    - make test
    - Optional: make markdownlint
    - Record evidence in ExecPlan]
Loading

File-Level Changes

Change Details Files
Introduce an ExecPlan document that defines the contextvar-based correlation ID lifecycle plan and its execution stages.
  • Add a markdown ExecPlan describing purpose, constraints, risks, and tolerances for implementing correlation ID lifecycle via contextvars.
  • Document staged plan (alignment, tests-first, implementation, documentation, and validation gates) with concrete steps and acceptance criteria.
  • Specify expected code touchpoints, testing strategy (pytest and pytest-bdd), and required quality-gate commands to validate the eventual implementation.
  • Include progress checklist, surprises/discoveries, and a decision log indicating the need for path alignment before implementation.
docs/execplans/2-4-2-contextvar-lifecycle.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 20, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch implement-contextvar-lifecycle-jkbrbw

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant