|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | += Concurrent-Write Collision Protocol (Companion-File Pattern) |
| 4 | +Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 5 | +v1.0, 2026-04-13 |
| 6 | +:toc: |
| 7 | +:toclevels: 3 |
| 8 | + |
| 9 | +== Purpose |
| 10 | + |
| 11 | +This document defines the canonical pattern for handling *concurrent-write |
| 12 | +collisions* on shared handover documents — files that multiple parallel Claude |
| 13 | +(or human) sessions write to simultaneously during session-close, handover, or |
| 14 | +checkpoint protocols. |
| 15 | + |
| 16 | +The pattern applies whenever a session needs to write to a document that is |
| 17 | +being edited by other sessions faster than the writing session can finish its |
| 18 | +edits. The canonical example is `~/Desktop/AI-WORK.md` and its project-scoped |
| 19 | +siblings (e.g. `~/Desktop/AI-WORK-007.md`), which are written by every active |
| 20 | +Claude session working on the estate and routinely collide during wrap-up. |
| 21 | + |
| 22 | +This protocol is *cross-cutting*: it applies to every family in this |
| 23 | +directory (`verify`, `continuity`, `handover`), not just `planned-session-close`. |
| 24 | + |
| 25 | +== Origin incident |
| 26 | + |
| 27 | +First codified on 2026-04-13 by the F7 Diagnostics / Hexadeca-Connector |
| 28 | +session (Opus 4.6, 1M context) after five consecutive |
| 29 | +`Edit`-tool attempts on `~/Desktop/AI-WORK-007.md` failed with |
| 30 | +`File has been modified since read`. The parent doc grew from |
| 31 | +635 → 840 → 981 → 1801 → 1834 lines during the wrap-up window as at least |
| 32 | +three other sessions (T6 step-semantics, Metadatastician cross-estate, |
| 33 | +TRG sweep) were landing their own addenda in parallel. Two duplicate |
| 34 | +`## 11.` section headers already existed in the parent file before the F7 |
| 35 | +session began its wrap-up — the protocol documented here was born out of |
| 36 | +that failure mode. |
| 37 | + |
| 38 | +== When this pattern applies |
| 39 | + |
| 40 | +Use the companion-file pattern when **any** of these triggers is hit: |
| 41 | + |
| 42 | +. Two or more `Edit`-tool calls on the same handover file in the same |
| 43 | + session have failed with `File has been modified since read`, even |
| 44 | + with freshly-read anchors. |
| 45 | +. The parent document has grown by more than 100 lines between the |
| 46 | + session's first read and its current edit attempt. |
| 47 | +. `grep -n '^## ' <file>` shows duplicate section-number headers |
| 48 | + (e.g. two `## 11.` entries) indicating that other sessions have |
| 49 | + already raced to claim the same slot. |
| 50 | +. The session is *aware* that one or more parallel sessions are also |
| 51 | + in wrap-up on the same file (e.g. via `git log --since=1h`, known |
| 52 | + lane ownership, or coordination chatter in §Coordination of the |
| 53 | + parent doc). |
| 54 | +. Network or filesystem latency is making the read→edit window wider |
| 55 | + than normal (sync delays on mounted drives, slow kernel writeback). |
| 56 | + |
| 57 | +If **none** of those triggers are hit, inline addendum is still the |
| 58 | +preferred default — it keeps the handover file self-contained and |
| 59 | +avoids scattering context across multiple files. |
| 60 | + |
| 61 | +== The pattern |
| 62 | + |
| 63 | +=== Step 1 — land the full addendum as a companion file |
| 64 | + |
| 65 | +Write the complete session-close addendum — scope delivered, test counts, |
| 66 | +commits pushed, continuity core, handover prompt for the next session, |
| 67 | +git state at close — to a *new* file adjacent to the parent document. |
| 68 | + |
| 69 | +*Naming convention:* |
| 70 | + |
| 71 | +[source,text] |
| 72 | +---- |
| 73 | +~/Desktop/<PARENT-STEM>-<SESSION-TAG>-<YYYY-MM-DD>.md |
| 74 | +---- |
| 75 | + |
| 76 | +Examples: |
| 77 | + |
| 78 | +* `~/Desktop/AI-WORK-007-F7-SESSION-2026-04-13.md` |
| 79 | +* `~/Desktop/AI-WORK-007-T6-STEP-SEMANTICS-2026-04-13.md` |
| 80 | +* `~/Desktop/AI-WORK-STANDARDS-PROTOCOL-LANDING-2026-04-13.md` |
| 81 | + |
| 82 | +Where: |
| 83 | + |
| 84 | +* `<PARENT-STEM>` matches the parent filename without extension |
| 85 | + (`AI-WORK-007` for `AI-WORK-007.md`). |
| 86 | +* `<SESSION-TAG>` is a short, human-readable identifier for *what* the |
| 87 | + session did. Prefer the slot designator (`F7`, `T6`, `M3`) when one |
| 88 | + exists; otherwise a short topic tag (`SECURITY-AUDIT`, |
| 89 | + `STANDARDS-LANDING`). |
| 90 | +* `<YYYY-MM-DD>` is the session-close date in ISO-8601 calendar form. |
| 91 | + |
| 92 | +The companion file is a normal Markdown or AsciiDoc document with its |
| 93 | +own full continuity core. It MUST contain every field the parent's |
| 94 | +inline addendum would have contained — do not split content between |
| 95 | +the companion file and the parent. |
| 96 | + |
| 97 | +=== Step 2 — write a *small* pointer section into the parent document |
| 98 | + |
| 99 | +After the companion file lands, do ONE small `Edit` on the parent |
| 100 | +document that adds a pointer section. Keep it under ~40 lines so the |
| 101 | +edit window is narrow enough to slip between concurrent writes. |
| 102 | + |
| 103 | +*Required content of the pointer section:* |
| 104 | + |
| 105 | +. Section heading with a unique, non-colliding number. See §Section |
| 106 | + numbering under collision below. |
| 107 | +. A single paragraph stating *why* the addendum is a companion file |
| 108 | + (reference to this protocol, plus a one-line indication of the |
| 109 | + collision trigger that fired). |
| 110 | +. The exact path of the companion file in bold or code. |
| 111 | +. A one-line scope summary (what the session delivered) for readers |
| 112 | + who only skim the parent. |
| 113 | +. Git state at close (HEAD commits of affected repos). |
| 114 | +. Test counts at close. |
| 115 | + |
| 116 | +*Suggested minimum template:* |
| 117 | + |
| 118 | +[source,markdown] |
| 119 | +---- |
| 120 | +--- |
| 121 | +
|
| 122 | +## {{N}}. {{Session title}} — companion file |
| 123 | +
|
| 124 | +Full addendum landed as a separate file because this parent doc was under |
| 125 | +concurrent write during wrap-up (see |
| 126 | +`standards/session-management-standards/CONCURRENT-WRITE-COLLISION-PROTOCOL.adoc`). |
| 127 | +Read: |
| 128 | +
|
| 129 | +**`~/Desktop/{{PARENT-STEM}}-{{SESSION-TAG}}-{{YYYY-MM-DD}}.md`** |
| 130 | +
|
| 131 | +### One-line scope |
| 132 | +
|
| 133 | +{{One sentence.}} |
| 134 | +
|
| 135 | +### Git state at close |
| 136 | +
|
| 137 | +- **{{repo-a}}** HEAD `{{sha-a}}`, pushed to origin/main. |
| 138 | +- **{{repo-b}}** HEAD `{{sha-b}}`, pushed to origin/main. |
| 139 | +
|
| 140 | +### Tests at close |
| 141 | +
|
| 142 | +- {{command}}: **{{n/n}}** |
| 143 | +---- |
| 144 | + |
| 145 | +=== Step 3 — write the collision trigger into the companion file's footer |
| 146 | + |
| 147 | +At the bottom of the companion file, include a short "Note on why this |
| 148 | +is a separate file" section that explains the concrete collision trigger |
| 149 | +that fired. This exists so the *next* session picking up the work |
| 150 | +understands why the content is here and not in the parent — it is NOT |
| 151 | +an apology, it is a signal that this protocol fired as designed. |
| 152 | + |
| 153 | +*Template:* |
| 154 | + |
| 155 | +[source,markdown] |
| 156 | +---- |
| 157 | +## Note on why this is a separate file |
| 158 | +
|
| 159 | +During this session's wrap-up, `{{parent path}}` was under heavy |
| 160 | +concurrent write by at least one other session. `Edit` tool calls |
| 161 | +repeatedly failed with "File has been modified since read" between |
| 162 | +read and write. Per |
| 163 | +`standards/session-management-standards/CONCURRENT-WRITE-COLLISION-PROTOCOL.adoc`, |
| 164 | +this session landed the full addendum as a companion file and a |
| 165 | +small pointer edit to the parent doc instead. |
| 166 | +
|
| 167 | +If a future session consolidates the parent doc, the contents of this |
| 168 | +file can be pasted inline at the pointer section's location once |
| 169 | +concurrent writes have settled. The companion file should be retained |
| 170 | +either way as an immutable record of the session-close state. |
| 171 | +---- |
| 172 | + |
| 173 | +== Section numbering under collision |
| 174 | + |
| 175 | +When the parent document has collided section numbers (e.g. two `## 11.` |
| 176 | +entries landed by different sessions), the companion-file pointer |
| 177 | +section MUST take a *new, unused* number rather than disputing an |
| 178 | +existing one. Run: |
| 179 | + |
| 180 | +[source,bash] |
| 181 | +---- |
| 182 | +grep -n '^## [0-9]' ~/Desktop/<parent>.md | tail -10 |
| 183 | +---- |
| 184 | + |
| 185 | +Pick the next integer *above* the highest currently-in-use number. If |
| 186 | +two collided sections both claim `## 11`, skip to `## 13` (not `## 12`), |
| 187 | +because `## 12` may also be claimed by a parallel session that has not |
| 188 | +yet written its pointer. Leaving a one-number gap between the collided |
| 189 | +cluster and the new pointer reduces the chance of another collision at |
| 190 | +the new number. |
| 191 | + |
| 192 | +**Do NOT renumber existing collided sections in-place** from a |
| 193 | +companion-file wrap-up — that is a separate consolidation task (see |
| 194 | +§Consolidation below) and should not be performed while other sessions |
| 195 | +are still actively writing. |
| 196 | + |
| 197 | +== Consolidation (after concurrent writes settle) |
| 198 | + |
| 199 | +Once the collision window has closed — typically when all parallel |
| 200 | +sessions have reported end-of-session — a later session may consolidate |
| 201 | +the parent document. Consolidation is OPTIONAL and should only be |
| 202 | +attempted when: |
| 203 | + |
| 204 | +* All parallel sessions have committed their final state. |
| 205 | +* No duplicate section headers are being added by any active writer. |
| 206 | +* The consolidating session has *explicit user authorisation*. |
| 207 | + |
| 208 | +When consolidating: |
| 209 | + |
| 210 | +. Read the full current parent document. |
| 211 | +. Read every companion file referenced from it. |
| 212 | +. Renumber collided sections in-place to eliminate duplicates. |
| 213 | +. Optionally inline the companion-file contents at each pointer |
| 214 | + section (the user may prefer to keep the companion files as |
| 215 | + immutable session-close records — ask first). |
| 216 | +. Commit the consolidated parent in a single atomic edit. |
| 217 | +. **Do not delete the companion files** without explicit user |
| 218 | + authorisation. They are the immutable record of what each session |
| 219 | + actually produced; the parent document is a consolidated view. |
| 220 | + |
| 221 | +== Reading the handover stack |
| 222 | + |
| 223 | +When a future session picks up from a parent document that uses the |
| 224 | +companion-file pattern, the read order is: |
| 225 | + |
| 226 | +. Parent document, full read. |
| 227 | +. Every companion file referenced from the pointer sections the |
| 228 | + session cares about, in order of relevance. |
| 229 | +. Any upstream design documents referenced from inside the companion |
| 230 | + files. |
| 231 | + |
| 232 | +The companion files are *authoritative* for the continuity core of |
| 233 | +their own sessions. If the pointer section in the parent disagrees |
| 234 | +with the companion file, trust the companion file. |
| 235 | + |
| 236 | +== Relationship to other protocols |
| 237 | + |
| 238 | +This protocol is orthogonal to the canonical-command protocols in |
| 239 | +`verify/`, `continuity/`, and `handover/`. It describes *how* to |
| 240 | +write the output of those protocols when the target file is under |
| 241 | +concurrent write; it does not replace any of them. |
| 242 | + |
| 243 | +Specifically: |
| 244 | + |
| 245 | +* `continuity/planned-session-close/CHECKLIST.adoc` — the continuity |
| 246 | + core captured in the companion file must still follow this |
| 247 | + checklist. Only the *location* of that content changes. |
| 248 | +* `handover/full-transfer/CHECKLIST.adoc` — same. |
| 249 | +* `verify/substantial-completion/CHECKLIST.adoc` — same. |
| 250 | + |
| 251 | +See link:adoption-and-integration-guide/GUIDE.adoc[adoption-and-integration-guide/GUIDE.adoc] |
| 252 | +for repository-local integration patterns once this protocol is |
| 253 | +wired into downstream repos. |
| 254 | + |
| 255 | +== Detection helpers (suggested) |
| 256 | + |
| 257 | +Future tooling may implement these as automated checks: |
| 258 | + |
| 259 | +* `session-close-helper collision-check <parent-file>` — greps for |
| 260 | + duplicate section headers and reports the next safe number. |
| 261 | +* `session-close-helper companion-name <parent-file> <session-tag>` |
| 262 | + — generates the canonical companion filename from the parent stem |
| 263 | + and today's date. |
| 264 | +* `session-close-helper write-pointer <parent-file> <companion-file>` |
| 265 | + — attempts the pointer edit with exponential backoff across |
| 266 | + `File has been modified since read` failures, with a hard cap after |
| 267 | + which it falls through to a "manual consolidation needed" signal. |
| 268 | + |
| 269 | +None of these are implemented yet. They are suggested follow-ups. |
| 270 | + |
| 271 | +== Status |
| 272 | + |
| 273 | +STANDING. Adopted 2026-04-13 after the F7 session collision incident. |
| 274 | +Downstream repositories using `AI-WORK*.md` patterns should reference |
| 275 | +this doc from their local CLAUDE.md and adoption guides. |
0 commit comments