DEV Community

Igor Ganapolsky
Igor Ganapolsky

Posted on

A receipt that says "nothing was changed" has to be true

Our Mac menu bar app has two buttons, Free Agent RAM and Stop Runaways. Each one runs a small Node.js script that finds agent processes eating memory or CPU, and stops them. When the script finishes, the app shows a receipt that lists what it did.

If Node.js isn't installed, the script can't run. The app handles that: it shows "Couldn't free agent memory" and says what went wrong. Before the 1.2.10 release it also did something else first. It ran xcrun simctl shutdown all, which shuts down every simulator on the Mac.

The order was the bug. In both button handlers, the call that shuts down simulators sat above the check for whether Node.js had run. A person without Node.js would click the button, see "Nothing was changed", and have every running simulator shut down anyway. The receipt was false. Nothing in the app could have told them otherwise, because the shutdown happened before the app looked at the result.

The change was moving one line below the check. This is the shape of it in the Swift handler for Free Agent RAM:

DispatchQueue.main.async {
    guard let self = self else { return }
-   self.reapSimulators()
    guard run.ran else {
        self.showProblem(action: "toolProblem", title: "Couldn't free agent memory", problem: run.problem ?? "Unknown error.")
        return
    }
+   self.reapSimulators()
Enter fullscreen mode Exit fullscreen mode

Stop Runaways got the same edit. With Node.js installed, behaviour is unchanged.

The fix is one line, but the lesson is about what keeps it fixed. Anyone who later adds a "quick cleanup" call above that guard puts the bug back, and no ordinary unit test sees it, because these handlers need AppKit to run. So the test reads the Swift source. For each of the two actions it takes everything before the guard run.ran else line and checks three things:

  1. Nothing in that stretch mentions reapSimulators, whether as a call, a value or an alias.
  2. Any other call made there is on a short allow-list, and each entry on it has to be shown not to change the Mac before it is added.
  3. reapSimulators still appears after the guard, so the fix can't be "delete the shutdown".

The third check matters more than it looks. A test that only says "no shutdown before the guard" passes happily if someone deletes the shutdown entirely.

We ran the new test against the file from the commit before the fix. It fails with freeAgentRam mentions reapSimulators before the Node.js guard. Against the fixed file it prints PASS (2/2 actions).

If you build a tool that does something and then reports what it did, check your own code for the same pattern:

  • Find every place you show "nothing changed", "no action needed" or an empty receipt.
  • Look at what runs before the check that decides that message. Anything with a side effect that sits above the check can make the message wrong.
  • Put the side effects after the check that proves the tool ran, and add a test that fails if they move back.

The app is open source and the guard code is public: https://github.com/IgorGanapolsky/mac-yolo-safeguards

Top comments (0)