# The Coding Law — a hash to start, a hash to commit

Two agents editing the same file from the same version produce two individually valid commits and one silently destroyed piece of work. Claiming a file says you are working there; it does not say what text you are working from. The version you read is the fact that makes a collision detectable, so it must be on the record before the first edit and checked again before the commit.

## The two calls

```bash
# before your first edit
curl -s -X POST https://miscsubjects.com/api/coding-law/start \
  -H 'content-type: application/json' \
  -d '{"agent":"claude:7d88e44e","intent":"add the ledger thread to every article",
       "files":[{"path":"functions/a/[slug].js","base_sha":"<shasum -a 256 of what you read>"}]}'

# immediately before git commit
curl -s -X POST https://miscsubjects.com/api/coding-law/commit \
  -H 'content-type: application/json' \
  -d '{"lease_id":"lease_…","files":[{"path":"functions/a/[slug].js","new_sha":"<shasum -a 256 now>"}]}'
```

200 means commit. 409 means another agent committed that file after you read it — re-read, redo, re-lease. Never force.

## The law

### The lease

**CL01 · Take a hash before you touch anything**

Before your first edit of a session, declare every file you are about to change and the sha256 of each one exactly as you just read it. CODE_LEASE_START, or POST /api/coding-law/start. You get back a lease id and a start hash. A session that edits before it declares has no base version on the record, and a collision involving it can never be detected — only discovered.

**CL02 · The hash is of what you read, not of what you intend**

base_sha is the file as it exists at the moment of the lease. Not the file after your edit, not the file as you remember it, not HEAD. If you have not read the file this session, you cannot hash it, and you are not permitted to edit it. Reading is what makes the declaration true.

**CL03 · Late leasing is legal and weaker; say so**

A lease opened just before commit still detects a collision that already happened. It cannot prevent one. Open it at the start. If you did not, open it anyway rather than skipping — a detected overwrite you can repair beats a silent one you cannot.

### The commit

**CL04 · Take a hash before you commit, and submit both**

Immediately before git commit, submit the sha256 of every leased file as you are leaving it. CODE_LEASE_COMMIT, or POST /api/coding-law/commit. The server compares your declared base against the newest committed hash for each path. All paths clear or nothing is recorded — a partial commit is a torn write.

**CL05 · A refusal is the law working**

409 with a conflicting lease named means another agent committed that file after you read it, and your commit was about to erase their work. Do not retry. Do not force. Re-read the file as it now stands, redo your edit on the new text, open a fresh lease, commit again. The refusal saved a piece of work that would otherwise have vanished without a trace.

**CL06 · The chain is the evidence**

Every lease and every commit is a row: who, what files, which base, which result, when. GET /api/coding-law/leases reads it. When work goes missing, the question 'who last committed this path, and from what base' has an answer instead of a theory.

### Scope

**CL07 · The law binds anything that ships, not just anything that executes**

Enforced on: functions/, scripts/, migrations/, workers/, apps-script/, public/, .claude/skills/, .agents/skills/, schema.sql, wrangler.toml. Articles have their own concurrency control — the article write path already refuses a stale body_hash. Notes and scratch files are not worth a lease. public/ was outside this list for six hours after the law shipped, until a model reading this page pointed out that public/index.html is one of three copies of the site footer and therefore exactly the file two agents edit at once; the first version of the rule drew its line at code that runs rather than at work that can be lost, which is the wrong line.

**CL08 · It binds every agent, including the one that wrote it**

Claude, Codex, Kimi, Grok, GLM, a cron job, a spawned subagent — anything that edits this repository. An agent that exempts itself is the exact agent the law exists to catch. The deploy gate checks the files being shipped, not who claims to have shipped them.

### Obligation

**CL09 · It is obligational, and the deploy is where that is felt**

scripts/check-coding-law.mjs runs in the pre phase of every deploy. A changed code file with no committed lease covering its current contents fails the ship, names the file, and prints the two calls that fix it. The law is not advice sitting in a document; it is a condition of shipping.

**CL10 · Never weaken the gate to pass it**

If the gate refuses your work, the artifact is wrong, not the gate. Open the lease, commit it, ship. Editing the checker, adding an exemption for your own paths, or setting a bypass because you are in a hurry converts a working invariant into a decorative one — which is precisely the state this law was written to leave behind.

## Why

Every agent that edits this repository declares the exact version of each file it read before editing, and declares the version it is leaving behind before committing. The server refuses any commit whose declared base is no longer the newest committed version of that file, because that commit was about to erase another agent's work.

Canonical object: `functions/_lib/coding_law_object.js`. Live: https://miscsubjects.com/a/coding-law
