Repository navigation
Description
Problem Description
devops tf drift always exits 0. OpenTofu check blocks only give warnings. The drift plan takes the state lock (analysis.py:417).
Order 5 of the plan in #5311. The owner approved the plan on 2026-11-13.
Reproduction Steps
- Run
devops tf drift <stack>in an initialized stack where a resource differs from the state. - Read the exit code with
echo $?. - Add a check block that fails, and run
devops tf drift <stack>again.
Expected vs Actual Behavior
Expected: exit 2 on drift, a non-zero exit on a failed or unknown check, and -lock=false passes to tofu so that the drift plan does not take the state lock.
Actual: tf_drift in src/devops_cli/commands/tf.py prints the drift table and returns, so the exit code is 0. read_plan_drift (src/devops_cli/tf/analysis.py:417) runs tofu plan -refresh-only -json with no way to add -lock=false, and it does not read the exit code of tofu.
Environment Diagnostics
devops-cli 0.124.0. Found by a read of the source on 2026-11-13. tofu is not installed in the devcontainer yet (#5317), so there is no live run.
Proposed Solution
Exit 2 on drift. Read .checks from 'tofu show -json' of the saved plan, and exit non-zero on a failed or unknown check. Pass extra arguments such as -lock=false to tofu.
Existing Tool Considered
tofu does all of the work. No open tool adds devops pacing, the agent refusal or a dry run with zero requests.
Cost: Build: about 2.0M tokens and 3 days in 3 PRs (pass-through 1.2M, drift fix 0.3M, guard 0.5M), test first. Keep: about 50k tokens a month. Fix: about 200k tokens for each break. This item is the drift part: about 0.3M tokens.
Acceptance Criteria
- The PR cites a fail-to-pass run.
- Tests use recorded plan JSON for four cases: clean, drift, failed check and unknown check.
Dependencies
Source: #5311
Alternatives check of 2026-11-22: in-use.
The repository already uses this tool.
Tool: opentofu/opentofu (https://github.com/opentofu/opentofu), MPL-2.0, last activity 2026-11-21, maintained.
Reason: Searches were degraded and returned no useful tool pages, but the repository facts and the issue's own read of OpenTofu show it supplies every needed primitive: detailed exit codes, unlocked refresh plans and saved-plan check results.
Covers:
-detailed-exitcodereturns 2 when the plan holds changes-lock=falseruns the drift plan without the state locktofu show -jsonof the saved plan holds the check results
Lacks:
- No CLI wiring that maps plan results to devops exit codes
- No drift table or in-sync message for operators
- No serialized
--format jsondrift report
Proposed design
Problem statement
devops tf drift always exits 0. The command prints the drift table or the in-sync line and returns. The refresh-only plan takes the state lock and ignores the exit code of tofu. This change saves the plan and reads its checks. It exits 2 on drift and 1 on a failed or unknown check or a plan error. It runs the plan with -lock=false.
Acceptance criteria
Requirement: Drift plan command flags
The drift plan SHALL run tofu plan -refresh-only -detailed-exitcode -lock=false -out=<plan file> and SHALL NOT pass -json.
Scenario: Drift plan argv
- WHEN the test calls
read_plan_drifton an initialized directory whose process fake records the plan call - THEN the test asserts that the recorded argv holds
plan,-refresh-only,-detailed-exitcode,-lock=falseand one-out=argument, and holds no-json.
Requirement: Check results read from the saved plan
read_plan_drift MUST read the top-level checks array of tofu show -json <plan file>.
Scenario: Failed and unknown checks
- WHEN the test gives
read_plan_drifta process fake whose plan run succeeds and whosetofu show -json <plan file>answer holds onefailcheck and oneunknowncheck - THEN the test asserts that
failed_checksholds the failed check'saddress.to_displayandunknown_checksholds the unknown check'saddress.to_display.
Requirement: Exit 2 on drift
devops tf drift SHALL exit 2 when the plan reports changes or the drift report holds any divergence.
Scenario: Recorded replay with drift
- WHEN the test scripts the plan to return exit code 2 and the saved-plan show to hold one drifted address
- THEN the test asserts
result.exit_code == 2and the output holds the drifted address andattribute changed.
Requirement: Exit 0 on a clean stack
devops tf drift SHALL exit 0 when the plan returns 0, each check passes and the drift report holds no divergence.
Scenario: No drift
- WHEN the test scripts the plan to return exit code 0 and the saved-plan show to hold no
resource_driftand onlypasschecks - THEN the test asserts
result.exit_code == 0and the output holds the in-sync message.
Requirement: Exit 1 on a failed check
devops tf drift SHALL exit 1 when a check result is fail or error.
Scenario: Failed and error checks
- WHEN the test scripts the plan to return exit code 0 and the saved-plan show to hold one check with status
failand one check with statuserror - THEN the test asserts
result.exit_code == 1and the output holds both checks'address.to_displayvalues.
Requirement: Exit 1 on an unknown check
devops tf drift SHALL exit 1 when a check result is unknown.
Scenario: Unknown check
- WHEN the test scripts the plan to return exit code 0 and the saved-plan show to hold one check with status
unknown - THEN the test asserts
result.exit_code == 1and the output holds the unknown check'saddress.to_display.
Requirement: Exit 1 on a plan error
devops tf drift SHALL exit 1 when tofu plan returns an exit code other than 0 or 2.
Scenario: Plan error
- WHEN the test scripts
tofu planto return exit code 1 with stderrError: backend unavailable - THEN the test asserts that
read_plan_driftreturnsplan_returncode1 andplan_errorError: backend unavailable. It asserts that the process fake recorded notofu showof a plan file and thatdevops tf driftexits 1 with that error in the output.
Requirement: Exit 1 wins over drift
devops tf drift SHALL exit 1 when a check fails or is unknown even if the plan reports drift.
Scenario: Failed check with drift
- WHEN the test scripts the plan to return exit code 2 and the saved-plan show to hold one
failcheck and one drifted address - THEN the test asserts
result.exit_code == 1and the output holds the failed check's address.
Requirement: Serialized output keeps the exit code
devops tf drift --format json MUST emit the report and then exit 2 on drift.
Scenario: JSON output on drift
- WHEN the test runs
devops tf drift --format jsonwith a plan that returns exit code 2 and one drifted address - THEN the test asserts that the emitted payload holds the drifted address and that the command exits 2.
Requirement: Uninitialized directory result
read_plan_drift SHALL return an empty PlanDriftResult when the directory has no .terraform directory.
Scenario: Uninitialized directory
- WHEN the test calls
read_plan_drifton a directory without a.terraformdirectory - THEN the test asserts that
drifted,failed_checksandunknown_checksare empty,plan_returncodeis 0, and the process fake recorded no call.
Requirement: Exit codes in the command help
The devops tf drift help SHALL state the exit codes 0, 1 and 2 and their meanings.
Scenario: Help text
- WHEN the test runs
devops tf drift --help - THEN the output states that exit code 0 means no drift, 1 a failed check or plan error, and 2 drift.
Requirement: Generated documentation stays current
The generated command page MUST hold the revised devops tf drift help.
Scenario: Documentation freshness
- WHEN the author runs
uv run devops docs generate - THEN
uv run devops ci --check --only docspasses.
Existing Tool Considered
opentofu/opentofu (MPL-2.0) is in use; the alternatives check in the issue body records what it covers and lacks. This item adds the CLI wiring that maps the plan return code and the saved-plan checks results to the devops exit codes. The command keeps the existing drift table and --format json report.
Key questions
- Why does
devops tf driftexit 0 today?:tf_driftprints the drift table or the in-sync line and returns. The serialized branch returns afteremit_serialized.read_plan_driftrunstofu plan -refresh-only -jsonat line 417. It returnsparse_plan_drift(res.stdout)at line 424 without readingres.returncode. [sources: src/devops_cli/commands/tf.py:609, src/devops_cli/commands/tf.py:615, src/devops_cli/commands/tf.py:617, src/devops_cli/commands/tf.py:623, src/devops_cli/tf/analysis.py:411, src/devops_cli/tf/analysis.py:417, src/devops_cli/tf/analysis.py:424] - How does the CLI learn that the plan holds drift?: Add
-detailed-exitcodeto the refresh-only plan and read exit code 2 as a drift signal. The command also exits 2 when the drift report'sdrift_detectedis true. That covers a declared resource that is absent from state, which a refresh-only plan can return as 0. (best practice) [sources: src/devops_cli/tf/analysis.py:388, src/devops_cli/tf/analysis.py:417]- Chosen: detailed-exitcode and report divergence. OpenTofu returns 2 for a plan with changes. The report covers a declared resource that is absent from state, which the plan code alone misses.
- Rejected: detailed-exitcode only. A refresh-only plan returns 0 for a declared resource that is absent from state. The command would then print a drift table and exit 0.
- Rejected: Parse the
tofu plan -jsonstream. The stream holds UI messages; the saved plan'sresource_driftis the stable structure. - Rejected: Compare resources in Python. Only the provider refresh shows attribute drift, and the repository already runs it.
- How does the CLI read the check results?: Save the plan with
-out=<temporary file>. Runtofu show -json <file>in the stack directory. Read the top-levelchecksarray. Each entry names the check ataddress.to_displayand givesstatusaspass,fail,errororunknown.failanderrorgo tofailed_checks;unknowngoes tounknown_checks. Delete the temporary directory in afinallyblock. (best practice) [sources: src/devops_cli/tf/analysis.py:411]- Chosen: Saved plan and
tofu show -json. The plan JSON representation holds thechecksresults and the exact plan that the exit code describes. - Rejected: Parse the
tofu plan -jsonstream. The stream holds UI messages, not thechecksarray. - Rejected: Parse the text output of
tofu plan. The warning text has no stable format and no check address.
- Chosen: Saved plan and
- Which exit codes does the command use, and which code wins when drift and a failed check occur together?: Use 0 when the plan returns 0, each check passes and the report holds no divergence. Use 1 for a failed or unknown check or a plan error. Use 2 for drift. A failed or unknown check wins over drift, because the plan cannot be trusted to describe the drift. The report is printed before the exit, as
devops config audit-keysdoes. (best practice) [sources: openspec/changes/archive/2026-10-28-1469-audit-keys-leak-exit-code/design.md:20, openspec/changes/archive/2026-10-28-1469-audit-keys-leak-exit-code/design.md:32, src/devops_cli/commands/tf.py:615]- Chosen: 1 for a check, 2 for drift, check wins. It keeps OpenTofu's 0/1/2 plan codes, reports a check error before a drift finding, and follows the project's exit-1 gate of
config audit-keys. - Rejected: 2 for both. It hides the difference between a drift finding and a check error.
- Rejected: 3 for a check. No project convention gives 3, and OpenTofu's plan codes are 0, 1 and 2.
- Rejected: 2 for drift even when a check fails. A failed check means the plan is not trustworthy; the error code must win.
- Chosen: 1 for a check, 2 for drift, check wins. It keeps OpenTofu's 0/1/2 plan codes, reports a check error before a drift finding, and follows the project's exit-1 gate of
- How does the drift plan get
-lock=false?:read_plan_driftadds-lock=falseto the refresh-only plan each time. The drift plan reads state and changes nothing, so it must not hold the state lock. No command option exposes the lock. (best practice) [sources: src/devops_cli/tf/analysis.py:417]- Chosen: Always
-lock=false. A drift plan is read-only, and no caller needs the lock. - Rejected: A
--lock/--no-lockoption ondevops tf drift. The command never applies a plan, so the option has no use. - Rejected: A generic pass-through of tofu arguments. The parent item feat(gh): manage GitHub organization and repository settings as code #5311 plans the pass-through as its own pull request.
- Chosen: Always
- What does
read_plan_driftreturn totf_drift?: Add aPlanDriftResultmodel insrc/devops_cli/models/tf.py. It holdsdrifted,failed_checks,unknown_checks,plan_returncodeandplan_error. It follows the shape ofTFLintResultandTFOutputResult.tf_driftpassesdriftedtodetect_drift, prints the check lists and the plan error, and usesplan_returncodefor the exit. (best practice) [sources: src/devops_cli/models/tf.py:91, src/devops_cli/models/tf.py:62, src/devops_cli/commands/tf.py:33, src/devops_cli/tf/analysis.py:411]- Chosen: A result model. The models file already holds the result types that
tf.pyimports, and the typed fields keep the plan return code, drift and checks apart. - Rejected: A tuple of lists. A caller cannot tell the fields apart.
- Rejected: Raise on drift. Drift is data that the table shows, not an error.
- Chosen: A result model. The models file already holds the result types that
- Do the tests need a live
tofubinary?: No. ADR 0007 requires a test ofsrc/to start no program. The test doubles the process edge with recorded JSON for the four cases: clean, drift, failed check and unknown check. The image and CI do install OpenTofu 1.13.9. A live run would need a provider and a live stack and would not be repeatable. (best practice) [sources: docs/adr/0007-tests-double-only-process-edges.md:10, docs/adr/0007-tests-double-only-process-edges.md:30, tests/tf/test_tf_analysis.py:756, .github/workflows/ci.yml:80]- Chosen: Offline tests with recorded JSON. ADR 0007 forbids a test of
src/from starting a program, and the fixtures make the four cases repeatable. - Rejected: A live
tofurun in CI. It needs a provider and a live stack, and it is not repeatable offline.
- Chosen: Offline tests with recorded JSON. ADR 0007 forbids a test of
- How are the new exit codes documented?: The
devops tf driftdocstring states the 0, 1 and 2 meanings. The pull request regenerates the generated command page withuv run devops docs generate. Thedocsrow of the gate proves freshness withuv run devops ci --check --only docs. (best practice) [sources: docs/adr/0025-generated-documents-stay-in-git-and-the-project-brain-cites-them.md:19, docs/adr/0014-required-checks-gate-the-merge-and-the-pre-push-hook-runs-the-fast-static-checks.md:78, docs/commands/tf.md:217, src/devops_cli/commands/tf.py:583]- Chosen: State the codes in the docstring and regenerate. The project's generated command pages come from the docstrings, and the
docsrow fails a stale page. - Rejected: Leave the help unchanged. A caller cannot learn the exit-code contract from the command, and the issue's point is a CI gate.
- Chosen: State the codes in the docstring and regenerate. The project's generated command pages come from the docstrings, and the
Fit
Fits in one pull request.
Suspected block
None.
Activity
dan-petty commentedon Nov 13, 2026
tf, and .github/labels.yml declares no scope/tf label. Add the label to the file to give the scope to this item.Both report drift, but #5127 covers infra repo hosts while this covers terraform stacks via tf drift.devops-cli-service commentedon Nov 16, 2026
Alternatives check: in-use
Reason: The searches found that OpenTofu itself already provides every mechanism this item needs:
tofu plan -refresh-only -detailed-exitcodereturns exit code 2 when the plan holds changes (drift),-lock=falseruns the plan without taking the state lock, andtofu show -jsonof a saved plan exposes thechecksresults the CLI can read to set a non-zero exit on a failed or unknown check. The issue already names tofu as the engine the project runs ("tofu does all of the work", "passes to tofu", "tofu show -json"), so the item is wiring OpenTofu's own flags and JSON output rather than adopting a new tool.The repository already uses this tool.
Tool: opentofu/opentofu (https://github.com/opentofu/opentofu)
Covers:
tofu plan -refresh-only -detailed-exitcodereturns exit code 2 when the plan contains changes (drift), distinct from 0 (clean) and 1 (error)tofu plan -lock=falseruns the refresh-only plan without taking the state locktofu show -jsonof the saved plan exposes thechecksresults (pass / fail / unknown) for the CLI to read and turn into a non-zero exitTools considered:
Searches run:
Sources:
Checked on 2026-11-16. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 17, 2026
Refine cannot set this item to Ready at 1774ebf while the items below stay open. The next refine round looks at the item again when they close, or when a person replies in a comment or edits the body.
devops-cli-service commentedon Nov 25, 2026
Next step: the stage job writes the plan of the item. See the section Proposed design of the body.
chore(main): release 0.220.0 (#7199)
devops-cli-service commentedon Nov 25, 2026
Released in v0.220.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.220.0
devops-cli-service commentedon Nov 25, 2026
Live since 2026-11-25 17:24 UTC