Skip to content

xpd untangle

List the places where the code gets wrong which pieces change together Use xpd untangle before a refactor of a codebase and after each round of it. It lists the places where the code gets one question wrong, which is whether two pieces should change together. It reads the files and the git history, and it runs no code.

Group What is wrong Kinds
Hidden link Pieces must change together, and the code does not show it copy, changes-together, shared-state
Needless link Pieces are tied together and change for different reasons volatile-import, import-cycle, private-reach, vague-module
Surplus A piece has no reason of its own to change pass-through, unreferenced, constant-parameter
Special case Code exists for one situation, which the design did not account for one-value-branch, one-caller-parameter, swallowed-exception, sleep-or-retry, disabled-test

copy and changes-together are found in every programming language that ast-grep parses. The other kinds are read from Python. A finding is evidence, so judge it against the code before you change anything. The xpd-untangle skill, which xpd skill --json prints, holds the steps of a refactor that the counts measure.

Terminal window
xpd untangle [OPTIONS] [PATHS]...
xpd untangle <COMMAND>

xpd untangle --help ends with these lines.

Examples:
xpd untangle
xpd untangle src/ --format json
xpd untangle --kind copy,changes-together --limit 0
xpd untangle --since main
Command What it does
xpd untangle start Start a run: record the commit it starts from, the paths, line count, checks and optional benchmark of the project
xpd untangle plan Record the plan of a round: the fixes in order, and the findings that are right as they are
xpd untangle approve Record that the user said yes to a fix that changes what callers or users see
xpd untangle gate Gate a fix that is committed: run the checks, time --bench when set, count lines and compare the findings with the commit before it. Exit 1 when the fix is not kept
xpd untangle drop Record that a fix was given up, after its files were restored
xpd untangle status Print the run and the stage that comes next
xpd untangle finish End the run: compare findings, line count and benchmark timing with the start commit. Exit 1 while a fix is open or a new finding has no reason

<PATHS>...

Files or directories (directories respect .gitignore)

Default: ..

--kind <KIND>

List only the findings of these kinds, separated by commas.

Give it more than once to pass several values.

Value Meaning
copy The same code is written out in more than one place
changes-together Two files change in the same commits, and no import joins them
shared-state Code writes state that another module or a function's callers depend on
volatile-import A module imports a module that changes at least twice as often as it does
import-cycle Modules import one another in a circle
private-reach Code uses a private name of another module
vague-module A module named utils, helpers or common holds code with no single purpose
pass-through A function only passes its parameters to one call
unreferenced No file mentions a function or class apart from its definition
constant-parameter Every call gives a parameter the same value
one-value-branch A branch runs for one string value
one-caller-parameter One call alone passes a parameter that has a default
swallowed-exception An exception is caught and dropped
sleep-or-retry Code waits a fixed time, or runs again when it fails
disabled-test A test is skipped, or may fail while the run passes

--limit <LIMIT>

The most findings to list of each kind, strongest first. 0 lists every finding.

Default: 20.

--since <COMMIT>

Count the findings at this commit too, and print each count beside the count of today.

--format <FORMAT>

Output format. Default: text on a terminal, json otherwise.

Values: text, json.

shop/orders.py
import time
from shop import pricing
def load(path, strict=False):
with open(path) as handle:
return [row for row in handle if row or not strict]
def summarize(rows, kind):
if kind == "wholesale":
rows = rows[:10]
pricing.RATE = 0.2
try:
value = pricing.total(rows)
except ValueError:
pass
time.sleep(1)
return value
def first():
return load("a.csv")
def second():
return load("b.csv")
shop/pricing.py
from shop import orders
RATE = 0.25
def total(rows):
return sum(float(row) for row in rows) * RATE
def report():
return total(orders.first()), orders.second(), orders.summarize([], "retail")
Terminal window
$ touch shop/__init__.py
$ xpd untangle shop --format text
xpd untangle: 7 findings in 3 files, 3 of them Python, with 0 commits of history
No git history was read, so no finding rests on how the files changed.
Hidden links: pieces that must change together, where the code does not show it: 1
shared-state: 1
29ce6e4b shop/orders.py:14 This writes RATE of the module pricing from outside it.
Needless links: pieces tied together that change for different reasons: 1
import-cycle: 1
8de3d24b shop/orders.py:3, shop/pricing.py:1 These 2 modules import one another in a circle, so none of them can change alone.
Surplus: pieces with no reason of their own to change: 2
unreferenced: 1
c17ac665 shop/pricing.py:10 No Python file that was read mentions report, apart from this definition.
constant-parameter: 1
0bea3ced shop/orders.py:6 None of the 2 calls of load passes strict, so it always has its default.
Special cases: code for one situation, which the design did not account for: 3
one-value-branch: 1
ccd51d7f shop/orders.py:12 This branch runs for one value: kind == "wholesale".
swallowed-exception: 1
ebae19e6 shop/orders.py:17 This catches an exception and does nothing with it.
sleep-or-retry: 1
0c77beff shop/orders.py:19 This waits a fixed time for something else to finish.

Each finding has its id, its places and what was measured. --kind keeps some kinds, and --limit sets how many findings of each kind are listed, which is 20 without it and every one with 0.

Terminal window
$ xpd untangle shop --kind import-cycle,shared-state --format json | jq -c '.findings[] | {kind, places: [.places[] | "\(.path):\(.line)"]}'
{"kind":"shared-state","places":["shop/orders.py:14"]}
{"kind":"import-cycle","places":["shop/orders.py:3","shop/pricing.py:1"]}

The summary holds every count, also of the kinds that --kind leaves out of the list.

Terminal window
$ xpd untangle shop --format json | jq -c '.summary.groups[] | {group, count}'
{"group":"hidden-link","count":1}
{"group":"needless-link","count":1}
{"group":"surplus","count":2}
{"group":"special-case","count":3}
Kind What xpd reports
copy A whole definition or block of at least 5 lines and 60 tokens that is written out token for token in two or more places. Comments and layout are not compared
changes-together Two files that changed in the same commits at least 5 times, and in at least half of the commits of the one that changed less. A commit of more than 25 files is left out. A pair of Python files is listed only when neither imports the other
shared-state A write to a name of another module, or a function that writes a module level name through global
volatile-import A module that imports a module with at least twice as many commits and at least 8, counted from the first commit of the importer. A test and an __init__.py are left out
import-cycle Modules that import one another in a circle
private-reach An import or a read of a name of another module that starts with one underscore
vague-module What python/vague-module-name reports
pass-through What python/pass-through-function reports
unreferenced A function or class at the top of a module that no Python file mentions apart from its definition. A decorated definition, a test and a name that a string holds are left out
constant-parameter A parameter that none of two or more calls passes, or that each of them gives the same literal
one-value-branch An if without elif that compares something with one string. Each value is one finding with every place that checks it, and a value that many places check comes first. Tests are left out
one-caller-parameter A parameter with a default that one of three or more calls passes
swallowed-exception An except that only passes or continues, or contextlib.suppress
sleep-or-retry time.sleep, asyncio.sleep or a retry decorator, and in a test what python/test-sleep and python/test-rerun report
disabled-test What python/test-skip reports

The parameters of a function are judged only when every mention of its name is a call whose arguments xpd can match to them, so a function that is passed as a value is left alone. unreferenced cannot see a caller in another language or outside the paths, so check before you delete.

The count of each kind is the measure of a refactor, and each finding has an id that it keeps from commit to commit. --since <commit> reads the files of that commit too and compares.

In the JSON Meaning
was beside each count The count at the commit
"new": true on a finding The commit had no finding of this id, so the change added it
fixed The findings of the commit that are gone today
summary.new and summary.fixed How many of each
Terminal window
xpd untangle --since main

xpd writes the files of the commit to a temporary folder, so the working tree and the index stay as they are. An id is made from the kind and from what the finding is about, such as the file, the name or the value, and not from a line, so it stays the same when code moves within a file. It changes when the file is renamed. Of two findings that are alike in one file, the first in the file has the first id. Without git, changes-together and volatile-import report nothing, and --since fails.

xpd untangle start begins a refactor run, which xpd records in the git folder of the repository as .git/xpd-untangle.json. The run belongs to no session and to no agent tool. An agent judges, designs and edits, and xpd records what was decided, says which stage comes next, and gates each fix itself.

Stage Command What xpd does
Start xpd untangle start <paths> --check "<command>" Records the commit, the paths and the checks
Plan xpd untangle plan <file> Checks the fixes and the accepted findings against the ids of the ledger
Ask xpd untangle approve <fix> Records the user's yes to a fix that changes what callers or users see
Gate xpd untangle gate <fix> Runs the checks, compares the findings, and keeps the fix or exits 1
Give up xpd untangle drop <fix> --reason "<why>" Records the reason, and the run goes on
Resume xpd untangle status Prints the run and the stage that comes next
End xpd untangle finish Compares with the commit of the start
Terminal window
$ git init -q && git add -A && git -c user.name=xpd -c user.email=xpd@example.com commit -qm start
$ xpd untangle start shop --check "python3 -c 'import shop.orders'" --format json | jq -c '{findings: .run.was, checks: [.run.checks[].passed], next: .next.stage}'
{"findings":7,"checks":[true],"next":"plan"}

A plan names the fixes in order and the findings that are right as they are.

plan.json
{
"fixes": [{"id": "no-fixed-wait", "title": "Remove the fixed wait in summarize", "findings": ["SLEEP"]}],
"accepted": []
}
Terminal window
$ sed -i "s/SLEEP/$(xpd untangle shop --kind sleep-or-retry --format json | jq -r '.findings[0].id')/" plan.json
$ xpd untangle plan plan.json --format json | jq -c '.next.stage'
"fix"
$ sed -i '/time.sleep(1)/d' shop/orders.py && git -c user.name=xpd -c user.email=xpd@example.com commit -qam "Remove the fixed wait"
$ xpd untangle gate no-fixed-wait --format json | jq -c '.run.rounds[0][0] | {id, outcome, fixed: (.fixed | length), added: (.added | length)}'
{"id":"no-fixed-wait","outcome":"kept","fixed":1,"added":0}
$ xpd untangle finish --format json | jq -c '{finished: .run.finished}'
xpd: 7 findings at the start, 6 now: 1 fixed, 0 added, 6 left without a reason; lines 39 -> 38
{"finished":true}
Exit code Meaning
0 It printed the ledger, with or without findings, or the stage of a run was done
1 A gate did not keep its fix, or finish did not end the run
2 It could not run, as when --since names no commit