Skip to content

[SDCT-1647] Collect caller commands inside a repository runner's environment - #396

Open
anmarchenko wants to merge 8 commits into
mainfrom
anmarchenko/commands-runner-environments
Open

anmarchenko wants to merge 8 commits into
mainfrom
anmarchenko/commands-runner-environments

Conversation

@anmarchenko

@anmarchenko anmarchenko commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Stacked on #395.

What

A command in a select --commands / collect --commands file can now name the repository runner environment it runs its tests in:

{"id": "vendor-py312", "job": "vendor 3.12", "argv": ["scripts/run-tests", "--venv", "f4cf61b462bb"],
 "runner": {"suite": "vendor", "environment": "f4cf61b462bb"}}

Some repositories' CI jobs don't call pytest: they call the repository's own test runner. In dd-trace-py, a job runs scripts/run-tests --venv f4cf61b462bb. The runner reads tests/suitespec.yml, builds a virtualenv, and runs pytest inside it. runner tells ddtest which of the runner's environments the command runs:

  • suite is the runner's name for a group of tests. In dd-trace-py, vendor is a suites: entry in tests/suitespec.yml, which runs pytest {cmdargs} tests/vendor/.
  • environment is one test environment of that suite, by the id the runner gives it. In dd-trace-py, a suite's matrix expands into one environment per Python version and dependency set. f4cf61b462bb is the hash of the vendor suite's Python 3.12 environment with msgpack~=1.0.0 and pytest-randomly, which is what --venv takes. scripts/run-tests --list prints suites with these hashes.
  • invocation picks the environment's test command, when it runs more than one. args are what the job passes after --, which the runner puts in place of {cmdargs}.

argv stays the job's command as CI runs it. ddtest records it in the inventory's identity but doesn't parse the runner's command line: the caller, such as the pipeline generator that wrote the job, already knows which environment the job runs, and names it in runner.

Collection lists the runner's environments, finds the command that the referenced one runs, and has the runner run ddtest's worker in that command's place, so the runner prepares the environment as it does for the job. The worker:

  • probes the interpreter the command actually runs, and lists its packages. That's platform.PythonProbeCommand for an interpreter or uv run/poetry run. For a pytest entry point, the worker reads the entry point's own interpreter line inside the prepared environment, including the wrapper uv writes for a relocatable environment;
  • records the identity and skipping controls that the runner sets for the command (_CI_DD_TAGS, DD_CIVISIBILITY_ITR_ENABLED, _DD_CIVISIBILITY_ITR_PREVENT_TEST_SKIPPING, …), which the inventory records in place of ddtest's own;
  • collects with --ddtrace --collect-only added to the runner's own PYTEST_ADDOPTS.

The inventory names the runner environment in its identity, and records that interpreter's runtime and packages. The command stays at run when the measured runtime isn't the Python that the runner declares for the environment (checked before any --runtime-tags apply), when the runner's command is a shell program (quoted characters are literal) or doesn't run pytest, or when the runner turns skipping off. A runner inventory decides only for its own commit: the runner's definitions, wherever the repository keeps them, decide what it collects. The runner never gets Datadog keys: it sees a placeholder DD_API_KEY and no DD_APP_KEY, and the service it sets for the command is the one the inventory records.

Python repositories are supported whose scripts/run-tests defines a TestRunner with environments by suite, run through scripts/uv-run-script, as dd-trace-py does. ddtest checks that interface by loading the runner. A source that only mentions --list is not a supported runner. The listing runs with the runner's own inline script dependencies, for at most two minutes, prints at most 16 MiB (the cap has no ReadFrom path), and has a WaitDelay so a descendant that holds the output open can't hang it. The runner runs with the command's whole environment.

Runners for other platforms join by implementing commandRunner in internal/selection/runner.go. plan and the decision are unchanged.

Why

#395 lets a caller list each CI job's commands, but collects only commands that run pytest directly. Repositories such as dd-trace-py run each job's tests through their own runner, which builds a test environment per suite and Python version and sets the tracer's identity and skipping controls itself. Collecting those commands outside the runner would probe the wrong interpreter and record the wrong identity, so their jobs couldn't skip safely.

E2E testing

Prerequisites: macOS or Linux with uv, Python 3.12 available to uv (uv python install 3.12), and this branch built with make build. Use a fresh dd-trace-py checkout at main. The steps use the runner's CI mode (GITLAB_CI=true), which runs on the host. CI mode needs a prebuilt ddtrace wheel, so a released one stands in. Unset DD_API_KEY: collect then only collects.

  1. Set up the checkout.
    git clone https://github.com/DataDog/dd-trace-py && cd dd-trace-py
    mkdir -p .cache/ddtrace-test-wheels/3.12
    uvx --python 3.12 pip download ddtrace --no-deps --only-binary=:all: -d .cache/ddtrace-test-wheels/3.12
  2. Find the vendor suite's Python 3.12 environment hash. scripts/run-tests --list --all-suites lists it. Use the first vendor venv with "python_version": "3.12", called HASH below.
  3. Write commands.json:
    {"schema_version": 1, "commands": [
      {"id": "vendor-py312", "job": "vendor 3.12", "argv": ["scripts/run-tests", "--venv", "HASH"],
       "env": {"GITLAB_CI": "true", "CI_COMMIT_AUTHOR": "QA <qa@example.com>", "DD_ENV": "ci"},
       "runner": {"suite": "vendor", "environment": "HASH"}}]}
  4. Run /path/to/ddtest collect --commands commands.json --output-dir inventories --platform python --framework pytest.
    Expected: exit 0. The first run builds .cache/test-environments/HASH. stdout lists vendor-py312 with inventories/vendor-py312.json.gz.
  5. Inspect it with gzip -dc inventories/vendor-py312.json.gz | jq '{complete, errors, runner: .identity.runner, runtime: .inputs.configurations["runtime.version"], tags: .inputs.configurations, vars: .datadog.variables, counts}'.
    Expected: runner is {"suite":"vendor","environment":"HASH"}, and runtime is a 3.12.x version even when the host's default python3 is another one. tags has test.configuration.env_hash: HASH and test.configuration.python: 3.12. vars holds the runner's _CI_DD_TAGS and _DD_CIVISIBILITY_ITR_PREVENT_TEST_SKIPPING: "1". counts is non-zero. complete is false, with the error worker skipping disabled by _DD_CIVISIBILITY_ITR_PREVENT_TEST_SKIPPING: dd-trace-py's runner turns skipping off unless the suite opts in.
  6. Opt the suite in. In tests/suitespec.yml, under vendor: → matrix:, add env: {DD_TRACE_PY_ENABLE_ITR_TEST_SKIPPING_FOR_JOB: 'true'} next to command:. Repeat steps 4 and 5.
    Expected: complete: true, errors: [], and _DD_CIVISIBILITY_ITR_PREVENT_TEST_SKIPPING: "0". Revert the file afterwards.
  7. Edge cases, each with collect and with jq '.errors':
    • "environment": "nope" → error runner suite "vendor" has no environment nope.
    • Run with --runtime-tags '{"runtime.name":"CPython","runtime.version":"3.13.0","os.platform":"Darwin","os.architecture":"arm64","os.version":"x"}' → error --runtime-tags declare Python 3.13.0, but the command collects with Python 3.12.x.
    • In a repository without scripts/run-tests, or with a scripts/run-tests that defines no TestRunner → error runner listing unavailable: …, and the command is not collected.
  8. Optionally, decide with ddtest select --inventory inventories/vendor-py312.json.gz --output selection.json and a Datadog API key. With step 5's inventory, the job's decision is run.

Cleanup: remove inventories/, commands.json, .cache/test-environments/HASH and .cache/ddtrace-test-wheels/.

🤖 Generated with Claude Code

anmarchenko and others added 4 commits December 4, 2026 15:16
select --commands and collect --commands read a commands file that lists
each CI job's commands, with their argv, cwd and environment. Each
command is collected with its job's env applied, so collection, its
probes, the identity Datadog is asked about and the inventory see the
command's values. collect writes one inventory per command to
--output-dir; select decides every command and rolls them up per job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…w identical commands with different ids

The Python platform read PYTEST_ADDOPTS when it detected the framework,
so discovery used ddtest's own options instead of a command's. Two
listed commands with different ids but the same contents are two
commands, not a duplicate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Windows compares variable names without case, so dd_api_key would set
DD_API_KEY there; case-insensitive filesystems give Unit and unit the
same inventory file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A command in a commands file can name the runner environment it runs in,
as runner: {suite, environment, invocation, args}. Collection lists the
runner's environments through its TestRunner, finds the command the
referenced one runs, and has the runner run ddtest's worker in that
command's place, so the runner prepares the environment itself.

The worker probes the interpreter the command runs, with the probe
command PythonProbeCommand gives for it, records the identity and
skipping controls the runner sets for the command, and collects the
command with --ddtrace and --collect-only added to the runner's
PYTEST_ADDOPTS. The inventory records that interpreter's runtime and
packages and the runner's identity, and names the runner environment in
its identity. The measured runtime is checked against the environment's
declared Python before any runtime tags apply.

The listing checks the runner's interface by loading it, bounds its
output without a ReadFrom path, and sets a WaitDelay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 89.89%
• Overall Coverage: 87.71% (+0.17%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: de64ad9 | Docs | View more details | Give us feedback!

@anmarchenko
anmarchenko marked this pull request as ready for review December 4, 2026 15:05
@anmarchenko
anmarchenko requested a review from a team as a code owner December 4, 2026 15:05
@anmarchenko

Copy link
Copy Markdown
Member Author

@autotest review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T15:06:36.081666Z 3f16b33 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@anmarchenko anmarchenko changed the title Collect caller commands inside a repository runner's environment [SDCT-1647] Collect caller commands inside a repository runner's environment Dec 4, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3f16b33ebe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/selection/collect.go Resolved

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Bits Code Review: FAIL

Runner-backed inventories can remain eligible for skipping after runner commands change, leaving newly selected tests unaccounted for. Runner listing also exposes the selection API key to repository code.

Open Bits AI session

🤖 Bits Code Review · Commit 3f16b33

Comment thread internal/selection/python_runner.go Resolved
Comment thread internal/selection/inventory.go Resolved
Comment thread internal/platform/python_launchers.go Resolved
Comment thread internal/selection/collect.go Resolved

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Bits Code Review: FAIL

Runner-definition changes can leave stale inventories eligible to skip jobs, and catalog loading exposes the API key to repository code during select --commands. Additional defects prevent collection or skipping for runner-specific services, quoted pytest arguments, and explicit pytest paths.

Open Bits AI session

🤖 Bits Code Review · Commit 3f16b33

Comment thread internal/selection/python_runner.go Resolved
Comment thread internal/selection/collect.go Resolved
Comment thread internal/selection/collect.go Resolved
Comment thread internal/selection/runner.go Resolved
Comment thread internal/selection/python_runner.go Resolved
- Record the service the runner gives the command, read while its
  variables apply, so an inventory whose runner sets DD_SERVICE can skip.
- Give the runner no Datadog keys: the listing and collection see the
  placeholder DD_API_KEY and no DD_APP_KEY.
- Find the interpreter a pytest entry point runs inside the prepared
  environment, from the entry point's own script, including the
  wrapper uv writes for a relocatable environment.
- Read quoted characters in a runner's command as literals, so options
  such as --ignore-glob='*civisibility*' don't make it a shell program.
- Decide a runner inventory only for its own commit: the runner's
  definitions, wherever the repository keeps them, decide what it
  collects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Bits Code Review: PASS

More details

Commands collect within repository-prepared Python environments;
credentials are sanitized, inventories cannot skip across commits,
pytest probing follows the entry point’s interpreter,
and service identity reflects runner variables.
All four earlier findings are fixed; no new reportable issue remains.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 9fe6afb

Base automatically changed from anmarchenko/commands-from-caller to main December 4, 2026 16:13
…mands-runner-environments

# Conflicts:
#	docs/running.md
#	internal/platform/python_command.go
#	internal/selection/collect.go
#	internal/selection/commands.go

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Bits Code Review: FAIL

Runner collection can fingerprint packages that uv replaces before test discovery, leaving a complete inventory describing the wrong environment. Bounded runner listings can also leave descendant processes running after returning.

Open Bits AI session

🤖 Bits Code Review · Commit 9fe6afb

Comment thread internal/selection/python_runner.go Resolved
- The worker runs the command, then probes its interpreter, since the
  command may install dependencies as it starts, as uv run does: the
  inventory records the packages collection ran with.
- The listing runs in its own process group, which a timeout kills, and
  what is left of the group is killed once the listing returns, so a
  descendant doesn't outlive it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Bits Code Review: FAIL

The PR collects pytest inside repository runner environments, preserving their runtime and identity; stale package snapshots and surviving listing descendants are now fixed. One issue remains: the new cleanup assertion misidentifies zombies as running processes, causing false test failures in containers without orphan reaping.

Open Bits AI session

🤖 Bits Code Review · Commit 614dc52

Where nothing reaps orphans, as in some containers, the killed
descendant stays a zombie, which kill -0 still reports as alive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@datadog-prod-us1-5 datadog-prod-us1-5 Bot 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.

Bits Code Review: PASS

More details

The PR collects pytest inside repository runner environments, recording their runtime, packages, identity, and skipping controls. Package snapshots now follow collection, listing descendants are cleaned up, and the cleanup assertion handles unreaped zombies; the previously failing non-reaping-parent scenario now succeeds, with no open findings.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit de64ad9

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