Skip to content

Two Constants, One Silent Lie: Catching Drift with ast Instead of Imports

Adityo Guni Waluyo

A constant that projects from another constant drifts the day the source changes. ast.parse compares them without executing a line.

TL;DR

A new CLOSE_ACTIONS value slipped through deploys because the kernel's STATE_ACTIONS in another file never learned about it. Importing the wrapper risks side effects and copied comments rot, so a shell script parses both files as text using Python's ast module. It checks CLOSE_ACTIONS against the expected projection and flags any drift.

Two constants, one silent lie

I added a new value to CLOSE_ACTIONS in the wrapper file. The deploy went green, the tests passed, nothing crashed. Weeks later a job that had been closed for days still showed up as active. The culprit was quiet: the kernel's STATE_ACTIONS in a different file never learned about the new value. The two constants had drifted apart, and nothing in the pipeline was built to notice.

My first instinct was the wrong one: import the wrapper module and read its constant. That drags import-time side effects into a place that should stay isolated. The second instinct was copy-pasting the list into a comment next to the kernel constant, and that is guaranteed rot, because the day someone updates the code and forgets the comment, the comment becomes an authoritative lie.

Read the file, never run it

The fix in commit 0bac516 (check-close-actions-drift.sh) treats both files as text. No import, no execution. The Python ast module does the work: ast.parse(source) turns source text into a syntax tree without executing the module [1].

Walking that tree is next, and the docs are explicit that ast.walk(node) yields descendants in no specified order [1]. Order is not a guarantee here, so the extraction collects matches into a set, and the comparison at the end is set equality, never list equality.

The extraction itself leans on ast.literal_eval, which only evaluates literal structures: strings, numbers, tuples, lists, dicts, sets [1]. Put a function call inside the constant and it raises instead of running anything. That restriction is the whole safety story: the script pulls STATE_ACTIONS from the kernel and CLOSE_ACTIONS from the wrapper as plain data. Python set displays, the curly-brace literal form, yield a new mutable set [3], which is exactly the shape the script extracts.

Projection, not duplication

The two constants are not supposed to be equal. The wrapper's close set is a projection of the kernel's state set, and the script encodes that rule explicitly:

# idea from check-close-actions-drift.sh
# expected = STATE_ACTIONS - EXTRACT_TIME - NONSTATE
# drift    -> print extra/missing, exit 1
# unreadable input -> exit 2
# in sync  -> exit 0

The kernel tracks every state transition; the wrapper only cares about actions that terminate a job. Subtracting the extract-time and non-state actions leaves exactly the set the wrapper is allowed to close with. If the real CLOSE_ACTIONS differs from that projection by even one value, the script prints the extras and the missing ones, then exits 1. An unreadable input exits 2, which lines up with the Unix habit of reserving 2 for usage errors and 1 for everything else [2].

The first honest run of the checker flagged a known skip-extra drift immediately, and that is the whole point. The tool paid for itself on day one, and from then on the projection rule lives in one executable place instead of in somebody's memory of two files.

Sources

Related articles