Skip to content

What a test changes for itself stays in its own task: no more shared statics in HeedCore - #279

Merged
Mpdreamz merged 2 commits into
mainfrom
fix/no-global-state
Nov 16, 2026
Merged

Mpdreamz merged 2 commits into
mainfrom
fix/no-global-state

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

Part 9 of the code-health plan (fix/no-global-state).

What was global, and what it is now

Was Now
Chats.localRoot, Paths.home, StartWorkForm.newSessionID, Clock.fixed (nonisolated(unsafe) static var) One HeedContext value. Code reads HeedContext.current: the task's own (HeedContext.with, used by tests), else the process's (HeedContext.pinProcess, behind a Mutex, called only by the shot runner and tutor check). The old names are read-only properties, so Clock.now's ~480 call sites are unchanged.
Remote.setTargets / setExtraOptions (ssh integration tests) Remote.with(targets:extraOptions:), a task local. The app keeps setRoutes for the configured routes.
GitEnvironment.isolate(), a one-way process switch the first git test turned on Set by the test bundle's constructor (HeedTestProcess.c) before any test runs; isolate() is removed.
GitEnvironmentTests used setenv("GIT_DIR", …) on the whole process GitEnvironment.$inheritedOverride for its own commands.

Why a task local plus a process value, not values passed in: the screenshot and tutor-check runs pin the fake world for the whole app, and SwiftUI draws from the run loop, outside any task, so that pin has to belong to the process. Tests need a change that only their own task tree sees, and @TaskLocal gives exactly that. Threading the values through every caller would not isolate anything further.

Looked at and left: PullTrustBook/WriteAccessBook/ToolLocator/TailscaleCLI (shared caches behind a Mutex or an actor, and no test changes them), GitHub.$executable and the other task locals, AuditTap/CallLog.install (only the app calls them), and HeedApp's main-actor statics.

Lint

.swiftlint.yml now has two custom rules. no_unsafe_static_var rejects a nonisolated(unsafe) static/class var in Sources, in either modifier order. tests_change_no_process_state rejects HeedContext.pinProcess, setenv/unsetenv/putenv, Remote.setRoutes, AuditTap.install and CallLog.install in Tests. I checked both rules with a probe file containing violations: 5 of 5 were flagged.

Verification

  • mise run fmt, mise run lint clean. mise run test: 2546 tests passed.
  • HeedContextTests reads a value while another task's override is in effect. I changed HeedContext.with to set the process value, as the statics did, and both of its expectations failed. I changed Remote.with to set the global routes, and that test failed too. Both changes are reverted.
  • GitEnvironmentTests still expects the commit author heed test, which only the constructor now sets.

Not tested

  • The ssh integration tests (RemoteIntegrationTests, RemoteSessionsIntegrationTests) need HEED_SSH_TEST. They compile but did not run.
  • I did not run mise run shots or the tutor check, which are the pinProcess callers. The pinned values are the same as before.
  • I did not run mise run live.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the bug Something isn't working label Nov 16, 2026
Mpdreamz and others added 2 commits November 16, 2026 17:37
…statics in HeedCore

Chats.localRoot, Paths.home, StartWorkForm.newSessionID and Clock.fixed were nonisolated(unsafe) statics.
ChatStartTests swapped the chats root while other suites ran beside it and read it. They are now one value,
HeedContext (home, chats root, pinned time, session id maker). Code reads HeedContext.current: the task's own
when a test set one with HeedContext.with, else the process's. The old names stay as read-only properties, so
the call sites (Clock.now has about 480) do not change.

Why a task local and a process value, and not values passed in: the screenshot and tutor-check runs pin the
fake world for the whole app, and SwiftUI draws from the run loop, outside any task, so their pin has to
belong to the process (HeedContext.pinProcess, behind a Mutex, set once at the start of those runs as
before). Tests need the opposite, a change only their own task tree sees, which is what @TaskLocal gives.
Threading the values through every caller of Clock.now and Paths.split would not buy more isolation than that.

The same for other process-wide state tests changed:
- Remote.setTargets and setExtraOptions (the ssh integration tests) are gone; Remote.with(targets:extraOptions:)
  scopes them to the test's task. The app still sets the configured routes with setRoutes.
- GitEnvironment.isolate() was a one-way switch the first git test turned on for the whole process, so a test
  that forgot it behaved differently depending on what ran before. The test bundle's constructor
  (HeedTestProcess.c) now sets the same variables before any test runs, and isolate() is gone.
- GitEnvironmentTests set GIT_DIR and friends with setenv for the whole process; it now hands them to its own
  commands with GitEnvironment.$inheritedOverride.

Looked at and left: PullTrustBook, WriteAccessBook, ToolLocator, TailscaleCLI (shared caches behind a Mutex or
an actor, which no test changes), GitHub.$executable and the other task locals, AuditTap and CallLog install
(only the app calls them), and HeedApp's statics (main-actor app state, not reached by the tests).

.swiftlint.yml now rejects a nonisolated(unsafe) static var in Sources, and pinProcess, setenv, setRoutes and
the AuditTap and CallLog installs in Tests. HeedContextTests reads while a neighbour's override is in effect;
with `with` changed to set the process value, as the statics did, both of its tests fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mpdreamz
Mpdreamz force-pushed the fix/no-global-state branch from c8df764 to 368196b Compare November 16, 2026 17:40
@Mpdreamz
Mpdreamz merged commit a977d67 into main Nov 16, 2026
2 of 3 checks passed
@Mpdreamz
Mpdreamz deleted the fix/no-global-state branch November 16, 2026 17:41
Mpdreamz added a commit that referenced this pull request Nov 16, 2026
)

Main's lint fails: #279 added the `no_unsafe_static_var` rule and #273
(light theme) added two `nonisolated(unsafe) static var`s in
GhosttyRuntime. The class is @mainactor and every use of `isDark` and
`started` is on it, so the statics drop `nonisolated(unsafe)` and are
main-actor isolated.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant