Skip to content
Start free

See and undo a change to the report's code

Put the report's contract under version control, let an AI quietly loosen a rule, see exactly what changed, and put it back.

2 parts · 3 and 11 min · part 2 on a computer

Free course. Your Maestro, an AI, .

On the previous page, changing a single order amount in September's data changed the report's fingerprint while leaving no clue which row had been touched. The same hazard applies to the itself. When you ask an AI assistant to tidy up contract.py or report.py, it can rewrite a condition while changing a variable name or formatting a loop. Because the edited file still runs from start to finish without raising an error, everything looks fine until a bad row passes straight into finance's totals.

Version control protects reproducibility by keeping your code anchored to a known-good state through three moves:

  1. Commit an approved version. Once finance confirms that the report and its rules behave as expected, you record a snapshot of the files along with a message stating why that version matters, such as "Contract as finance approved it". Keeping a separate folder named project_backup_old only leaves you guessing which file changed and when; a commit ties the exact file contents to an ordered log entry right inside your project. Committing also stays local on your computer, so recording a version never publishes or sends your files anywhere else.
  2. See what changed (diff). Whenever you or an AI edits a file, you compare the working file in your folder against the last committed snapshot. A unified diff ignores every unchanged part of the file and prints only the lines that were removed (marked with -) and the lines that replaced them (marked with +), framed by a few neighboring lines so you can see where the edit sits.
  3. Restore the committed version. When the diff reveals an unwanted edit, you copy the committed snapshot back over the working file. Diffing again returns an empty result, confirming that the code on disk matches the approved version byte for byte.

How version_demo.py shows the three moves

Because git is not installed in this course sandbox, version_demo.py implements those same three moves in plain inside a temporary working folder:

  • commit(message) creates a numbered snapshot directory inside versions/, copies the current contract.py into versions/1/contract.py, and appends "1 Contract as finance approved it" to log.
  • Right after that commit, the script simulates an AI edit that rewrites if amount < 0: as if amount < -1000: in the working copy of contract.py. The code still runs cleanly, yet any negative order amount between -1000 and 0 would now slip past the contract without a warning.
  • diff() runs difflib.unified_diff between committed/contract.py and working/contract.py, printing the exact line where the threshold moved.

If the - and + lines in the unified diff output look unfamiliar, ask your Maestro to walk through them with you.

Try it

Run version_demo.py first to see the unified diff of the AI's edit to contract.py. Then complete the bottom of version_demo.py to copy version 1 from versions/1/contract.py back over path, print the difference again, and print log.

A good place to stop. Up next: the workbench · 11 min · on a computer

Your turn

Run version_demo.py: it records the approved contract as version 1, then an AI loosens one rule, and the script prints the line-by-line difference. Where the comment says, restore the committed version, then print the difference again and the log. You're done when the difference is empty and you can say what the AI's change would have let through.

When your restore step works, the script prints the unified diff showing - if amount < 0: and + if amount < -1000:, followed by Difference after restore: '' and Log: ['1 Contract as finance approved it']. Copy the committed file back with shutil.copy(os.path.join(versions, "1", "contract.py"), path) and print repr(diff()) so the empty string '' is visible. If your difference after restore is still not empty, paste your last three lines to your Maestro.

The same moves with git on your own machine

On your own computer, you run git in a inside the project folder instead of writing a Python helper. Each function in version_demo.py corresponds to a standard git command:

  • git init starts version tracking in the current folder, and git add contract.py report.py followed by git commit -m "Contract as finance approved it" records the approved snapshot.
  • git diff prints the exact line-by-line unified diff between the committed snapshot and your working files.
  • git restore contract.py overwrites the working contract.py with the last committed snapshot, undoing the AI's edit.
  • git log --oneline prints the one-line history of commits in the folder.
Terminal
git initgit add contract.py report.pygit commit -m "Contract as finance approved it"git diffgit restore contract.pygit log --oneline
The equivalent git commands to run in a terminal on your own machine (not run in this course sandbox, where git is not installed).

Check yourself: After an AI assistant tidies contract.py, the report script still runs without errors. Before running next month's report, you check git diff (or diff() in the sandbox). What does that step catch that running the script alone misses?