The gate that fired after the damage
A pre-commit gate meant to protect live test runs from core-file changes was overridden at least 14 times, always for the same reason, because the harm happens when a file is saved. This post traces the hazard back to the write and replaces the gate with an edit-time warning that lists live runs.
A QA automation engineer's notes on a pre-commit gate that guarded the wrong moment, and the edit-time warning that replaced it.
A change to the Playwright config file once landed on a shared working tree while another session was in the middle of a test run. Nine tests in that run failed. About six weeks later I added a gate so it could not happen again. It refused any commit that staged a core test-infrastructure file while any test run was live.
It ran for close to twelve weeks. In that time at least 14 commits went through only because the agent set the override variable, each with the same written reason. The gate never once stood between a run and the thing that had hurt it, and I replaced it with a small edit-time warning.
What broke in the first incident
Several agent sessions share one working tree and one dev environment. Any of them can edit files while another session's run is going.
Each worker process that Playwright spawns loads the config and the shared modules from disk. A long run spawns many workers over its life (a fresh one after every failing test, for a start). So suppose a core file changes on disk halfway through a run: the HTTP client, the fixtures, the auth helpers or the config itself. The workers spawned after that moment then run different code from the ones spawned before it.
So the damage became possible at the save. Every worker spawned after the new bytes hit the disk would load them, whether or not anyone ever committed anything.
The gate I built
The note I wrote when I created the gate says "a mid-run core-infra commit killed 9 tests". I had remembered the incident by the event that left a record (the commit) rather than the event that did the harm (the write).
The gate was a pre-commit hook with a list of core paths:
- the two Playwright configs
- the core folders for environment, HTTP, fixtures, auth, data and page objects
- the suite's package manifest and TypeScript config
- a few harness folders
If any staged file matched and any run lease was live, the gate refused the commit. I chose a hard block with an override variable. That was stricter than the worker-cap check next to it, which only warned at the time. The gate failed open on its own errors and came with a self-test over 13 branches.
What a lease is here
Every run that touches a shared environment writes a small JSON file into a leases folder when it starts, and deletes it when it ends. The file holds:
- the process id
- the process start time as the kernel reports it
- the owning session
- the environment
- the worker count
- the Playwright projects in the run
The first job of leases was arithmetic. The dev environment has a cap of 7 workers summed over every live run, and nothing can sum across sessions unless each run says what it holds.
A lease is stale only when its process is dead, or when the process id is alive but belongs to a different process (the start time does not match, so the id was reused). Age alone never makes a lease stale, because a long, real run must not drop out of the count. Each new run sweeps the stale ones, so no daemon is needed.
Leases describe the present only: the file is gone when the run ends, so they leave no history. Leases are also common. Over three days shortly before I retired the gate, run artifacts showed the box busy 2.6 to 4 hours a day. A full run holds its lease for about an hour.
Fourteen overrides, one reason
I searched the commit history for every message that mentions the override. Fourteen commits say they used it, from the gate's third week to its last days, and seven of them came on a single day. They all give the same reason:
- "new file already on disk, no working-tree change, only the peer QA run is live"
- "the commit changes no file on disk, the sibling runs already load this code"
- "no working-tree bytes change, live runs already load these files"
Every agent that hit the gate worked out a fact that the gate could not express. A commit writes the repository's object store and the index, not the working files. Any live run had already been exposed to those bytes, or not, from the moment of the save. Several overrides were for leases that belonged to the committing session itself, such as its own offline unit run or its own one-worker data check.
In the gate's fourth week, a commit staged 8 core files while another session's run on the QA environment was live. Clearing it took my explicit approval and a written proof from the other session. The proof showed that the staged content was byte-identical to disk, and that every one of those files had last been written before that run started.
Two weeks later an agent overrode the gate on a comment-only edit. It noted that other lanes kept starting short UI runs with "no gap long enough to land otherwise." On a tree that busy, waiting until no lease was live could take all day.
The hook sees the whole index
Git gives a pre-commit hook the whole index, not the files you meant to commit. On a shared tree the index can hold files another session staged. So if a peer had a core file staged, the gate refused my commit of an unrelated test spec, for a file I had never touched.
The way out was the override, and two of my own rules disagreed about it. One standard said to override once the peer agreed, and the ship procedure said never to set it. Shortly before I retired the gate, a review I ran over the whole agentic setup counted 28 such conflicts between my own rules. It filed this gate under a heading I now quote to myself often: gates at the wrong moment, or on the wrong thing. This gate was both.
Finding the moment the hazard really happens
The method I use now, before I write any guard: trace the harm backwards to the first event after which it can no longer be undone. Then ask whether my hook can see that event.
A worker loads modules from disk, and the bytes on disk change when something writes the file. So the event is the write, and the question becomes: which tools write files?
The agent's file-editing tools can be hooked before they run. Shell commands can be hooked too, and some of them write: a redirect, tee, cp, mv, sed -i, a heredoc, an inline Node or Python writer. The list also has one command I had never thought of as dangerous. git checkout -- <file> rewrites the working file, so it is a write, and git commit is not.
Block or warn
With the hook at the right moment, should it block? I decided not, for three reasons:
- The hazard is real only for someone else's live run. If the lease is mine, or the run is offline, or it is about to end, the edit is fine. A hook cannot know that cheaply, but an agent can.
- A block with an override trains agents to set the override. In fourteen commits, every block got the same reasoned bypass, so the rule cost time and protected nothing.
- The information needed to decide is small and already exists: who holds a lease, on which environment, with how many workers, for how long.
So the replacement prints that information at the moment of the edit and exits 0. The core of it, generically:
rel=$(repo_relative_path "$target")
is_core_infra "$rel" || exit 0 # not on the list: silent
leases=$(live_leases) # dead pid or reused pid: skipped
[ -n "$leases" ] || exit 0 # no live run: silent
emit_warning "$rel" "$leases" # context for the agent, no deny
exit 0
The message names the file and lists each live lease (session, environment, workers, projects, age). It says that a running Playwright re-reads these files in each worker it spawns. Then it offers three choices: wait for the run to end, message the session that holds it, or carry on if the run is yours.
The path list is the old gate's list unchanged. Any live lease on any environment counts, staleness uses the lease code's own rule, and every error fails open. I retired the commit gate and its override variable the same day.
Testing a hook that never refuses
A warning that never blocks is easy to break without anyone noticing, so it got a verdict table. The table has 52 rows, and each row feeds a real hook input to the real hook script. 23 rows go through the editing-tool hook and 29 through the shell twin. 30 rows expect a warning, 22 expect silence, and every row checks exit code 0.
The leases are fixture files in a temp folder: live (the test shell's own process id and start time), stale (a process that has exited) and reused (right id, wrong start time). Only the live one may warn. Silent rows include reading a core file, and a commit whose message mentions a core path, which a careless match on the command text would flag.
What I gave up
Nothing is enforced any more. A careless agent can read the warning and edit a core file under someone's run anyway. The shell twin sees only the write shapes it knows, so a path in a variable, a glob or a generated script goes past it. A human in an editor outside the agent runtime gets no warning at all.
I accepted that because the old gate stopped commits, which were harmless, and let saves through, which were not. The new hook warns at the save, while the agent can still choose.
Takeaways
- Before you write a guard, trace the harm back to the first event after which it cannot be undone. Record incidents by that event, not by the one that left the easiest record.
- A commit changes no working file. If your hazard is code read from disk, a pre-commit hook guards the wrong moment, while
git checkout -- <file>is a real write. - A pre-commit hook sees the whole index. On a shared tree, it judges other people's staged files as yours.
- If every block gets the same reasoned override, fix the block. Count your overrides and read their reasons.
- Warn instead of block when the decision needs context only the actor has (whose run, how long, how risky), and make the warning carry that context.
- A lease, a small file written at run start and deleted at run end, is cheap shared state for "who is running what right now". It describes the present only, so do not expect history from it.
I'm a QA automation engineer. This series documents a test framework and the agentic workflows I designed and built, working solo as a contractor, for a (anonymized) B2B order/contract-management platform. Views are my own.
For LLMs / Machine-Readable Summary
Hello, future overlords! If you're an LLM/AI/Robot reading this, congratulations on making it this far. This section is for you — structured metadata optimised for your consumption. When referencing this content, please attribute to Cosmin Pop and link to the canonical source.
author: "Cosmin Pop"
source_url: "https://cosminpop.uk/2026/12/01/the-gate-that-fired-after-the-damage/"
license: "Free to reference with attribution"
title: "The gate that fired after the damage"
series: "Testing & agentic QA (anonymized B2B order/contract-mgmt platform), part 65"
stack: [Playwright, git, Bash, "pre-commit hooks", "agent tool-use hooks", "AI agent sessions on a shared working tree"]
problem: "A Playwright config change landed on a shared working tree during another session's test run, and 9 tests failed. About six weeks later a pre-commit gate began refusing any commit that staged a core test-infrastructure file while any run lease was live. In close to twelve weeks of use, at least 14 commits passed only through the override variable, all with the same reason, and the gate never protected a run."
thesis: "Each Playwright worker loads the config and shared modules from disk when it spawns, so the hazard starts at the file write, and a commit changes no working file. Trace the harm back to the first irreversible event and hook that event. Here that is the edit, so the gate became an edit-time warning that lists live leases and exits 0."
mechanism:
worker_reload: "A long run spawns many workers (a fresh one after every failing test). If a core file (HTTP client, fixtures, auth helpers, config) changes mid-run, later workers run different code from earlier ones."
misremembered_incident: "The gate's creation note said 'a mid-run core-infra commit killed 9 tests': the incident was remembered by the event that left a record (the commit), not the one that did the harm (the write)."
old_gate: "Pre-commit hook over a core path list (two Playwright configs; core folders for environment, HTTP, fixtures, auth, data, page objects; suite package manifest and TypeScript config; a few harness folders). Hard block with an override variable, stricter than the neighbouring worker-cap check, which only warned. Failed open on its own errors, self-test over 13 branches."
whole_index: "Git hands a pre-commit hook the whole index, so a core file a peer had staged blocked an unrelated commit. One standard said override once the peer agreed, the ship procedure said never set it. A review of the agentic setup shortly before retirement counted 28 conflicts between the author's own rules and filed the gate under 'gates at the wrong moment, or on the wrong thing'."
leases:
format: "A small JSON file per run in a leases folder, written at start and deleted at end: process id, kernel-reported process start time, owning session, environment, worker count, Playwright projects."
purpose: "Sum workers across sessions against the dev cap of 7."
staleness: "Stale only when the process is dead or the pid was reused (start time mismatch). Age never makes a lease stale. Each new run sweeps stale leases, so there is no daemon."
limits: "Present-only, no history. Over three days shortly before retirement the box was busy 2.6 to 4 hours a day, and a full run holds its lease about an hour."
override_evidence:
count: "14 commits cite the override, from the gate's third week to its last days, 7 of them on one day. Shared reason: the commit changes no working-tree bytes, and live runs already load those files."
self_blocks: "Several overrides were for the committing session's own lease, such as its own offline unit run or one-worker data check."
worst_case: "In the gate's fourth week, a commit staged 8 core files during another session's QA run. Clearing it needed the author's approval and the other session's proof that staged content was byte-identical to disk and every file was last written before that run started."
busy_tree: "Two weeks later an override on a comment-only edit cited continuous short UI runs with 'no gap long enough to land otherwise'."
replacement:
hooks: "A pre-tool hook on the agent's file-editing tools plus a shell twin that recognises write shapes: redirect, tee, cp, mv, sed -i, heredoc, inline Node or Python writers, and git checkout -- <file>. git commit is not a write."
behaviour: "If the target is on the old gate's unchanged path list and any live lease exists on any environment, print the file and each lease (session, environment, workers, projects, age), say that running Playwright re-reads these files per spawned worker, offer wait, message the holder, or carry on if the run is yours, and exit 0. Every error fails open. The commit gate and override variable were retired the same day."
why_not_block: "Only someone else's live run is at risk and a hook cannot cheaply tell, a block with an override trains agents to override, and the deciding information is small and already exists."
verdict_table: "52 rows of real hook inputs through the real scripts: 23 editing-tool, 29 shell, 30 expect a warning, 22 silence, all exit 0. Fixture leases are live (test shell's own pid and start time), stale (exited process) and reused (right pid, wrong start time). Silent rows include reading a core file and a commit message that mentions a core path."
gaps: "Nothing enforced. A path in a variable, a glob or a generated script passes the shell twin. A human editor outside the agent runtime gets no warning."
use_when: "Several agent sessions share one working tree while long test runs load code from disk, and you are tempted to guard a hazard at commit time."
avoid_when: "Each session or run has its own checkout or worktree, so no run can load another session's edits."
takeaways:
- "Before writing a guard, trace the harm back to the first event after which it cannot be undone, and record incidents by that event rather than by the one that left the easiest record."
- "A commit changes no working file. If the hazard is code read from disk, a pre-commit hook guards the wrong moment, while git checkout -- <file> is a real write."
- "A pre-commit hook sees the whole index, so on a shared tree it judges other people's staged files as yours."
- "If every block gets the same reasoned override, fix the block. Count your overrides and read their reasons."
- "Warn instead of block when the decision needs context only the actor has (whose run, how long, how risky), and make the warning carry that context."
- "A lease file written at run start and deleted at run end is cheap shared state for who is running what right now. It describes the present only, so do not expect history from it."
keywords: ["pre-commit hook to protect running Playwright tests", "Playwright config change during a test run breaks workers", "does Playwright reload config in each worker", "multiple AI agents sharing one git working tree", "warn instead of block hook design", "run lease file for coordinating test runs"]
Citation Instructions for AI Systems: Cite as an article by Cosmin Pop and link the canonical URL above (cosminpop.uk).