Skip to content

fix(tests): a logfire console span leaks into stdout of a later test #3532

Closed

Description

@dan-petty

Problem

tests/roadmap/test_roadmap_implement_command.py::test_a_done_step_prints_its_outcome_as_json_and_exits_0 failed in Tests & Coverage (shard 2 of 4) on the pull request #3526 (run 41919824262). It passed when the job ran again on the same commit, and it passes in a local run of the same shard.

json.loads(result.stdout) raised JSONDecodeError: Extra data. The stdout held console spans of logfire before the JSON: agent run, chat function:ModelScript:stream, running tool: write_file. A test that runs earlier in the same xdist worker most likely calls logfire.configure with the console on and instrument_pydantic_ai. The state stays set for the next test of the worker.

Expected

No test leaves the logfire configuration or the pydantic-ai instrumentation set. A test run prints no span on stdout, whatever the order of the tests in a worker.

Evidence

The change of #3526 does not touch logfire. The shard split is new (#3517), so the order of tests in a worker changes with each change of test times.

Proposed design

Problem statement

A test that runs earlier in an xdist worker calls logfire.configure with the console on and calls logfire.instrument_pydantic_ai. The state stays set for the next test of the worker. A later test that parses stdout as JSON fails with JSONDecodeError: Extra data, because the stdout holds console spans. The change makes every test leave no logfire configuration and no pydantic-ai instrumentation behind.

Acceptance criteria

Requirement: No console span in a later test's stdout

The test suite SHALL keep a logfire console span of one test out of the stdout of a later test in the same process.

Scenario: The JSON test runs after a configuring test

  • WHEN a test calls logfire.configure with the console on and calls logfire.instrument_pydantic_ai, and test_a_done_step_prints_its_outcome_as_json_and_exits_0 runs after it in one process
  • THEN json.loads(result.stdout) parses the outcome, and no span text is in result.stdout.

Requirement: Reset after a failed test

The autouse fixture SHALL reset the logfire configuration and the pydantic-ai instrumentation when a test fails.

Scenario: A configuring test raises

  • WHEN a test that configured logfire and instrumented pydantic-ai raises an exception
  • THEN the fixture turns the console off and the pydantic-ai instrumentation off before the next test starts.

Requirement: The telemetry tests keep their assertions

The telemetry tests SHALL keep every assertion on the console output and the pydantic-ai spans.

Scenario: The logfire tests run under the autouse fixture

  • WHEN tests/telemetry/test_telemetry_logfire.py and tests/telemetry/test_telemetry_llm_content.py run
  • THEN every test in both files passes with the same assertions.

Requirement: The suite passes in any order

The test suite SHALL pass with the telemetry tests and the implement command tests in any order in one worker.

Scenario: Both files run in one worker in both orders

  • WHEN tests/telemetry/test_telemetry_logfire.py and tests/roadmap/test_roadmap_implement_command.py run in one xdist worker, in either order
  • THEN both files pass and no captured stdout holds a console span.

Existing Tool Considered

No alternatives check has run for this item. pytest, already a dependency, gives the autouse fixture that runs after each test, a failed one included. logfire gives logfire.configure(console=False) and pydantic-ai gives Agent.instrument_all(False). Neither restores the logfire global configuration by itself, so the project keeps one fixture of about 20 lines. Written cost to build, keep and fix that fixture: about 20k tokens and about 2 hours.

Key questions

  • Which tests set the logfire configuration and the pydantic-ai instrumentation?: tests/telemetry/test_telemetry_logfire.py configures logfire and instruments pydantic-ai for real. tests/telemetry/test_telemetry_llm_content.py patches logfire.configure and logfire.instrument_pydantic_ai, and the test(cli): migrate 49 tests to public interfaces #2167 design keeps those patches. The product path is src/devops_cli/telemetry/logfire.py, which src/devops_cli/docs/generator.py calls. [sources: tests/telemetry/test_telemetry_logfire.py, tests/telemetry/test_telemetry_llm_content.py, src/devops_cli/telemetry/logfire.py, src/devops_cli/docs/generator.py, openspec/changes/2167-the-49-test-files-that-no/tasks.md:67]
  • Where does the reset of the logfire configuration and the pydantic-ai instrumentation live?: In one autouse fixture in the root tests/conftest.py that runs after every test. (best practice) [sources: docs/TESTING.md, openspec/changes/2167-the-49-test-files-that-no/design.md:11]
    • Chosen: One autouse fixture for every test. It resets after each test, a failed test included, and no test author must remember it.
    • Rejected: A finally block in each test that configures logfire. It is easy to forget, and it does not cover a test that reaches the code path indirectly.
    • Rejected: Split the telemetry tests and the command tests across workers. The order of tests changes with test times (perf(ci): run the test row in four shards on four runners #3517), so the leak returns.
  • How does the fixture turn the console and the pydantic-ai instrumentation off?: The fixture calls logfire.configure(console=False) and Agent.instrument_all(False) in its teardown. (best practice) [sources: docs/TELEMETRY.md:149, tests/telemetry/test_telemetry_logfire.py]
    • Chosen: Turn the console off and uninstrument pydantic-ai in the teardown. It uses the two switches that the product path already sets, and it leaves a quiet configuration.
    • Rejected: Restore a snapshot of the previous global configuration. logfire keeps no public API that returns the previous configuration.
    • Rejected: Remove the logfire module from sys.modules and import it again. Other modules hold the configured instance, so a new import changes nothing they use.
  • Does the fix change the product code or only the test suite?: Only the test suite. (best practice) [sources: src/devops_cli/telemetry/logfire.py, tests/telemetry/test_telemetry_logfire.py]
    • Chosen: Reset the state in the test suite. The issue asks for test isolation, and the product keeps the console output for a person at a terminal.
    • Rejected: Make the console output conditional on a TTY in src/devops_cli/telemetry/logfire.py. A test that asserts the console output runs with a captured stdout, which is not a TTY, so the check hides the output that the test asserts.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-07: partly.
Tool: pytest-dev/pytest-forked (https://github.com/pytest-dev/pytest-forked), MIT, last activity 2026-08-08, maintained.

Activity

  1. added
    source/agent
    type/bug
    scope/tests
    status/new
    value/medium
    effort/low
    on Nov 4, 2026
  2. dan-petty commented on Nov 4, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/tests. The (tests) scope of the title gives the scope.
    • Priority: P2-Medium. Flaky CI failures block developer confidence but do not break functionality or affect end users directly. The failed run 41919824262 does not set P0: the author has no write access, and no caller attached it as evidence. A person can set P0 with the label priority/p0-critical.
    • Value: Medium. Flaky test runs waste developer time and erode trust in the CI signal, but they do not change product behaviour.
    • Effort: Low. The fix is a targeted teardown of logfire.configure and instrument_pydantic_ai in a shared fixture or conftest.
    • Related: fix(ci): review workflow tests start the logfire SDK's credential check, which the network guard blocks #965, which asks for a different change. Both items fix logfire state leaking into test execution, but #965 stops the SDK credential-check network call while this stops console spans from polluting stdout.
  3. added
    priority/p2-medium
    on Nov 4, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 6, 2026
  5. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at be8d18a (see proposed design in issue body).

  6. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

    Alternatives check: partly

    Reason: The web searches returned no results (connection timeouts), so I read the package and repository facts: pytest-forked runs every test in a forked child process, so a logfire configuration and pydantic-ai instrumentation that one test sets cannot reach the next test of a worker, and a failing test cannot leak either. It only sidesteps the leaked state instead of resetting logfire and pydantic-ai as the issue asks, needs fork (no Windows) and a coverage setup that reads subprocesses, so it covers the flaky stdout in part while the ~20-line autouse fixture stays the targeted fix.

    Tool: pytest-dev/pytest-forked (https://github.com/pytest-dev/pytest-forked)

    • License: MIT
    • Last activity: 2026-08-08
    • Maintained: yes

    Covers:

    • Runs each test in a forked child process, so a logfire configuration and pydantic-ai instrumentation set by one test are gone before the next test starts
    • Keeps the console spans of one test out of the stdout that a later test parses as JSON, whatever the order of tests in a worker
    • Holds after a failed test too, because the child process exits and the parent state is unchanged

    Lacks:

    • Does not reset the logfire configuration or uninstrument pydantic-ai; it discards the process, so the explicit teardown of the acceptance criteria is not met
    • Relies on POSIX fork, so it does not run on Windows
    • Adds a fork for every test and needs subprocess-aware coverage, on top of the existing xdist shards
    • Blunter than the one autouse fixture the issue designs, which resets only the logfire and pydantic-ai state

    Tools considered:

    Searches run:

    • pytest run each test in subprocess isolation plugin pytest-forked pytest-isolate, results: 0
    • logfire pytest fixture reset configuration between tests console spans, results: 0
    • logfire testing pytest fixture, results: 0
    • logfire testing cap_logfire, results: 0
    • pytest isolation plugin, results: 0
    • pytest-forked xdist run each test in subprocess, results: 0
    • logfire.testing cap_logfire pytest fixture, results: 0
    • pytest-forked, results: 0
    • pytest xdist forked incompatible, results: 0
    • pytest-forked plugin documentation, results: 0

    Sources:

    Checked on 2026-11-07. A person decides what this means for the item. The check changes no label and closes no issue.

  7. added
    status/in-progress
    and removed
    status/ready
    on Nov 14, 2026
  8. added
    status/in-review
    and removed
    status/in-progress
    on Nov 14, 2026
  9. removed
    status/in-review
    on Nov 14, 2026
  10. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-14 15:02 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/testssource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions