Inferensys

Prompt

Monorepo Affected Test Detection Prompt Template

A practical prompt playbook for using Monorepo Affected Test Detection Prompt Template in production AI workflows.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Defines the ideal scenario, required inputs, and limitations for using the monorepo affected test detection prompt in a CI/CD pipeline.

This prompt is designed for build engineers and SDETs working in monorepo environments who need to determine which tests to run based on a set of code changes. Instead of executing the entire test suite, which can take hours, this prompt analyzes shared dependency changes, package.json or build file diffs, and cross-package import graphs to produce a targeted test selection. Use this prompt when you have a git diff, a dependency graph representation, and a test-to-package mapping. The output is a structured test selection with impact rationale, a build graph summary, and a parallelization recommendation.

The ideal input is a structured diff of changed files, a machine-readable dependency graph (e.g., a JSON representation of your Nx, Turborepo, or Bazel project graph), and a mapping of test suites to the packages or modules they cover. The prompt works best when the dependency graph includes explicit dependsOn relationships and the test mapping is granular enough to distinguish unit, integration, and end-to-end test suites. You should also provide a runtime budget or a maximum parallelism constraint so the model can produce a realistic execution plan rather than an unbounded recommendation.

This prompt is not a replacement for a dedicated build system plugin like Nx Affected or Turborepo's --filter flag. It is a decision-support tool that produces auditable selection logic you can feed into your CI orchestrator. Do not use this prompt when the dependency graph is incomplete, when test-to-package mappings are missing or stale, or when the change set includes infrastructure-as-code modifications that require environment-level validation outside the package graph. In high-risk regulated environments, always require a human to review the selection rationale and confirm that no safety-critical tests were excluded before proceeding with a reduced test run.

PRACTICAL GUARDRAILS

Use Case Fit

Where this prompt works and where it does not. Understand the operational boundaries before integrating it into a CI/CD pipeline.

01

Good Fit: Monorepo CI/CD Pipelines

Use when: you have a monorepo with multiple packages, shared libraries, and a build graph (e.g., Nx, Turborepo, Bazel). The prompt excels at analyzing package.json diffs, import graph changes, and cross-package dependency propagation to shrink test suites. Guardrail: always provide the full build graph or dependency map as structured input; the model cannot infer it from code alone.

02

Bad Fit: Single-Repository or Polyrepo Setups

Avoid when: your project is a single deployable artifact with no internal package boundaries, or you manage services in separate repositories with explicit API contracts. The prompt's dependency analysis adds overhead without benefit. Guardrail: use a simpler change-based selection prompt for polyrepo setups, and rely on contract tests at service boundaries.

03

Required Inputs: Build Graph and Diff Context

Risk: the prompt produces unreliable test selections if it lacks a structured representation of the dependency graph. Guardrail: provide a machine-readable build graph (JSON or YAML), the full file diff, and a list of affected packages. Without these, the model hallucinates dependency relationships and misses transitive impacts.

04

Operational Risk: False Negatives in Test Skipping

Risk: the prompt may recommend skipping tests that are actually affected by a change, especially when dependencies are implicit (e.g., shared database schemas, environment variables, or runtime configuration). Guardrail: always run a mandatory smoke test suite regardless of the prompt's recommendation, and log skipped tests with explicit rationale for post-release audit.

05

Operational Risk: Build Graph Staleness

Risk: if the provided build graph is outdated or incomplete, the prompt's impact analysis will miss new dependencies or include removed ones. Guardrail: regenerate the build graph as a CI step before invoking the prompt, and include a graph generation timestamp in the prompt context so the model can flag potential staleness.

06

Scale Limit: Very Large Monorepos

Risk: in monorepos with hundreds of packages and thousands of interdependent tests, the prompt context window may overflow or the model may produce incomplete parallelization plans. Guardrail: partition the analysis by affected subgraphs, run the prompt per partition, and merge results with a deduplication step. Consider a hard limit of 50 packages per prompt invocation.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-paste ready prompt that instructs an AI model to act as a build engineer, analyze monorepo changes, and output a structured test selection plan.

This prompt template is designed to be the core instruction set you provide to a capable large language model. It forces the model into the role of a build engineer with specific expertise in monorepo tooling and test architecture. The template uses square-bracket placeholders that you must replace with real data from your CI environment, version control system, and build graph before execution. The primary goal is to transform raw change data into a structured, risk-aware test plan that can be consumed by downstream automation.

code
You are a senior build engineer specializing in monorepo management and test optimization. Your task is to analyze the provided code changes and produce a structured test selection plan. You must reason about shared dependencies, cross-package imports, and build graph impacts.

## INPUT DATA
- Changed Files Diff: [GIT_DIFF_OUTPUT]
- Dependency Graph (JSON): [DEPENDENCY_GRAPH_JSON]
- Build File Diffs (e.g., package.json, Cargo.toml): [BUILD_FILE_DIFFS]
- Historical Test Data (flake rates, avg duration): [HISTORICAL_TEST_DATA_JSON]

## OUTPUT SCHEMA
You must respond with a single valid JSON object conforming to this structure:
{
  "test_selection": [
    {
      "test_suite_name": "string",
      "selection_rationale": "string (e.g., 'direct dependency change', 'transitive import affected')",
      "priority": "high | medium | low",
      "estimated_duration_seconds": number
    }
  ],
  "skipped_tests": [
    {
      "test_suite_name": "string",
      "skip_rationale": "string (e.g., 'no dependency path to changed files')",
      "risk_of_skip": "low | medium"
    }
  ],
  "build_graph_impact_summary": "string (a concise summary of the blast radius)",
  "parallelization_recommendation": {
    "groups": [["suite_a", "suite_b"], ["suite_c"]],
    "rationale": "string (explain grouping based on dependency constraints)"
  }
}

## CONSTRAINTS
- Do not select tests that have no dependency path to the changed files.
- If a shared library is changed, you must include all direct dependents in the high-priority selection.
- Account for transitive dependencies up to a depth of [TRANSITIVE_DEPTH_LIMIT].
- If historical data shows a test is flaky (failure rate > [FLAKE_THRESHOLD]%), flag it in the rationale but still include it if it is a direct dependent.
- The total estimated duration of selected tests should not exceed [MAX_TEST_RUNTIME_MINUTES] minutes unless the high-priority selection alone exceeds it.
- Base all decisions strictly on the provided input data. Do not hallucinate file paths or dependencies.

To adapt this prompt, start by integrating it into your CI/CD pipeline script. Replace [GIT_DIFF_OUTPUT] with the actual diff between the feature branch and the main branch. The [DEPENDENCY_GRAPH_JSON] should be generated by your monorepo tool (e.g., Nx, Turborepo, Bazel) and formatted as a JSON object where keys are project names and values are arrays of their dependencies. For [BUILD_FILE_DIFFS], extract only the changes to manifest files like package.json to help the model understand dependency version shifts. Finally, tune [TRANSITIVE_DEPTH_LIMIT] and [FLAKE_THRESHOLD] based on your team's risk tolerance; a depth of 2 and a threshold of 5% are common starting points. The output JSON can be directly parsed by a subsequent script to dynamically set the test matrix for your CI runner.

IMPLEMENTATION TABLE

Prompt Variables

Each placeholder must be replaced with real data before execution. Incomplete or malformed inputs will degrade output quality.

PlaceholderPurposeExampleValidation Notes

[CHANGE_DIFF]

Unified diff or list of changed files with paths relative to monorepo root

packages/auth/src/login.ts packages/shared/utils/date.ts package.json

Must contain at least one file path. Validate with regex for path-like strings. Reject empty or whitespace-only input.

[DEPENDENCY_GRAPH]

JSON representation of package dependency relationships across the monorepo

{"packages/auth": {"dependsOn": ["packages/shared"]}, "packages/shared": {"dependsOn": []}}

Must be valid JSON with a top-level object. Each key must map to an object with a dependsOn array. Circular dependencies should be flagged but not rejected.

[BUILD_FILE_PATTERNS]

List of glob patterns identifying build configuration files to scan for changes

["/package.json", "/tsconfig.json", "/Dockerfile", "/Makefile"]

Must be a valid JSON array of strings. Each string must be a valid glob pattern. Empty array means no build file scanning.

[TEST_SUITE_MAP]

Mapping of test suites to the packages or paths they cover

{"auth-unit": {"path": "packages/auth/tests", "covers": ["packages/auth"]}, "shared-integration": {"path": "tests/integration/shared", "covers": ["packages/shared", "packages/auth"]}}

Must be valid JSON. Each suite entry requires path and covers fields. covers must be a non-empty array of package paths. Reject if any suite has zero coverage targets.

[PARALLELIZATION_CONSTRAINTS]

Resource limits and execution environment constraints for test parallelization

{"max_workers": 8, "memory_per_worker_mb": 2048, "exclusive_resources": ["database", "redis"]}

Must be valid JSON. max_workers must be a positive integer. exclusive_resources must be an array of strings. Omit or set to null if no constraints.

[HISTORICAL_FAILURE_DATA]

Optional record of recent test failure history per suite for risk weighting

{"auth-unit": {"failure_rate_7d": 0.02, "last_failure": "2025-03-28"}, "shared-integration": {"failure_rate_7d": 0.15, "last_failure": "2025-03-30"}}

Optional. If provided, must be valid JSON with suite names as keys. Each value must have failure_rate_7d as a float between 0 and 1. Null allowed if no history available.

[OUTPUT_FORMAT]

Desired structure for the test selection output

json

Must be one of: json, markdown, or summary. Default to json if omitted. Reject unknown format values.

[RUNTIME_BUDGET_MINUTES]

Optional maximum total runtime in minutes for selected tests

45

Optional. If provided, must be a positive integer or float. Null allowed. When set, the prompt must prioritize tests to fit within the budget.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the monorepo affected test detection prompt into a CI pipeline with validation, retries, and audit logging.

This prompt is designed to run as a gate in your CI/CD pipeline after dependency changes are detected but before a full test suite execution. The typical integration point is a GitHub Actions workflow, Jenkins pipeline, or Buildkite step that triggers on pull requests or merges to main. The prompt consumes a structured diff of changed files, a dependency graph extracted from your monorepo's package manifests (e.g., package.json, Cargo.toml, build.gradle), and an optional test-to-module mapping file. The output is a JSON test selection plan that your test orchestrator reads to decide which test targets to execute and in what order.

Validation and retry logic is critical because an incorrect test selection can skip tests that would have caught a regression. Implement a JSON schema validator that checks the output for required fields: affected_modules, selected_tests, excluded_tests_with_rationale, parallelization_groups, and confidence_score. Each excluded test must include a rationale string and a risk_level enum (low, medium, high). If validation fails, retry the prompt once with the validation errors appended to the [CONSTRAINTS] block. If the second attempt also fails validation, fall back to running the full test suite and log an alert to your observability platform. For high-risk repositories (e.g., payment processing, auth services), configure a human approval step when the confidence_score falls below 0.85 or when any excluded test carries a high risk_level.

Model choice and latency matter in CI pipelines where developers are waiting. Use a fast model (e.g., Claude 3.5 Haiku, GPT-4o-mini) for the initial selection pass. Reserve larger models for the fallback retry path. Cache the dependency graph and test mapping files as pipeline artifacts to avoid regenerating them on every run. The prompt's [INPUT] should be a pre-computed JSON object containing changed_files, dependency_graph, and test_module_map—do not pass raw file contents or git diffs directly. Build a preprocessing step that parses your monorepo's package manifests and produces a normalized dependency graph with explicit depends_on and used_by edges. This preprocessing is the most common failure point: if the graph is incomplete, the prompt will produce a plausible but incorrect test selection. Validate the graph's completeness by checking that every changed file maps to at least one module in the graph before invoking the prompt.

Logging and auditability are non-negotiable for compliance and incident review. Log the full prompt input, output, validation result, retry count, and final test selection decision to your CI artifacts or a dedicated audit store. Include a selection_id UUID in the output that ties the prompt's decision to the specific pipeline run. When a defect escapes to production, this audit trail lets you trace whether the prompt excluded the relevant test and why. Wire the output into your test reporting dashboard so teams can see which tests were skipped and the stated rationale. Finally, run a weekly eval sweep using historical commits where you know which tests should have been selected—compare the prompt's recommendations against ground truth and track precision/recall over time. Degradation in these metrics is your signal to update the dependency graph extraction logic or adjust the prompt's risk thresholds.

IMPLEMENTATION TABLE

Expected Output Contract

The model must return a JSON object matching this structure. Validate these fields before using the output in your pipeline.

Field or ElementType or FormatRequiredValidation Rule

affected_tests

Array of objects

Must be a non-empty array. Each object must contain test_suite, test_file, and rationale fields.

affected_tests[].test_suite

String

Must match a known test suite name from the [TEST_CATALOG] input. Regex: ^[a-zA-Z0-9_-/]+$

affected_tests[].test_file

String

Must be a valid relative file path ending in a recognized test file extension (e.g., .test.ts, _test.py, Spec.scala). Validate against [REPO_STRUCTURE] if provided.

affected_tests[].rationale

String

Must contain a non-empty explanation linking the test to a specific changed file or dependency from [CHANGE_SET]. Minimum 20 characters.

impact_summary

Object

Must contain total_affected_modules, dependency_graph_depth, and cross_package_imports fields.

impact_summary.total_affected_modules

Integer

Must be a positive integer. Validate that count does not exceed total modules in [DEPENDENCY_GRAPH].

impact_summary.dependency_graph_depth

Integer

Must be a non-negative integer. If 0, confirm no transitive dependencies are affected. Max depth should not exceed [MAX_DEPTH] constraint if provided.

impact_summary.cross_package_imports

Array of strings

Each string must represent a package import path (e.g., @scope/package). Validate against [PACKAGE_REGISTRY] if provided.

parallelization_recommendation

Object

Must contain strategy and estimated_time_savings_percent fields.

parallelization_recommendation.strategy

String (enum)

Must be one of: 'none', 'by-suite', 'by-module', 'by-dependency-layer'. Validate against allowed enum values.

parallelization_recommendation.estimated_time_savings_percent

Number

Must be a float between 0.0 and 100.0. If strategy is 'none', value must be 0.0.

skipped_tests

Array of objects

If present, each object must contain test_suite, test_file, and skip_reason. skip_reason must be one of: 'no-dependency-change', 'flaky-test-excluded', 'out-of-scope'.

PRACTICAL GUARDRAILS

Common Failure Modes

What breaks first when using a monorepo affected test detection prompt in production, and how to guard against each failure.

01

Incomplete Dependency Graph

What to watch: The prompt misses cross-package imports because the dependency graph is stale, incomplete, or ignores dynamic imports and peer dependencies. This causes affected tests to be skipped, leading to undetected regressions. Guardrail: Require the prompt to list all dependency sources it consulted (package.json, lock files, build graph exports) and flag any packages where the graph was unavailable or inferred. Add a manual override for teams to inject known-but-missing edges.

02

Over-Selection and Test Suite Bloat

What to watch: The prompt selects too many tests because it follows transitive dependencies too deeply or treats every shared utility change as high-risk. This defeats the purpose of affected test detection by running nearly the full suite. Guardrail: Add a configurable [MAX_TRANSITIVE_DEPTH] parameter and require the prompt to categorize tests by impact tier (direct, indirect, low-risk transitive). Include a runtime budget constraint that forces explicit trade-offs when the selection exceeds the budget.

03

Silent Ignorance of Non-Code Changes

What to watch: The prompt analyzes only source code diffs but ignores configuration changes, environment variable updates, CI script modifications, or Dockerfile changes that can break tests. Tests pass against the wrong configuration and fail in production. Guardrail: Expand the input schema to include [CONFIG_DIFF], [INFRA_DIFF], and [BUILD_FILE_DIFF] as separate inputs. Require the prompt to explicitly state which change categories it considered and which it excluded, with a confidence penalty for excluded categories.

04

Parallelization Conflicts from Shared State

What to watch: The prompt recommends parallel test execution groups that conflict on shared databases, file systems, or ports. Tests fail not because of code defects but because of resource contention introduced by the parallelization plan. Guardrail: Require the prompt to output a [RESOURCE_CONFLICT_MATRIX] that identifies tests sharing mutable state. Add a rule that tests with overlapping resource claims must be serialized or isolated, and flag any parallel group where isolation cannot be confirmed.

05

Stale Test-to-Module Mappings

What to watch: The prompt relies on a static test-to-module mapping that hasn't been updated after refactoring or test reorganization. Tests are mapped to the wrong packages, causing false negatives (skipped tests that should run) or false positives (tests run unnecessarily). Guardrail: Add a validation step where the prompt cross-references its test selection against recent test execution history and flags tests whose module affiliation has changed. Include a [MAPPING_CONFIDENCE] score per test and require human review for low-confidence mappings.

06

Ignoring Cross-Package Type and Interface Changes

What to watch: The prompt detects direct import changes but misses breaking changes to shared TypeScript types, protobuf definitions, or GraphQL schemas that propagate silently across packages. Downstream consumers compile successfully but behave incorrectly at runtime. Guardrail: Add [SHARED_TYPE_DIFF] and [SCHEMA_DIFF] as required inputs. Require the prompt to trace type usage across package boundaries and flag any consumer that imports a changed type, even if the import path itself didn't change.

IMPLEMENTATION TABLE

Evaluation Rubric

How to test output quality before shipping this prompt into your CI pipeline. Run these checks against a known set of changes with expected test selections.

CriterionPass StandardFailure SignalTest Method

Change-to-Test Traceability

Every selected test maps to at least one changed file, shared dependency, or cross-package import in the diff

Selected test has no traceable link to any changed artifact; rationale field is empty or circular

Manual audit: for each recommended test, verify the dependency path from the diff using the build graph

False-Negative Risk Disclosure

Prompt output includes an explicit list of modules or test suites that were NOT selected, with a risk justification for each exclusion

Output contains only selected tests with no mention of what was excluded or why; risk section is missing or boilerplate

Check for presence of an ExcludedTests or RiskDisclosure section in the structured output; verify at least one exclusion rationale references a specific module

Cross-Package Import Coverage

All cross-package imports in the diff are represented in the test selection, including transitive dependents up to [MAX_DEPTH] levels

A changed package with known downstream consumers has zero tests selected for those consumers

Pre-build a dependency graph fixture with known cross-package imports; verify that every changed import edge has a corresponding test in the output

Build Graph Impact Summary Completeness

Output includes a build graph impact summary listing affected packages, their dependents, and the depth of impact propagation

Impact summary is missing, lists only changed packages without dependents, or contains packages not present in the provided build graph

Parse the output's ImpactSummary field; diff against the input build graph to confirm all affected nodes are present and no hallucinated nodes appear

Parallelization Recommendation Validity

Parallelization groups are mutually exclusive (no test appears in two groups) and respect the dependency order from the build graph

Same test appears in multiple parallel groups; groups suggest parallelizing tests that have a known serial dependency

Extract parallelization groups from the output; run a set intersection check for duplicates; validate group ordering against the build graph's topological sort

Placeholder and Input Adherence

All required placeholders ([DIFF], [BUILD_GRAPH], [PACKAGE_FILES], [MAX_DEPTH]) are consumed; output does not reference data outside the provided inputs

Output references files, packages, or tests not present in any input; selection rationale cites external knowledge

Grep the output for file paths and package names; cross-reference against the input payload; flag any string not found in the input set

Output Schema Compliance

Output is valid JSON matching the [OUTPUT_SCHEMA] with all required fields present and no extra top-level keys

JSON parse failure; missing required field such as SelectedTests or ImpactSummary; unexpected null where a list is required

Validate output against the JSON Schema definition; check required field presence, correct types, and no additional properties at the top level

Runtime Budget Constraint

Estimated total runtime of selected tests does not exceed [RUNTIME_BUDGET] when provided; if exceeded, output includes a risk-ranked subset within budget

Total estimated runtime exceeds budget with no explanation or fallback subset; runtime estimates are missing or clearly fabricated

Sum the EstimatedRuntimeMs field across all selected tests; compare to [RUNTIME_BUDGET]; verify fallback subset is present and risk-ranked when budget is exceeded

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a single model call and lighter validation. Focus on getting a reasonable test list from a diff or change description. Accept a simple JSON array of test paths without requiring confidence scores or traceability.

code
You are analyzing a monorepo change. Given the [DIFF] or [CHANGED_FILES] list, return a JSON array of affected test file paths that should run.

Return only: { "affected_tests": ["path/to/test.spec.ts", ...] }

Watch for

  • Missing cross-package imports that aren't obvious from file paths alone
  • Overly broad selections when the model doesn't understand the build graph
  • No explanation for why a test was selected, making manual review harder
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.