Event-log lock and append
Where this code lives. The code described here lives in this repository, under
service/andcontracts/, imported from core at512567eee71e31bb39f580c4f7acf8b865e9d1b2with its history (ADR-0002). Citations of those files point here; citations of what stays in core (its ADRs, the CLI, the Python appenders) point at core at4eecb124, where they were checked.
While the CLI still writes a log, a Java writer that touches the same log must exclude the CLI’s writer and write the same bytes. Even after a log is handed over whole, an older CLI installed in another checkout of the same repository can keep appending to it, so the lock must hold across languages (the lock-interop spike).
The lock the Python appender takes
src/kernel/event_log_append.py
is the one interim lock contract for every event log except status.events.jsonl and
canonical-events.jsonl (#6606). In its own words:
- Anchor: the log itself, or the family directory for one-file-per-record logs
(
kitty-ops/<op>.jsonl). - Lock file:
<git-common-dir>/spec-kitty-locks/event-logs/<digest>.lock, where<digest>is the first 32 hex characters of the SHA-256 of the anchor’s resolved,os.path.normcase-d path. Outside a git repository it is<anchor>.event-log.lock; when git cannot be invoked, the append refuses. - Lock: an exclusive
kernel.locks.machine_file_lockwith a bounded wait. - Torn tail: under the lock, a log whose last byte is not LF is refused
(
EVENT_LOG_TORN_TAIL) and nothing is written. - Write: the whole unit in one
os.writeon anO_APPENDdescriptor, thenos.fsync. A short or failed write cuts the log back to its size before the write.
The OS lock under it is in
src/kernel/locks.py:
fcntl.flock on POSIX and msvcrt.locking on Windows. status.events.jsonl keeps the
Mission status lock (<key>.status.lock), built on the same primitive.
The Java side
The event-log library’s adapter.out.ledger package holds the Java half
(README, “Package layout”):
LedgerLocks.forThisOs()picksflock(2)through the Foreign Function and Memory API on Linux and macOS, andFileChannel.lockon Windows, so it excludes the CLI’skernel.locksholder.TornTailRepairis the Java port of the glossary ledger’s_repair_torn_tail.
The #160 work added the status-log appender as a fourth package, adapter.out.statuslog:
StatusLogAppendertakes<gcd>/spec-kitty-locks/<missionDirectory basename>.status.lock— the key core’sfeature_status_locktakes, from the directory’s basename rather than its slug or mission id — with the time left to the caller’s deadline, and yields oneLockedStatusLogSessionfor<missionDirectory>/status.events.jsonl.LockedStatusLogSessionreads the whole log under the lock, refuses a torn tail, then writes every new row in oneO_APPENDwrite of full JSON lines andforce()s it. When the log is created it syncs the parent directory too. A failed or short write is rolled back by truncating to the size before the write and forcing again. One session permits one append.EventLogBoundaryTestholds the package to itself,adapter.out.ledgerandjava..; the FR-010 architecture rule makes it the only package that opens a status event log for writing.- Native metadata. The FFM downcalls (
open,flock,close) are registered inservice/command-cli/src/main/resources/META-INF/native-image/org.speckitty/command-cli/reachability-metadata.json, andNativeDowncallMetadataTestguards the three descriptors. The jar manifest carriesEnable-Native-Access: ALL-UNNAMED, and the native build passes--enable-native-access=ALL-UNNAMED.
What the lock-interop spike found (#6193)
Verdict: GO on all three operating systems, from CI run 40557031791 (the spike page’s evidence table):
| OS | Java lock | Against | Result |
|---|---|---|---|
| Linux | flock(2) through FFM | fcntl.flock | GO: 0 overlaps in 1730 rounds |
| Linux | FileChannel.lock | fcntl.flock | NO-GO: an fcntl record lock, which Linux keeps independent of flock; 172 overlaps in 495 rounds |
| macOS | flock(2) through FFM | fcntl.flock | GO |
| macOS | FileChannel.lock | fcntl.flock | GO (the macOS lock manager makes the two conflict) |
| Windows | FileChannel.lock | msvcrt.locking | GO: both are byte-range locks that overlap at byte 0 |
Under load, appends from both languages landed as whole lines, each byte-identical to what
its writer wrote, and both the Python reader and the Java LedgerLineDecoder accepted the
whole log. The spike listed what the write service still needed, among them: computing the
lock path itself, porting the status lock’s path rule, O_CLOEXEC and O_NOFOLLOW on the
lock file, checking and writing through one descriptor, and registering the FFM downcall
for the native image. The production appender above does all five; StatusLogInteropTest
now proves the same cross-process exclusion and byte framing against core’s real status
writer, and the lock-interop job runs it with LockInteropSpikeTest.
Atomic, durable append
The append contract of the write service is set by core ADR 2026-10-27-1 D-5:
- A unit (one transition and its annotations) is one
O_APPENDwrite of full JSON lines, synced withfsyncbefore the answer. A batch is one write and onefsync, all or nothing. A refusal writes nothing. - The service writes the bytes the CLI’s encoder writes:
sanitize_event_for_log, thenjson.dumps(..., sort_keys=True). - On Windows there is no
O_APPENDatomicity between processes, so the service holds the repository’s write lock around the write.
The production Java append is the adapter.out.statuslog package above
(StatusLogAppender + LockedStatusLogSession). It appends the encoded bytes the CLI’s
encoder writes, under the Mission status lock, one write and one sync per request. The
spike’s harness remains in the event-log test sources, package
org.speckitty.missionstatus.spike.lockinterop; StatusLogInteropTest is the production
counterpart and runs in the same lock-interop job.
Torn-tail detection and repair
A torn tail is what a write cut short by a crash leaves after the last newline.
- Detection. The Python appender refuses to append after one (above). The read service
answers an unterminated last line with
500and the diagnosticledger_torn_tail, and never serves the partial line (README, “Fail-loud reads”, L4). - Repair.
TornTailRepairappends the fragment as one line to<ledger>.tornand makes that durable, then cuts the ledger back to its last newline and makes that durable too, so no byte is lost. It runs under the leaf lock<ledger>.lock, the one the Python repair takes, and re-reads the tail inside it. The caller decides when to repair. - Shared goldens.
event-log/src/test/resources/torn-tail/repair-cases.jsonholds the cases both the Java and the Python repair are tested against, byte for byte. It ships in theconformance-fixturesrelease asset (ADR-0003).
Which policy the write service applies is now decided: it refuses. A torn tail answers
500 STATUS_EVENT_PERSISTENCE_VERIFICATION_FAILED and nothing is written; the write path never
repairs. The operator ruling of 2026-10-31 keeps that.
The status log has its own explicit repair, and it lands before core #6195 removes core’s
status-log writer (spec-kitty-events#222,
delivered with #236). It is the
operation POST /api/v1/missions/{missionId}/status-log-repairs. It takes the same in-process
turn and the same Mission status lock as a write, under the same 10 s budget. A repair never
runs inside a write. It reads the tail and answers one of three actions:
none: the log is missing, empty or ends in LF. Nothing is touched, and no file is created.completed: the tail is exactly one status row, with anevent_idno earlier row has, that folds after the log’s prefix. One LF is appended.quarantined: any other tail. The fragment is appended tostatus.events.jsonl.tornbeside the log, and the log is cut back to its last LF.
The repair writes in this order: the quarantine path is checked and the fragment is appended to
the quarantine file and forced; the directory holding both files is synced; the log’s size is checked
again; only then is the log truncated to its last LF, forced and its directory synced. The
truncate is the only step that removes bytes from the log, so a failure before it leaves the log
unchanged. A failure after it leaves the fragment in the quarantine file. The answer reports
readableAfterRepair, which says whether the log now folds as the next write would.
The glossary writer’s TornTailRepair described above is a different operation. It has its own
lock (<ledger>.lock) and its own file, and it stays available to the caller that chooses to
repair a ledger. The status log’s repair is the operation above.
Why CI pins a core SHA for the interop test
The interop test drives core’s production Python code, never a copy: the peer
scripts/ci/lock_interop_peer.py calls kernel.event_log_append and
kernel.locks.machine_file_lock (spike page, “What was tested”). That code
stays in core, so this repository’s CI checks core out to run it.
ADR-0002 decision 4 sets the terms:
- The services never depend on core at build time or at run time.
- Test-only peers, the lock-interop peer among them, use a core SHA pinned in this repository and moved only by a reviewed PR.
- The pinned SHA never reaches a shipped artifact.
A pin rather than core main keeps this CI from depending on a moving reference. The cost
is named in the ADR’s risks: a stale pin hides a lock change in core until the pin moves.
The lock-interop job runs here, not in core, so a lock change in core re-runs it only when
a reviewed PR moves the pin (spike page, “Evidence”).