From 8b6e7fa09379334a01d1359e4913737186c6a791 Mon Sep 17 00:00:00 2001 From: Brett Heap <1513478+brettheap@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:22:26 +0000 Subject: [PATCH 1/2] 007 S007-11+11a+12+14: `LANDING EXPIRED` and LR005's cases, test 4's order module and AT-F5's race, the in-doubt module, AT-F10 and AT-LR006 (12 tasks; part of opensoft/openRepoTools#165) Pull request P04 of feature 007's task sets, version 12f: lane 2's sets S007-11, S007-11a, S007-12 and S007-14 in one commit, through the code queue. It is stacked on P01, code pull request opensoft/openRepoTools-code#88, at P01's head 7f1141be15ac9c0d20c25c802ba9533c2fec811c on code main c9c65cc14695732c9badf9f1f7cac795f661d524, which line R takes as merge154 32e11de4ad2c736281a04cc80f3ddfbb71e10e4b (rule 2). It is cut from lane 1's line R at merge158 7e11b7712678fc57d190d7b2b77d92f0c012eab7. Its 123 task commits, all lane 2's and each whole, were picked with cherry-pick -x in line R's order across the four sets: the 69 of lane 2's line N2 that line R's merge116 d0198d1537cd858ef16fdaac9ed4d213c1edc013 took, then the 54 of lane 2's line ID that its merge117 8da196ee59e3c9530103f6e1c88411a0781a708a took. Each part below lists its own, in that order. Every pick applied clean, with its original's patch-id, so no hunk was settled by hand. T252 is this PR's since version 12f, whole, in S007-11a (lane 3's point 4, on lane 2's opensoft/openRepoTools#165 6885798100), so T232's and T233's picks apply onto T252's lines, as they do on line R. Line N2's other commits are not picked: T216 5f3c46336b85e647742c27fc2c2ea3b5dbdb5cf4, T216a fa5988e2db7de879f75f24f8f58c0f4a3a109d79 and T216b 383cfdbb52dca814c3a36bed88f3d330ba24f29e, the roles module's (S007-05, P02), and its two merges of lane 2's line H, 84f725fd1b9f16ffbd6e0c3a43f6ff4b49edc49e and 642b9323020f1fb49f42370c66ff40623108acfd, which change only P01's helper modules. No plain commit of landing note 15 falls in these sets, and no other line R commit holds their code by hunk. The two gate tables (rule 11) are T223's and T222's own slices, T223b and T223c in the order module and T222h and T222i in the in-doubt module, and this commit holds them. The full shas are pointers into lane 1's share. 007 S007-11: AT-LR5's `LANDING EXPIRED` cases, LR005's rules and the act's key, and AT-F5's group A cases (T223, T232, T233; part of opensoft/openRepoTools#165) - Lane 2's, each whole, from line R's merge116 d0198d1537cd858ef16fdaac9ed4d213c1edc013, in line R's order (pick numbers in brackets): T223 520cdff15cd4ca3e4120bf6fb1171e2151b9cb6e [1], T232 3913e36b15d05dd1a97bb3b703c1425470dd4d4a [7], T233 9e27825d61632f3f03f09d2f6823fa1219c3e4d0 [12], T232a f9b79376bdee87cbf3f57ad9e51d817250984e97 [15], T233a c6b9029c85ccbbc55efd029a422c57dca4b245a8 [18], T233b e5760763e29439c0b9174bf210fd9828f9eb15f1 [27], T233c 841f2c6398fb4cb2d6d2a75b42c24cb1c0d8bc7a [41], T233d 67f2fac6f26acdb6ab244af973c5ef4d43fc24b6 [47], T233e 582eb02ab8b7a3f5db952ad555ada4f1f7f41809 [54], T223b 1c1c15db2603bb86d45625371bbd2391bdb6cf73 [68], T223c d80af8610e215188dbc11c87ec27b802fd1041ed [69]. - Markers: tests/sql_register_landed/T223, T232 and T233. 007 S007-11a: Test 4's order, AT-F14's burst and AT-F5's group B race: the order module, with the logging tools that the in-doubt module imports (T243, T248, T252; part of opensoft/openRepoTools#165) - Lane 2's, each whole, from line R's merge116 d0198d1537cd858ef16fdaac9ed4d213c1edc013, in line R's order (pick numbers in brackets): T243 ed35beab47e68394b0e323d43a7e676e28c0f9e2 [4], T248 4e21292a18ab0b5019ac3bd0c48410c2d2825033 [5], T252 522bf9a34eac2b54225d4f3a395e115211b3ec3b [6], T243a ef861cc3ac2eaca296f42171ceb56b3b6cb2060a [8], T243b 06a3e062d533da77cd8ac11da4d2eec9d0a5385b [9], T243c 1808c6e69c5141544c5d129cfaa253dd90c621ad [10], T252b 81b18fe13994ee7da2ecde1e41b770c2c48c7e2f [11], T252c a0cf67454dbe5dbf72955b728b698e05e14634a2 [13], T243d c37e5d0c91d80b2fdd7edb3ee60c9b0b8c41af35 [14], T243e 9f4eefe9e782ba490a5bbe9bed2cc906916cf85c [16], T252d 57c2da926700b90a387291c0d00c705c265d91db [17], T252e 31bd03b40436ef9d33906a5c60aa873ecaa65a67 [19], T252f cadda0ffb58757ed6bf73ef6f738f7a0abcf8e05 [20], T252g 96cdcb9a73208e8b420484d14909c26522bbe54c [21], T252h f802db91ac4250fd0e2a841c0f151557363054b7 [22], T252i 6f3f9f310fe16b22508393edb8ae1e3ea1303ae3 [23], T252j 73c3c50f807ca661346182d41f3f3d21afa26d92 [24], T243f a3c9d94509dada45d7dfa78e96e21d71b26f18e1 [25], T248a 31c264937fe48bd41b42ff5e20fcc7b2461a695e [26], T243g 28f52ec5661b4b2bce0f664c6d3cec67274bef4a [28], T243h 36e688b4ad57a9491024bc186769a0ed9614719c [29], T252k eca60df56b90e975513d03222d8df025a3c0128a [30], T252l 9fb06648dfd7dffcaea70fa075316248fd2067b2 [31], T252m e5ff83804447a2598edd67a5fcfc2106b8669808 [32], T243i a2a47ff320ac5e9e804445bf7fe99a2f8cc9fb15 [33], T243j 4500b72ca84a21b4066a19e37a84717ab9d1ce55 [34], T243k f864e461ef663256f4f32983ba9e158080fca8c0 [35], T248b faca3f763df7c5b7db0302fd598ffe7af1c45e87 [36], T252n 2ee229d8503de97f2d714ee6c55f24553c930754 [37], T248c f554f7bdca2425d9835ce67b1f5e586b5947da85 [38], T252o 1f8b0acda849a222a6bef26556a4ad1c3e018e6a [39], T252p 27291952afbe067d45d399ff86d9a68c0b9b4bb6 [40], T252q f96fdeab1e56730ce63b56446f09f33a34b362e9 [42], T243l 0e2459f9da94a3f67602c9b1f34142c928bd296f [43], T243m b0226071976e67cb8d50b228adc29e426c7a4177 [44], T243n 9dcbb790a8ee26218eb9b96ad68278a1ab8db694 [45], T248d 34cecf787b9a2b80a8c74bedbd36653330b25d96 [46], T243o 28b2076f656e9cb08141cbb2fa669f01ef909f3f [48], T252r 1a9253024f2bfce48226a04571f7b51fb5f149e4 [49], T252s b94ffde3e2d2508d5d18e1f9f8337ec2edb0ce17 [50], T243p db5bae19ef0a1e2a2a6927a861562d369d510ab2 [51], T243q c2f74ccb83cb9ecf204ff8c2f56f0aa8075fcb7c [52], T243r c0f81f501bac310159d7ca256f978c9d3b7fb609 [53], T243s 71e611272c386abbb503620bb396eba0315d2c6c [55], T248e 6a8ceeef19e2b66dc05936a41395b4d556490b0c [56], T252t f753a90bba13c50e8d9153108f46c72cea690ead [57], T243t c900063a7563f66fbe737f2ed8c29622de96b808 [58], T252u 20377aed7d8ebe78330bc6bbfcec36f24b15efb5 [59], T252v 66a0a5b6434c619741f8ea9f48d2a0b715345add [60], T252w 70896d1537399f601538cf01186e86f39e5419d2 [61], T252x c04a1922f2397ba45465bc5b58856c53eb6a4b41 [62], T243u 4d157eeb5f2d4c1508a2d2827f590ffcb059737c [63], T252y 97e2318f1608ea483f9a13ac76fd759b5e33525a [64], T243v e4a107898c419dde8322efa40692fa0c81bf38f7 [65], T243w 0bb577a7916b55d32ed52dc05461e4f415c17f01 [66], T252z 2b0669cd83ac3cce6675b6831798a1b0bc7c26e4 [67]. - tests/test_sql_register_order.py, which S007-11 and S007-11a share, equals line R's merge158 blob 36b921cbc43481fdaed74b11661a061dff0cce28 after pick 69, T223c, the blob merge116 first held. - Markers: tests/sql_register_landed/T243, T248 and T252. 007 S007-12: AT-LR007/LR011's first checks, test 12, D8/AT-F34 and AT-F3: the in-doubt module (T222, T244, T245, T246; part of opensoft/openRepoTools#165) - Lane 2's, each whole, from line R's merge116 d0198d1537cd858ef16fdaac9ed4d213c1edc013, in line R's order (pick numbers in brackets): T222 2f044aed051751d8c70e99304cc3dae63735174d [2], T222f 95c3a68da1f7107f284008f211cb57062646e8fc [3]. - Lane 2's, each whole, from line R's merge117 8da196ee59e3c9530103f6e1c88411a0781a708a, in line R's order (pick numbers in brackets): T244 1bd0692a20e1128591197e9afd4ee21a2a21f07b [70], T245 f13bcc9a0ef3e16a8d68aa691cd51c12fa12a0ad [71], T246 d234396b33bccd9aa1660d7b7a916a6b8e2f77ed [72], T222g 290ef3f219809b6971730acd3c0eeb3a31b5ff65 [75], T245b f5855122fe91d4ac80fa98d6a0bc5bb44a3a906d [76], T244b 3965033393a5307cf327d8ee430381d1bef69b4a [77], T244c 8c61c2c19f4878fe720131b2bed97207d5b316c0 [78], T246b 1c2bec7c7533a2f83fb0dcf9c7fecf84dd1e584a [79], T245c 6a9d877ac6a70151b2bad74fe92297230bde1b2f [80], T244d e3e59b0a48bb5688af748db22d8fcc7b12eefd8f [81], T246c 0edabb229954b65bcbec712a313430c03a33c230 [82], T245e d37d31253d98dd37e0eab5b070b849e12237d72d [83], T245f 6489ae110809deaa3edddfe8aad4ce64f65ab617 [84], T246d d5bcc2aeb52441068eebd28c5c4b26d81bd0dd71 [85], T244e 4929ea34380f14ec2dc759442362ab3e192527a6 [86], T246e d7f90d7aff45b08326dc371e3a323dd61d440aaa [88], T245g 136de3e5989a7fe34a7dcb24d171f712307b9f6e [89], T244f 36f9fb53d36133adb74247b8e5c65548dbad4ba8 [90], T245h da71d7babc9692c170e3fe7fbb7616a645c852bd [91], T245i 7cc7e63fdf44dbca8ba5cf6f9b45a07038892629 [92], T245j ee2196015b338a8870c2d241c6d33ea6d689d342 [93], T245k 0e4b566b7b63a13b25a2f2a936031707cf38f3df [94], T244g 849a4ec7bdbb8b22e7ae766f34f5ae5a7cc8abad [95], T245l 15952df7fc6f9833e414223b6f27c7202cdc81de [96], T245m acb7d09cc066b7c58ed98e53902ecbf708200631 [97], T245n a9bdcc17ff78a8ea77f9484378bff2c9b853c014 [98], T245o 10c619c8ea61f5dcae2ab2542572b1ae36aa95d4 [99], T245p 3b326c2421fca7d2e113c10d7d4d839a97c8534d [101], T245q 4ae779428c8cc8074098fc3c38d2492343431a51 [102], T245r 04116b6bfe19c91bb663d3d84f9189e97aed78dc [103], T245s 3dfe7e73d9714a399a28a56ea1e1c0040e084f27 [104], T245t 36cdcd377d3c91b93b7ea1e04183263c7cdb03cc [105], T245u e0007b9039dd1d22ac3c48f60ce3598df61cb64f [106], T245v caab8aad300a69d3d7ede1cb9c8e3cdc9c6c6bd3 [107], T245w d76490a83bf48ea7902d5474c4bc5b0c47f9597d [108], T245x 78a06a3e0863a1a9b90c14bf3fafdcc7a6d0ae40 [109], T245y b63e5678fcbeb930f9e6db158fcf41f9de6389cc [110], T245z 8d0ebfa8865086b0783921052d2a5f11c5d9a414 [111], T245za 7f1249b3584da7014dd4b5e5a9f74b6aea39ad3a [112], T245zb a8232d8054aa49115056bae7edca8dbbc595b0d4 [113], T245zc 0a525da2b9ec8ae8b28b0bc6f822855fb8498099 [114], T245zd 1bc0ace57336a3765da24804d4d21f048aeb8745 [115], T245ze b3bf6fcf46cc7ce9b82f18d89dd46f1ec407d0ee [116], T246f ea9c1d7d387bc4acfdc6b3d7c25dd1f043a7e121 [117], T245zf e0387f1e6968e6e97cb0db852ba8e63e6563a1d7 [118], T245zg 7a510d5dd63c30213388b8515aee66dd728fcf98 [119], T246g a014a1ceaa9ca40000accb9a3657c43229b1c438 [120], T245zh 502525074c963b9fb2da5fcbc5aa707d93f5b2dc [121], T222h 92c3a696d78734c7ca3b5bae28af91d5f8681cc4 [122], T222i f8c6d319abf8fda33c214aea5a1cc6509da8dc9e [123]. - tests/test_sql_register_in_doubt.py, which S007-12 and S007-14 share, equals line R's merge158 blob 284d62baf110c80a8ddbefb1a377ec3f757512ac after pick 123, T222i, the blob merge117 first held. - Markers: tests/sql_register_landed/T222, T244, T245 and T246. 007 S007-14: AT-F10 and AT-LR006/AT-F33 (T247, T251; part of opensoft/openRepoTools#165) - Lane 2's, each whole, from line R's merge117 8da196ee59e3c9530103f6e1c88411a0781a708a, in line R's order (pick numbers in brackets): T247 bc124f97b8c19d506008ff9651eccaec6d77ad70 [73], T251 268f339551596c472e56e80b89249c673eb58fc2 [74], T247b cb6765f5735580612afe1d9a84622210adf18389 [87], T247c 38c07a2da4eb4558de2ad09c585bdbd4916b8d89 [100]. - Markers: tests/sql_register_landed/T247 and T251. Lane: openRepoTools-1 Co-Authored-By: Claude Opus 5.5 --- tests/sql_register_landed/T222 | 0 tests/sql_register_landed/T223 | 0 tests/sql_register_landed/T232 | 0 tests/sql_register_landed/T233 | 0 tests/sql_register_landed/T243 | 0 tests/sql_register_landed/T244 | 0 tests/sql_register_landed/T245 | 0 tests/sql_register_landed/T246 | 0 tests/sql_register_landed/T247 | 0 tests/sql_register_landed/T248 | 0 tests/sql_register_landed/T251 | 0 tests/sql_register_landed/T252 | 0 tests/test_sql_register_in_doubt.py | 3590 ++++++++++++++++++++ tests/test_sql_register_order.py | 4758 +++++++++++++++++++++++++++ 14 files changed, 8348 insertions(+) create mode 100644 tests/sql_register_landed/T222 create mode 100644 tests/sql_register_landed/T223 create mode 100644 tests/sql_register_landed/T232 create mode 100644 tests/sql_register_landed/T233 create mode 100644 tests/sql_register_landed/T243 create mode 100644 tests/sql_register_landed/T244 create mode 100644 tests/sql_register_landed/T245 create mode 100644 tests/sql_register_landed/T246 create mode 100644 tests/sql_register_landed/T247 create mode 100644 tests/sql_register_landed/T248 create mode 100644 tests/sql_register_landed/T251 create mode 100644 tests/sql_register_landed/T252 create mode 100644 tests/test_sql_register_in_doubt.py create mode 100644 tests/test_sql_register_order.py diff --git a/tests/sql_register_landed/T222 b/tests/sql_register_landed/T222 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T223 b/tests/sql_register_landed/T223 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T232 b/tests/sql_register_landed/T232 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T233 b/tests/sql_register_landed/T233 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T243 b/tests/sql_register_landed/T243 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T244 b/tests/sql_register_landed/T244 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T245 b/tests/sql_register_landed/T245 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T246 b/tests/sql_register_landed/T246 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T247 b/tests/sql_register_landed/T247 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T248 b/tests/sql_register_landed/T248 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T251 b/tests/sql_register_landed/T251 new file mode 100644 index 00000000..e69de29b diff --git a/tests/sql_register_landed/T252 b/tests/sql_register_landed/T252 new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_sql_register_in_doubt.py b/tests/test_sql_register_in_doubt.py new file mode 100644 index 00000000..284d62ba --- /dev/null +++ b/tests/test_sql_register_in_doubt.py @@ -0,0 +1,3590 @@ +# SPDX-License-Identifier: Apache-2.0 +"""AT-LR007 and AT-LR011: the order of `commit_write`'s first checks, and +`find_write` (#165; FR-008, FR-016). + +PHASE N, ON A REAL POSTGRESQL 16, by direct calls to lane 1's SQL (design.md +§1.3, §1.3.1). `commit_write` takes the head row's lock, then checks the +generation (LR007 `generation`), then the write id (LR006, or LR007 +`another-write` for another write's entry), and only then the head (LR001), +the version (LR003) and the switch (LR002), so a write id of another +generation never ends in 69 or 0 (V2's S-3). `find_write`, F3's one lookup, +waits on the server's clock, holding no lock, until it has passed +`p_not_after`, at most 3 s (LR011 past that); then it takes the head row's +lock under the caller's `lock_timeout`, checks the clock again, then the +generation, then the entry, and returns the write's seq, or NULL. + +THE FIXTURE, AT-LR006'S, ON A CLONE OF ITS OWN: each test clones `tmpl` +(T205 makes it, T227 builds it by the bootstrap), as T221's `[new-write]` +does, runs §1.4's two database-level statements on the clone, and drops it +`WITH (FORCE)` at the end. Nothing needs `tmpl_imported` (lane 3, #165 +6768050756 point 1, which lane 2 accepts; R-F11-23), so T114 need not wait for +T115. A clone of `tmpl` is at `files` with the marker `absent` and an empty +journal, and each test brings it where it needs by AT-LR006's route +(TP:4610–4615), by the documented functions as `lanes_w_fx` and nothing else: +`Clone.imported` commits an `import` entry (one file, `lanes/LANES.md`, of one +line), which is what a clone of `tmpl_imported` held, and `Clone.to_primary` +goes on with a `cutover` entry over that import, then `record_marker`: + + * database R, between a re-import's bootstrap and its cutover: the clone + after `Clone.imported`, at `files` with the marker `absent`, whose + generation is `g1`; `g0`, the lost database's, is a uuid of the test's + own; + * database S, a restore: the clone brought to `primary` with the marker + `present` by `Clone.to_primary` (the import, a `cutover` entry over it, + then `record_marker`: TP:4610–4615), standing in for a restored + backup. W0 and W1 commit under its generation `g0`; then its restore-form + cutover, an after-loss `cutover` entry whose label is not the switch's, + sets a new generation `g1`, and `record_marker` confirms it; then W2 + commits under `g1`; + * AT-LR011's: S's route to `primary`, with write W committed (lane L1, + verb V, `args_sha256` a). + +Every call logs in as the fixture's writer login, `lanes_w_fx` (T228), in +the commit call's own form, `psql -X -q -A -t -v ON_ERROR_STOP=1 -v +VERBOSITY=verbose -v SHOW_CONTEXT=never` with the script on stdin, so a +refusal's first stderr line is `ERROR: : ` and its second +the `DETAIL:` token. A lookup runs at READ COMMITTED in a transaction the test +rolls back, with D9's bounds set in it (`lock_timeout=2000ms`, +`statement_timeout=6000ms`). A holder of the head row is a superuser session +fed on stdin, and every barrier is a state polled in `pg_stat_activity`, by +the session's `application_name`. No elapsed time is asserted: the one time +read, `clock_timestamp()` not before `p_not_after`, is a bound on the +server's own clock. + +Each refused call writes nothing: the head, the journal and the switch equal +their values before. + +Every case FAILS UNTIL lane 1's T107 (L1-A6: `commit_write` and +`find_write`) and T114 (L1-A12) land, with lane 2's T227 (`tmpl` holding the +register) and T228 (`lanes_w_fx`). A clone of `tmpl` with no register is +refused by the fixture's own assertion. Without PostgreSQL 16 each skips. + +THE MODULE ALSO HOLDS, each at its own section below: T244's test 12 (AT-12) +and section 4.3's rival, and T245's D8 and AT-F34 (phase W, the client's +writers on a real register, through the shim), with T245's texts for 3, which +read the scripts and run with no engine; and T246's AT-F3, a write in doubt at +the deadline (phase W, with its direct calls in phase N); T247's AT-F10, a +pending record against an unrelated identical command (phase W); and T251's +AT-LR006 and AT-F33, two attempts of one write meeting at the commit (phase +N, on these clones). + +PHASE W'S WORKSTATION IS T243's: `Estate`, its `estates` fixture, and the +logging tools of T248 are imported from `test_sql_register_order`, as +`test_handoff_skill_80_85` imports its sibling's `Sandbox`, so one +workstation serves both modules and neither keeps a copy. +""" + +from __future__ import annotations + +import datetime +import hashlib +import itertools +import json +import os +import re +import signal +import subprocess +import sys +import time +import uuid +from pathlib import Path +from typing import Dict, List, Optional, Sequence, Tuple + +import pytest + +from pgcluster import SUPERUSER, TEMPLATE, Database +from sql_register_helpers import _proc_start +from test_sql_register_order import (ARROW, BURST, CLIENT, DOT, EM, LA, LB, LC, OBJ, UUID_RE, Barrier, # noqa: F401 + Estate, Writer, claim, estates, hold_point, kind_line, loggers, run_git, + session_of, tool_calls) + +#: THE LANDING GATE'S TABLE (T280; its reader, `tests/sql_register_gate.py`, +#: says how a table is read), TEMPORARY: T281 removes it in the last set. +#: Where the gate is active, each id is skipped until every task its row +#: names has landed, and the skip names them; an id's own row, `name[param]`, +#: replaces its function's. Each row holds the fails-until clauses of the task +#: that owns the test (T222, T244, T245, T246, T247 or T251), each L-label read +#: as the task that covers it, as lane 2's gate tables plan (GP-24, version 2) +#: gives them, with lane 3's rulings on T245's texts for 3 and T247's +#: record-less text. T247's rows also name T415, whose `pw_run` is the write +#: path on primary that each of its tests takes, and T417 where an id +#: asserts its 3, ahead of T247's Order (lane 4's GT-L4, #165 6879601079). +#: No row is `"*"`, since the six tasks' waits share no task. Each +#: `tasks:` is tasks.md's line at `204145ae`, the reader's `SPEC_COMMIT`. +SQL_REGISTER_GATE_TABLE = { + "test_a_call_in_doubt_that_committed_runs_its_tail_once_from_the_kept_child": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_callers_rerun_after_a_failed_reconnect_reuses_the_write_id": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_a_claim_from_another_host_or_container_counts_as_live_for_120_s": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_claim_with_no_record_beside_it_is_waited_for": ("T412", "T415"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_commit_lost_before_it_landed_is_retried_cleanly_and_ends_with_one_entry": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_a_commit_past_its_not_after_raises_lr008_and_one_within_it_commits": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_a_commit_that_landed_before_the_connection_was_lost_ends_with_one_entry_and_exit_0": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_a_committed_write_leaves_no_pending_record": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_generation_change_after_the_snapshot_read_gives_lr007_generation": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_a_lanes_write_id_run_waits_for_a_live_owner": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_live_pending_record_is_waited_for_not_shared": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_lookup_held_at_the_head_row_ends_55p03_inside_its_statement_timeout": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_a_lookup_made_too_early_exits_3_and_keeps_the_record": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_a_lookup_never_mistakes_a_rivals_row_for_the_lost_write": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_a_lookup_refused_for_another_generation_exits_2_and_keeps_the_record": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_a_lookup_refused_for_another_write_exits_2_and_removes_the_record": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_a_lookup_try_without_an_answer_is_made_again_inside_the_window": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_a_pending_record_is_owned_through_numbered_claim_files_and_two_reruns_never_both_take_it": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_plain_rerun_reuses_the_pending_write_id_once_and_says_so": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_record_of_another_generation_is_refused_2_until_lanes_write_id_new": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_replay_of_rename_lane_or_request_handoff_that_stops_or_differs_runs_none_of_the_tail": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_replay_of_request_handoff_types_into_no_pane": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_replay_that_stops_or_differs_runs_no_tail_and_warns": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_request_handoff_that_runs_none_of_its_tail_exits_2_and_writes_nothing": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_a_rerun_after_a_3_replays_the_verbs_tail_once": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_rerun_that_finds_its_write_committed_runs_the_verbs_after_commit_tail": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_rerun_that_finds_tail_sent_runs_no_tail_and_writes_the_done_mark": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_rerun_with_other_arguments_does_not_take_the_pending_id": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_retry_with_the_old_connection_still_open_writes_no_second_row": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_a_reused_pid_in_a_pending_record_reads_as_dead": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_run_killed_after_its_call_line_or_mid_call_is_taken_over_in_doubt_and_waits_past_not_after": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_run_with_no_record_runs_no_tail_and_names_it": ("T412", "T415", "T417", "T418"), # T245; tasks:2363 + "test_a_same_generation_id_after_a_restore_form_cutover_gets_lr006": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_a_sent_record_whose_child_never_read_committed_replays_and_an_owed_mark_runs_no_tail": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_tail_record_a_takeover_cannot_use_is_refused_2_and_kept": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_write_id_already_journaled_for_the_same_arguments_exits_0_at_the_snapshot": ("T412", "T415"), # T247; tasks:2377 + "test_a_write_id_of_other_arguments_verb_or_generation_is_refused_2_at_the_snapshot": ("T412", "T415"), # T247; tasks:2377 + "test_a_write_in_doubt_at_the_deadline_makes_one_lookup": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_a_write_killed_by_an_outer_timer_is_retried_under_its_own_id": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_write_of_another_generation_at_files_gets_lr007_not_lr002_or_lr001": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_a_write_whose_every_call_ended_lr008_exits_75_with_no_lookup": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_an_attempt_committed_after_the_commits_snapshot_gives_40001_without_a_wait": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_an_attempt_committed_after_the_snapshot_read_gives_lr006": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_an_attempt_committed_while_the_commit_waits_gives_40001_then_exit_0": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_an_attempt_holding_past_lock_timeout_gives_55p03_then_exit_0": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_an_attempt_that_never_commits_leaves_the_write_to_the_retry": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_an_expired_pending_record_is_not_reused": ("T412", "T415"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_an_id_in_the_journal_for_another_lane_gets_lr007": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_an_in_doubt_claim_never_leaves_two_claimed_lines_or_a_false_69": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_an_in_doubt_write_whose_next_attempt_is_refused_makes_its_lookup_before_it_exits": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_an_old_generation_id_after_a_restore_form_cutover_gets_lr007_not_lr006": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_an_orphan_that_takes_the_lock_past_its_not_after_gets_lr008_and_the_lookup_finds_it_absent": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_an_orphaned_commit_call_released_after_its_not_after_writes_nothing": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_an_outer_timer_expiry_is_in_doubt_like_exit_2": ("T107", "T108", "T114", "T412", "T417"), # T244; tasks:2350 + "test_another_writes_entry_committed_after_the_snapshot_read_gives_lr007_another_write": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_find_write_gives_lr011_before_lr007": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_find_write_refuses_another_generation_lr007": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_find_write_refuses_another_writes_entry_lr007": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_find_write_refuses_lr011_at_once": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_find_write_returns_this_writes_seq_or_null": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_find_write_waits_on_the_server_clock_holding_no_lock": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_lanes_write_id_of_another_write_is_refused_2_and_the_pending_record_kept": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_lanes_write_id_of_the_records_own_write_takes_it_over_past_its_age": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_liveness_is_the_highest_claims_never_the_records_pid": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_lr006_comes_before_lr001": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_lr008_alone_never_gives_3_or_69": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_lr008_at_the_deadline_leaves_the_exit_to_find_write": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_lr008_inside_the_deadline_is_a_lost_turn_and_the_write_exits_0": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_no_interleaving_of_two_attempts_of_one_write_reaches_23505": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_status_lists_and_resolves_pending_records": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_the_after_commit_tail_of_log_runs_once_when_a_rerun_finds_the_write": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_the_after_commit_tail_of_release_runs_once_when_a_rerun_finds_the_write": ("T412", "T415", "T417", "T418"), # T245; tasks:2363 + "test_the_after_commit_tail_of_request_handoff_types_once_in_the_kept_child_and_polls": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_the_after_commit_tail_of_retire_rows_runs_once_when_a_rerun_finds_the_write": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_the_after_commit_tail_of_start_record_runs_once_when_a_rerun_finds_the_write": ("T412", "T417", "T418", "T427"), # T245; tasks:2363 + "test_the_deadlines_lookup_waits_past_the_largest_not_after": ("T107", "T108", "T114", "T417"), # T246; tasks:2371 + "test_the_no_lookup_control_gives_lr001_where_the_lookup_gives_lr006": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_the_no_lookup_control_reaches_23505_so_the_oracle_can_fail": ("T107", "T108", "T413", "T414", "T415", "T417"), # T251; tasks:2390 + "test_the_pending_digest_covers_the_session_and_equals_args_sha256": ("T412", "T415"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_the_pending_records_call_lines_are_written_before_the_send_and_dropped_only_for_a_call_that_did_not_commit": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_the_switchs_own_generation_at_files_still_gets_lr002": ("T107", "T108", "T114", "T227", "T228"), # T222; tasks:2135 + "test_the_texts_for_3_say_may_not_be_written_and_relay_the_writers_rerun": ("T412", "T417", "T418", "T433", "T434"), # T245; tasks:2363 + "test_two_matching_calls_of_one_change_set_leave_the_replays_tail_owed": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_with_the_kept_child_gone_a_replay_runs_the_tail_once": ("T412", "T417", "T418"), # T245; tasks:2363 + "test_a_claim_with_no_record_beside_it_is_waited_for[live-at-deadline]": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_claim_with_no_record_beside_it_is_waited_for[write-id-live-at-deadline]": ("T412", "T415", "T417"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_live_pending_record_is_waited_for_not_shared[ends-in-time]": ("T412", "T415"), # T247; tasks:2377; lane 4, #165 6879601079 + "test_a_replay_of_rename_lane_or_request_handoff_that_stops_or_differs_runs_none_of_the_tail[rename-lane-stops]": ("T412", "T417", "T418", "T422"), # T245; tasks:2363 + "test_the_pending_records_call_lines_are_written_before_the_send_and_dropped_only_for_a_call_that_did_not_commit[refusal]": ("T412", "T415", "T417", "T418"), # T245; tasks:2363 +} + +REGISTER = "lanes_register" +WRITER_LOGIN = "lanes_w_fx" +CONNECT_TO = "lanes_register_writer, lanes_register_reader, lanes_indexer" +LANES_MD, STORE = "lanes/LANES.md", "lanes/STORE" +WS, AUTHOR = "ws-t222", "T222 Fixture " +OPERATOR = "operator-t222" +MARKER_COMMIT = "c0ffee" + "1" * 34 +IMPORT_COMMIT, IMPORT_TREE = "c" * 40, "d" * 40 +IMPORT_LINES = ["# Lanes"] +RESTORED_LABEL = "restored-t222" +L1, L2, VERB, OTHER_VERB = "L1-t222", "L2-t222", "append-line", "lane-end" + +COMMIT_SQL = ("BEGIN ISOLATION LEVEL SERIALIZABLE;\n" + "{set}" + f"SELECT {REGISTER}.commit_write(:base, :'wid'::uuid, :client, :'gen'::uuid, :'entry'::jsonb);\n" + "COMMIT;\n") +MARKER_SQL = f"SELECT {REGISTER}.record_marker(:client, :seq, :'commit', :'ws');\n" + +#: F3's lookup: READ COMMITTED, D9's bounds, the call, the clock read next in +#: the same transaction, and the rollback. +LOOKUP_SQL = ("BEGIN ISOLATION LEVEL READ COMMITTED;\n" + "SET LOCAL lock_timeout = '2000ms';\n" + "SET LOCAL statement_timeout = '6000ms';\n" + f"SELECT coalesce({REGISTER}.find_write(:'wid'::uuid, :'not_after'::timestamptz, :'gen'::uuid, " + ":'lane', :'verb', nullif(:'args', ''))::text, 'NULL');\n" + "SELECT clock_timestamp() >= :'not_after'::timestamptz;\n" + "ROLLBACK;\n") + +#: What a refused call must leave as it was: the head, the journal, the switch. +STATE_SQL = f""" +SELECT md5(concat_ws('|', + (SELECT seq::text FROM {REGISTER}.register_head), + (SELECT string_agg(j::text, ',' ORDER BY j.seq) FROM {REGISTER}.journal j), + (SELECT string_agg(s::text, ',') FROM {REGISTER}.register_switch s))); +""" + +HOLD_HEAD = f"BEGIN;\nSELECT 1 FROM {REGISTER}.register_head WHERE id = 1 FOR UPDATE;\n" + +_NAMES = itertools.count(1) + + +def sha256(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def rendered(lines: Sequence[str], final_newline: bool) -> bytes: + """`render_file`'s bytes: the lines joined by LF, and a last LF where + the file ends with one and holds a line.""" + return ("\n".join(lines) + ("\n" if final_newline and lines else "")).encode("utf-8") + + +class Clone: + """One clone of `tmpl`, and the documented functions called on it as + REGISTER-WRITER, through `lanes_w_fx` (AT-LR006's fixture).""" + + def __init__(self, cluster, name: str) -> None: + self.cluster, self.name = cluster, name + self.db = Database(cluster, name) + self.made = 0 + + # -------------------------------------------------- the superuser's reads + def value(self, sql: str) -> str: + return self.db.psql(sql).stdout.strip() + + def json(self, sql: str): + return json.loads(self.value(sql)) + + def switch(self) -> dict: + return self.json(f"SELECT row_to_json(s) FROM {REGISTER}.register_switch s;") + + def head(self) -> int: + return int(self.value(f"SELECT seq FROM {REGISTER}.register_head;")) + + def client(self) -> int: + return int(self.value(f"SELECT version FROM {REGISTER}.schema_version;")) + + def state(self) -> str: + return self.value(STATE_SQL) + + def clock(self, seconds: float = 0) -> str: + """The database's clock, plus `seconds`.""" + return self.value(f"SELECT (clock_timestamp() + make_interval(secs => {seconds}))::text;") + + def file(self, path: str) -> Tuple[Optional[bool], List[str]]: + said = self.json( + f"SELECT json_build_object('nl', (SELECT final_newline FROM {REGISTER}.files WHERE path = '{path}'), " + f"'lines', (SELECT coalesce(jsonb_agg(text ORDER BY line_no), '[]') FROM {REGISTER}.doc_lines " + f"WHERE path = '{path}'));") + return said["nl"], said["lines"] + + def activity(self, appname: str) -> List[List[str]]: + """Each backend of `appname`: pid, wait_event_type, wait_event.""" + out = self.value( + "SELECT coalesce(json_agg(json_build_array(pid, wait_event_type, wait_event)), '[]') " + f"FROM pg_stat_activity WHERE application_name = '{appname}' AND state = 'active';") + return json.loads(out) + + def head_locks(self, pid: int) -> int: + """The locks `pid` holds or waits for on the head row's table, its + tuples, or a transaction id.""" + return int(self.value( + f"SELECT count(*) FROM pg_locks WHERE pid = {pid} AND (relation = '{REGISTER}.register_head'::regclass " + "OR locktype IN ('tuple', 'transactionid'));")) + + # -------------------------------------------------- the calls, as the writer + def argv(self, variables: Dict[str, object]) -> List[str]: + argv = [self.db.psql_bin, "-X", "-q", "-A", "-t", "-v", "ON_ERROR_STOP=1", + "-v", "VERBOSITY=verbose", "-v", "SHOW_CONTEXT=never"] + for name, value in variables.items(): + argv += ["-v", f"{name}={value}"] + return argv + + def call(self, sql: str, appname: str = "t222", **variables) -> subprocess.CompletedProcess: + return subprocess.run(self.argv(variables), input=sql, capture_output=True, text=True, timeout=60, + env=self.db.env(WRITER_LOGIN, appname), check=False) + + def start(self, sql: str, appname: str, user: str = WRITER_LOGIN, **variables) -> subprocess.Popen: + """The same call, in the background: its script sent and its stdin closed.""" + proc = subprocess.Popen(self.argv(variables), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, env=self.db.env(user, appname)) + proc.stdin.write(sql) + proc.stdin.close() + proc.stdin = None + return proc + + def commit_variables(self, entry: dict, base: Optional[int], write_id: str, + generation: Optional[str]) -> Dict[str, object]: + return {"base": self.head() if base is None else base, "wid": write_id, "client": self.client(), + "gen": generation or self.switch()["generation"], + "entry": json.dumps(entry, ensure_ascii=False)} + + def commit(self, entry: dict, *, base: Optional[int] = None, write_id: Optional[str] = None, + generation: Optional[str] = None) -> Tuple[subprocess.CompletedProcess, str]: + write_id = write_id or str(uuid.uuid4()) + proc = self.call(COMMIT_SQL.format(set=""), **self.commit_variables(entry, base, write_id, generation)) + return proc, write_id + + def committed(self, entry: dict, **options) -> Tuple[int, str]: + proc, write_id = self.commit(entry, **options) + assert proc.returncode == 0, ( + f"commit_write as {WRITER_LOGIN} exited {proc.returncode}: {proc.stderr.strip()[-800:]}") + return int(proc.stdout.strip()), write_id + + def lookup(self, write_id: str, not_after: str, *, generation: Optional[str] = None, lane: str = L1, + verb: str = VERB, args: str = "") -> subprocess.CompletedProcess: + return self.call(LOOKUP_SQL, appname="t222-lookup", **self.lookup_variables( + write_id, not_after, generation, lane, verb, args)) + + def lookup_variables(self, write_id, not_after, generation, lane, verb, args) -> Dict[str, object]: + return {"wid": write_id, "not_after": not_after, + "gen": generation or self.switch()["generation"], "lane": lane, "verb": verb, "args": args} + + # -------------------------------------------------- entries + def journal(self, kind: str, verb: str, lane: str, **extra) -> dict: + row = {"kind": kind, "verb": verb, "lane": lane, "workstation": WS, "author": AUTHOR, + "message": f"{verb} by {lane} (T222)"} + row.update(extra) + return row + + def appended(self, path: str, texts: Sequence[str]) -> Tuple[list, list]: + final_newline, lines = self.file(path) + edits = [] + if final_newline is None: + edits.append({"ord": 1, "op": "create", "path": path}) + final_newline = True + for text in texts: + edits.append({"ord": len(edits) + 1, "op": "append", "path": path, "new_text": text}) + return edits, [{"path": path, "present": True, "final_newline": final_newline, + "sha256": sha256(rendered([*lines, *texts], final_newline))}] + + def write_entry(self, lane: str = L1, verb: str = VERB, *, seconds: float = 60) -> dict: + """A write: one line of no kind appended to `lanes/LANES.md`.""" + self.made += 1 + text = f"t222 note {self.made} — lane {lane}" + edits, files = self.appended(LANES_MD, [text]) + args = sha256(f"{lane}\0{verb}\0{text}".encode("utf-8")) + return {"journal": self.journal("write", verb, lane, args_sha256=args), + "not_after": self.clock(seconds), "edits": edits, "files": files} + + def operator_entry(self, **switch) -> dict: + edits, files = self.appended(STORE, ["primary"]) + entry = {"journal": self.journal("cutover", "cutover", OPERATOR), "not_after": self.clock(60), + "edits": edits, "files": files} + if switch: + entry["switch"] = switch + return entry + + def mark(self, seq: int) -> None: + marked = self.call(MARKER_SQL, client=self.client(), seq=seq, commit=MARKER_COMMIT, ws=WS) + assert (marked.returncode, marked.stdout.strip()) == (0, "present"), marked.stderr + sw = self.switch() + assert (sw["mode"], sw["marker_state"]) == ("primary", "present"), sw + + def imported(self) -> int: + """AT-LR006's route, its first step (TP:4610-4615): an `import` entry + on the clone of `tmpl`, at `files` with the marker `absent` and an + empty journal, as T221's `bring_to_primary` starts. It creates + `lanes/LANES.md` with `IMPORT_LINES`, so the clone holds what a clone + of `tmpl_imported` did, and the switch stays as it was.""" + sw = self.switch() + assert (sw["mode"], sw["marker_state"], self.head()) == ("files", "absent", -1), ( + f"a clone of {TEMPLATE} is not at files, marker absent, with an empty journal: {sw}, head {self.head()}") + edits, files = self.appended(LANES_MD, IMPORT_LINES) + entry = {"journal": self.journal("import", "import", OPERATOR, git_commit=IMPORT_COMMIT, + git_tree=IMPORT_TREE), + "not_after": self.clock(60), "edits": edits, "files": files} + seq, _ = self.committed(entry) + sw = self.switch() + assert (sw["mode"], sw["marker_state"]) == ("files", "absent"), sw + return seq + + def to_primary(self) -> int: + """AT-LR006's route (TP:4610-4615): `imported`, then a `cutover` + entry over that import, then `record_marker`.""" + self.imported() + head = self.head() + last_import = int(self.value(f"SELECT coalesce(max(seq), -2) FROM {REGISTER}.journal WHERE kind = 'import';")) + assert last_import == head >= 0, f"the clone's head {head} is not its import ({last_import})" + seq, _ = self.committed(self.operator_entry()) + self.mark(seq) + return seq + + def refused(self, entry: dict, code: str, detail: Optional[str], *says: str, **options) -> List[str]: + """The call ends with `code` and `detail`, and writes nothing.""" + before = self.state() + proc, _ = self.commit(entry, **options) + return self.check_refused(proc, before, code, detail, *says) + + def check_refused(self, proc, before: Optional[str], code: str, detail: Optional[str], *says: str) -> List[str]: + lines = proc.stderr.splitlines() + assert proc.returncode == 3, f"exit {proc.returncode}, not 3: {proc.stderr.strip()}" + assert lines and lines[0].startswith(f"ERROR: {code}: "), lines + if detail is not None: + assert lines[1:2] == [f"DETAIL: {detail}"], lines + for text in says: + assert text in lines[0], f"{text!r} is not in {lines[0]!r}" + if before is not None: + assert self.state() == before, "the refused call changed the head, the journal or the switch" + return lines + + +@pytest.fixture() +def clones(pg_cluster): + """Clones of `tmpl`, each the test's own, dropped at the end.""" + made: List[str] = [] + + def clone() -> Clone: + name = f"t222_{next(_NAMES)}" + pg_cluster.psql(f"CREATE DATABASE {name} TEMPLATE {TEMPLATE};") + made.append(name) + found = pg_cluster.psql(f"SELECT to_regclass('{REGISTER}.register_head') IS NOT NULL;", dbname=name) + assert found.stdout.strip() == "t", ( + f"{TEMPLATE} holds no {REGISTER} schema: lane 2's T227 builds it by the bootstrap, " + "which waits for lane 1's L1-A7") + pg_cluster.psql(f"REVOKE ALL ON DATABASE {name} FROM PUBLIC;\n" + f"GRANT CONNECT ON DATABASE {name} TO {CONNECT_TO};\n") + return Clone(pg_cluster, name) + + yield clone + for name in made: + pg_cluster.drop_database(name) + + +@pytest.fixture() +def holders(pg_cluster): + """Superuser sessions fed on stdin, each ended at the test's end.""" + started: List[subprocess.Popen] = [] + + def hold(c: Clone, appname: str = "t222-holder") -> subprocess.Popen: + proc = c.db.session(SUPERUSER, appname) + proc.stdin.write(HOLD_HEAD) + proc.stdin.flush() + started.append(proc) + until(c, lambda: c.value(f"SELECT count(*) FROM pg_stat_activity WHERE application_name = '{appname}' " + "AND state = 'idle in transaction';") == "1", "the holder took the head row") + return proc + + yield hold + for proc in started: + end(proc) + + +def end(proc: subprocess.Popen) -> None: + """Roll the holder back and let it exit.""" + if proc.poll() is None: + try: + proc.stdin.write("ROLLBACK;\n") + proc.stdin.close() + proc.stdin = None + except (BrokenPipeError, ValueError): + pass + try: + proc.communicate(timeout=30) + except subprocess.TimeoutExpired: + proc.kill() + proc.communicate() + + +def until(c: Clone, ready, what: str, deadline: float = 20.0): + """Poll `ready` until it answers truly, or fail naming `what`.""" + end_at = time.monotonic() + deadline + while True: + said = ready() + if said: + return said + assert time.monotonic() < end_at, f"not seen within {deadline:.0f} s: {what}" + time.sleep(0.05) + + +def restored(clones) -> Tuple[Clone, Dict[str, dict]]: + """Database S: W0 and W1 under `g0`, the restore-form cutover to `g1`, + confirmed, then W2 under `g1`. Returns each write's id, entry and seq.""" + c = clones() + c.to_primary() + writes: Dict[str, dict] = {} + for name in ("W0", "W1"): + entry = c.write_entry() + seq, write_id = c.committed(entry) + writes[name] = {"id": write_id, "entry": entry, "seq": seq, "gen": c.switch()["generation"]} + g0 = writes["W0"]["gen"] + assert c.switch()["database_label"] != RESTORED_LABEL + seq, _ = c.committed(c.operator_entry(after_loss=True, database_label=RESTORED_LABEL)) + sw = c.switch() + assert (sw["mode"], sw["marker_state"], sw["database_label"]) == ("primary", "adding", RESTORED_LABEL), sw + assert sw["generation"] != g0 + c.mark(seq) + entry = c.write_entry() + seq, write_id = c.committed(entry) + writes["W2"] = {"id": write_id, "entry": entry, "seq": seq, "gen": c.switch()["generation"]} + return c, writes + + +def at_lr011(clones) -> Tuple[Clone, dict]: + """A clone on `primary` with write W committed: lane L1, verb V, args a.""" + c = clones() + c.to_primary() + entry = c.write_entry() + seq, write_id = c.committed(entry) + return c, {"id": write_id, "seq": seq, "args": entry["journal"]["args_sha256"], + "gen": c.switch()["generation"]} + + +# ------------------------------------------------------------ AT-LR007 +@pytest.mark.parametrize("base", ["current-base", "stale-base"]) +def test_a_write_of_another_generation_at_files_gets_lr007_not_lr002_or_lr001(clones, base): + """In R, at `files`: a write of `g0` is LR007 `generation`, not LR002, + with the base current, and not LR001 with it stale.""" + c = clones() + c.imported() + head = c.head() + g0 = str(uuid.uuid4()) + c.refused(c.write_entry(), "LR007", "generation", f"belongs to register generation {g0}", + generation=g0, base=head if base == "current-base" else head - 1) + + +def test_the_switchs_own_generation_at_files_still_gets_lr002(clones): + """In R, a write of `g1`, the switch's own, with the base current: LR002.""" + c = clones() + c.imported() + c.refused(c.write_entry(), "LR002", None, "register switch is files with the marker absent") + + +@pytest.mark.parametrize("base", ["current-base", "stale-base"]) +def test_an_old_generation_id_after_a_restore_form_cutover_gets_lr007_not_lr006(clones, base): + """In S, after the restore form's cutover, W0's id with `g0`, found in + the restored journal: LR007 `generation`, not LR006.""" + c, writes = restored(clones) + w0 = writes["W0"] + head = c.head() + c.refused(w0["entry"], "LR007", "generation", f"write {w0['id']} belongs to register generation {w0['gen']}", + write_id=w0["id"], generation=w0["gen"], base=head if base == "current-base" else head - 1) + + +@pytest.mark.parametrize("base", ["current-base", "stale-base"]) +def test_a_same_generation_id_after_a_restore_form_cutover_gets_lr006(clones, base): + """In S, W2's id with `g1` and W2's own lane, verb and `args_sha256`: + LR006, naming its seq.""" + c, writes = restored(clones) + w2 = writes["W2"] + head = c.head() + c.refused(w2["entry"], "LR006", None, f"write {w2['id']} is in the journal at seq {w2['seq']}", + write_id=w2["id"], generation=w2["gen"], base=head if base == "current-base" else head - 1) + + +def test_an_id_in_the_journal_for_another_lane_gets_lr007(clones): + """In S, W2's id with `g1` and another lane: LR007 `another-write`.""" + c, writes = restored(clones) + w2 = writes["W2"] + other = json.loads(json.dumps(w2["entry"])) + other["journal"]["lane"] = L2 + c.refused(other, "LR007", "another-write", f"write {w2['id']} is seq {w2['seq']}, another write's (F10)", + write_id=w2["id"], generation=w2["gen"]) + + +# ------------------------------------------------------------ AT-LR011 +@pytest.mark.parametrize("case", ["found", "absent"]) +def test_find_write_returns_this_writes_seq_or_null(clones, case): + """With `p_not_after` passed, W's id with its own generation, lane, verb + and `args_sha256` returns W's seq; an id not in the journal, NULL.""" + c, w = at_lr011(clones) + write_id = w["id"] if case == "found" else str(uuid.uuid4()) + proc = c.lookup(write_id, c.clock(-1), args=w["args"]) + assert proc.returncode == 0, proc.stderr + assert proc.stdout.split() == [str(w["seq"]) if case == "found" else "NULL", "t"] + + +@pytest.mark.parametrize("case", ["lane", "verb", "args", "null-args"]) +def test_find_write_refuses_another_writes_entry_lr007(clones, case): + """W's id with another lane, another verb, another `args_sha256`, or a + NULL one, which never matches a write's entry: LR007 `another-write`.""" + c, w = at_lr011(clones) + options = {"lane": L1, "verb": VERB, "args": w["args"]} + options.update({"lane": {"lane": L2}, "verb": {"verb": OTHER_VERB}, + "args": {"args": sha256(b"another write")}, "null-args": {"args": ""}}[case]) + proc = c.lookup(w["id"], c.clock(-1), **options) + c.check_refused(proc, None, "LR007", "another-write", f"write {w['id']} is seq {w['seq']}") + + +@pytest.mark.parametrize("case", ["found", "absent"]) +def test_find_write_refuses_another_generation_lr007(clones, case): + """W's id, and an id not in the journal, each with a generation not the + switch's: LR007 `generation`.""" + c, w = at_lr011(clones) + write_id = w["id"] if case == "found" else str(uuid.uuid4()) + other = str(uuid.uuid4()) + proc = c.lookup(write_id, c.clock(-1), generation=other, args=w["args"]) + c.check_refused(proc, None, "LR007", "generation", f"belongs to register generation {other}") + + +def test_find_write_gives_lr011_before_lr007(clones): + """With `p_not_after` 4 s ahead and a generation not the switch's: LR011.""" + c, w = at_lr011(clones) + proc = c.lookup(w["id"], c.clock(4), generation=str(uuid.uuid4()), args=w["args"]) + c.check_refused(proc, None, "LR011", None, "past the 3 s wait") + + +def test_find_write_waits_on_the_server_clock_holding_no_lock(clones, holders): + """With `p_not_after` 2 s ahead and the head row held, the lookup's + backend is seen first in `PgSleep`, holding no lock, then waiting on a + `Lock`; once the holder ends it returns W's seq, and the clock read next + is not before `p_not_after`.""" + c, w = at_lr011(clones) + holder = holders(c) + not_after = c.clock(2) + proc = c.start(LOOKUP_SQL, "t222-lookup", **c.lookup_variables(w["id"], not_after, None, L1, VERB, w["args"])) + try: + pid = until(c, lambda: [a[0] for a in c.activity("t222-lookup") if a[2] == "PgSleep"], + "the lookup sleeping in PgSleep")[0] + assert c.head_locks(pid) == 0, "the lookup holds a lock while it sleeps" + until(c, lambda: [a for a in c.activity("t222-lookup") if a[1] == "Lock"], "the lookup waiting on a Lock") + finally: + end(holder) + out, err = proc.communicate(timeout=30) + assert proc.returncode == 0, err + assert out.split() == [str(w["seq"]), "t"] + + +def test_a_lookup_held_at_the_head_row_ends_55p03_inside_its_statement_timeout(clones, holders): + """With `p_not_after` 2.9 s ahead and the head row held throughout, the + lookup ends with 55P03 from its `lock_timeout`, not 57014: its wait and + its lock fit its `statement_timeout`.""" + c, w = at_lr011(clones) + holder = holders(c) + try: + proc = c.lookup(w["id"], c.clock(2.9), args=w["args"]) + finally: + end(holder) + c.check_refused(proc, None, "55P03", None) + + +def test_an_orphan_that_takes_the_lock_past_its_not_after_gets_lr008_and_the_lookup_finds_it_absent(clones, holders): + """Commit call O of write X, `not_after` 2 s ahead, waits on the head row + behind a holder, under `SET LOCAL lock_timeout = '5s'`; the lookup for X + waits past `not_after`, then queues. The holder rolls back once the + clock is past `not_after` and both wait on a `Lock`. Whichever takes the + lock first, O ends LR008, the lookup returns NULL, and no journal row + holds X's id.""" + c, w = at_lr011(clones) + holder = holders(c) + entry = c.write_entry(seconds=2) + write_id = str(uuid.uuid4()) + variables = c.commit_variables(entry, None, write_id, None) + orphan = c.start(COMMIT_SQL.format(set="SET LOCAL lock_timeout = '5s';\n"), "t222-orphan", **variables) + lookup = c.start(LOOKUP_SQL, "t222-lookup", **c.lookup_variables( + write_id, entry["not_after"], variables["gen"], L1, VERB, entry["journal"]["args_sha256"])) + try: + until(c, lambda: c.value(f"SELECT clock_timestamp() > '{entry['not_after']}'::timestamptz;") == "t" + and any(a[1] == "Lock" for a in c.activity("t222-orphan")) + and any(a[1] == "Lock" for a in c.activity("t222-lookup")), + "the clock past not_after, with the orphan and the lookup each waiting on a Lock") + finally: + end(holder) + orphan_out, orphan_err = orphan.communicate(timeout=30) + lookup_out, lookup_err = lookup.communicate(timeout=30) + c.check_refused(subprocess.CompletedProcess(orphan.args, orphan.returncode, orphan_out, orphan_err), + None, "LR008", None, f"write {write_id} reached its end") + assert lookup.returncode == 0, lookup_err + assert lookup_out.split() == ["NULL", "t"] + assert c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{write_id}';") == "0" + + +# ============================================================ PHASE W: AT-12 and section 4.3's rival (T244) +# +# A COMMIT IN DOUBT (AT-12, ADV-C3; TP:2245-2420), ON T243's WORKSTATION: a +# clone of `tmpl` brought to `primary` by AT-LR006's route, one configuration +# naming it through `lanes_w_fx`, and per writer a `psql` directory holding +# T203's shim, armed by its control file for the writer's FIRST commit call +# only. Commit calls are the requests carrying `commit_write`, lookup tries +# those carrying `find_write`, and the snapshots lane 1's one read-only +# request, `BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY` (`lanes-index`'s +# `_query_many`). The oracle never matches on stderr where AT-12 says so: the +# shim's `FATAL` line is one a real loss does not print. +# +# The lookup's timer is D9's `T_L`, `N_a`·`c` + 7 s, 9 s at `c` = 2 (open +# point 37). YS:685 as amended (R-F10-2): where `request-handoff` already +# wrote, its refusal names what it wrote; which of `[tail-sent]`, +# `[tail-done]` and `[no-record]` follow a committed write is NOTES question +# 33, so each asserts the journal's one entry and no `send-keys`. +# +# They FAIL UNTIL lane 1's T107 (L1-A6) and T114 (L1-A12, `journal +# --write-id`), lane 4's attempt loop on primary (L4-B2), its in-doubt rule +# and pending record (L4-B5) and its tail, once (L4-B15) land, with lane 2's +# T227 and T228. Without PostgreSQL 16 each skips. + +SNAPSHOT_TEXT = "REPEATABLE READ READ ONLY" +AFTER = {"mode": "after", "match": "commit_write", "call": 1} +BEFORE = {"mode": "before", "match": "commit_write", "call": 1} +#: D9's commit-call timer, `C`, and AT-12's figures from it (TP:2331-2357). +C_S, WINDOW_S, T_L_S = 8, 10, 9 +#: The tries' spacing, with lane 2's tolerance (T246's bullet; NOTES question 34). +GAP_S, LATE_S = 0.75, 1.25 +#: The earliest exit 3 after `no-reconnect`, from the snapshot call's start +#: (TP:2346-2349, "no earlier than about 17 s": 8 s of waiting, and the 10 s +#: window less up to 1 s). The client's wait may itself end up to 1 s early, +#: since its estimate adds whole `SECONDS` to a whole-second clock, which is +#: why try 1 is allowed at `C_S - 1`; so the exit is allowed 1 s early too: +#: 16 s (T244e; lane 4's triage, #165 6833813092: 16.96 s on CI). +EXIT3_EARLIEST_S = (C_S - 1) + (WINDOW_S - 1) +PENDING = Path("openRepoTools") / "register-pending" +WRITE_ID_SAID = re.compile(r"LANES_WRITE_ID=(" + UUID_RE + r")") +GENERATION_SAID = re.compile(r"LANES_WRITE_GENERATION=(" + UUID_RE + r")") +NOT_AFTER_SAID = re.compile(r'"not_after"\s*:\s*"([^"]+)"') +#: The commit call's last COMMIT, with the `\;` that joins it to the statement +#: before it. `lanes-index` sends the commit as ONE request, `BEGIN ISOLATION +#: LEVEL SERIALIZABLE \; SELECT ...commit_write(...) \; COMMIT;` +#: (`PostgresStore.request`; contract §2.3; Y:1419), and `psql` sends its query +#: buffer only at a `;` that is not `\;`: a replay left ending ` \;` is never +#: sent, and its session never holds the head row (T244c; lane 4's triage, +#: #165 6833813092). +LAST_COMMIT = re.compile(r"(?:[ \t]*\\;)?[ \t]*\bCOMMIT\b[ \t]*;?(?![\s\S]*\bCOMMIT\b)", re.I) + + +def without_commit(script: str) -> str: + """The commit call's script with its last COMMIT dropped, and sent whole: + a ` \\; COMMIT;` becomes `;`, so `psql` sends the BEGIN and the + `commit_write` call as one request and the session is left idle in its + transaction; a `COMMIT;` of its own is dropped (`LAST_COMMIT`).""" + return LAST_COMMIT.sub(lambda m: ";" if "\\;" in m.group(0) else "", script, count=1) + + +def note(lane: str, tag: str) -> List[str]: + """A plain write: one `NOTE` line, by `append-line`.""" + return ["append-line", f"NOTE {EM} lane {lane}: {tag} (T244)"] + + +def one_entry(e: Estate, w: Writer, grew: int = 1) -> str: + """AT-12's oracle: every commit call of `w` carried one write id, the + journal grew by exactly `grew`, and that id appears once.""" + ids = w.write_ids() + assert ids and len(set(ids)) == 1, f"{w.name}'s commit calls carried {ids}, not one write id" + rows = e.journal_after() + assert len(rows) == grew, f"the journal grew by {len(rows)}, not {grew}: {rows}" + assert [r["write_id"] for r in rows].count(ids[0]) == 1, (ids[0], rows) + return ids[0] + + +def lookups(w: Writer) -> List[dict]: + return [c for c in w.stamps() if "find_write" in (c.get("stdin") or "")] + + +def pending(e: Estate) -> List[Path]: + """The pending records, `/openRepoTools/register-pending/`.""" + where = e.state / PENDING + return sorted(p for p in where.iterdir() if re.fullmatch(r"[0-9a-f]{64}", p.name)) if where.is_dir() else [] + + +def until_seen(what: str, ready, w: Optional[Writer] = None, deadline: float = 90.0): + end_at = time.monotonic() + deadline + while True: + said = ready() + if said: + return said + if w is not None: + assert w.ended is None, f"{w.name} exited {w.code} before {what}: {w.err[-400:]}" + assert time.monotonic() < end_at, f"not seen within {deadline:.0f} s: {what}" + time.sleep(0.05) + + +def fifo(e: Estate, name: str) -> Path: + path = e.root / f"{name}.fifo" + os.mkfifo(str(path)) + return path + + +def first_not_after(w: Writer) -> str: + m = NOT_AFTER_SAID.search(w.commit_calls()[0].get("stdin") or "") + assert m, "the first commit call's entry carries no not_after" + return m.group(1) + + +def db_past(e: Estate, utc: str) -> bool: + """Whether the database's clock has passed `utc`: an entry's + `not_after`, UTC text, or a `call` line's, whole epoch seconds + (pending-record.md §3.2), which `::timestamptz` cannot read, so it is + read with `to_timestamp` (T244f; lane 4's triage, #165 6833813092).""" + when = f"to_timestamp({int(utc)})" if re.fullmatch(r"[0-9]+", utc) else f"'{utc}'::timestamptz" + return e.c.value(f"SELECT clock_timestamp() >= {when};") == "t" + + +# ------------------------------------------------------------ AT-12: after, before, timer +@CLIENT +def test_a_commit_that_landed_before_the_connection_was_lost_ends_with_one_entry_and_exit_0(estates): + """`after`: the first COMMIT lands, then its backend is terminated. One + entry, its write id once, exit 0.""" + e = estates() + w = e.run(LA, note(LA, "after"), arms=[AFTER]) + assert w.code == 0, w.err[-800:] + one_entry(e, w) + + +@CLIENT +def test_a_commit_lost_before_it_landed_is_retried_cleanly_and_ends_with_one_entry(estates): + """`before`: the first COMMIT never lands. The writer retries cleanly, + under the same write id, and exits 0 with one entry.""" + e = estates() + w = e.run(LA, note(LA, "before"), arms=[BEFORE]) + assert w.code == 0, w.err[-800:] + assert len(w.commit_calls()) >= 2, "the lost commit was not retried" + one_entry(e, w) + + +@CLIENT +def test_an_outer_timer_expiry_is_in_doubt_like_exit_2(estates): + """`timer`: the real `psql` commits, then the shim sleeps past the + commit call's 8 s client timer (D9), whose expiry is in doubt as an exit + 2 is. One entry, exit 0; the time is recorded, not asserted.""" + e = estates() + started = time.monotonic() + w = e.run(LA, note(LA, "timer"), arms=[{"mode": "timer", "match": "commit_write", "call": 1, + "ms": 3 * C_S * 1000}]) + assert w.code == 0, w.err[-800:] + one_entry(e, w) + print(f"[T244 timer] took={time.monotonic() - started:.1f}s against C={C_S}s") + + +# ------------------------------------------------------------ AT-12: the half-open case +@CLIENT +@pytest.mark.parametrize("ending", ["rollback", "commit", "idle"]) +def test_a_retry_with_the_old_connection_still_open_writes_no_second_row(estates, ending): + """`before` loses the first commit call; the old connection, still open, + is the test's session as `lanes_w_fx`, replaying that call's script + without its COMMIT, so it holds the head row with the row inserted. The + shim's `hold` keeps the next snapshot until the replay has returned. + While the session is open no second row exists; [rollback]: the retry + inserts once; [commit]: the waiting retry gets 40001, the next snapshot + finds the id, and no commit call starts after the session's COMMIT; + [idle]: the role's 5 s idle bound ends the session, and the writer + commits once. One row, exit 0, in each.""" + e = estates() + gate = fifo(e, "snapshot-2") + w = e.launch(LA, note(LA, f"half-open {ending}"), + arms=[BEFORE, {"mode": "hold", "match": SNAPSHOT_TEXT, "call": 2, "fifo": str(gate)}]) + first = until_seen("the first commit call", lambda: w.commit_calls()[:1], w)[0] + script = without_commit(first["stdin"]) + assert "commit_write" in script and script != first["stdin"], "the first commit call has no COMMIT to drop" + assert script.rstrip().endswith(";") and not script.rstrip().endswith("\\;"), ( + f"the replay does not end in a `;` that psql sends: {script[-80:]!r}") + session = e.c.db.session(WRITER_LOGIN, "t244-half-open") + try: + if ending != "idle": + session.stdin.write("SET idle_in_transaction_session_timeout = 0;\n") + session.stdin.write(script.rstrip() + "\n") + session.stdin.flush() + until_seen("the replay holding the head row", lambda: e.c.value( + "SELECT count(*) FROM pg_stat_activity WHERE application_name = 't244-half-open' " + "AND state = 'idle in transaction';") == "1", w) + write_id = w.write_ids()[0] + hold_point(gate, w).close() + if ending != "idle": + until_seen("a retry's commit call", lambda: len(w.commit_calls()) >= 2, w) + assert not e.has_write(write_id), "a second row exists while the old transaction is open" + closed = time.time() + session.stdin.write("ROLLBACK;\n" if ending == "rollback" else "COMMIT;\n") + session.stdin.flush() + session.stdin.close() + session.stdin = None + assert w.wait() == 0, w.err[-800:] + finally: + if session.poll() is None: + session.kill() + session.communicate() + assert one_entry(e, w) == write_id + if ending == "commit": + late = [c for c in w.stamps() if "commit_write" in (c.get("stdin") or "") and c["t"] > closed] + assert not late, f"a commit call started after the old transaction committed: {late}" + + +# ------------------------------------------------------------ AT-12: no-reconnect, and the rerun +@CLIENT +@pytest.mark.parametrize("rerun", ["write-id", "plain"]) +def test_a_callers_rerun_after_a_failed_reconnect_reuses_the_write_id(estates, rerun): + """`no-reconnect`: the first commit call is lost, and every later call + exits 2. The writer waits about 8 s, makes at least two lookup tries in + the 10 s window, each at least 1 s after the last (with lane 2's 0.75 s + tolerance) and none past 20 s from the first, and exits 3 no earlier than + about 17 s after its snapshot (asserted at `EXIT3_EARLIEST_S`, 16 s) and + under 54 s. Its report names the id and + the rerun, read from it. [write-id]: the rerun under `LANES_WRITE_ID` + and `LANES_WRITE_GENERATION`; [plain]: the same command line, which finds + the pending record. Either exits 0, never mints a new id, never 69, and + the journal holds one row for the id.""" + e = estates() + argv = claim(OBJ, "--no-github") + w = e.run(LA, argv, arms=[{"mode": "no-reconnect", "as": "before", "match": "commit_write", "call": 1}]) + assert w.code == 3, f"exit {w.code}, not 3: {w.err[-800:]}" + snap = w.stamps()[0]["t"] + tries = lookups(w) + assert len(tries) >= 2, f"{len(tries)} lookup tries" + assert tries[0]["t"] - snap >= C_S - 1, "a lookup try started before the client's wait ended" + for k, (last, this) in enumerate(zip(tries, tries[1:]), 2): + assert this["t"] - last["t"] >= GAP_S, f"try {k} started {this['t'] - last['t']:.2f} s after the last" + assert tries[-1]["t"] - tries[0]["t"] <= 2 * WINDOW_S, "a try started past twice the window" + took = time.time() - (time.monotonic() - w.ended) - snap + assert EXIT3_EARLIEST_S <= took < 2 * (C_S + WINDOW_S + T_L_S), ( + f"exit 3 after {took:.1f} s, not in [{EXIT3_EARLIEST_S}, {2 * (C_S + WINDOW_S + T_L_S)}) s") + print(f"[T244 no-reconnect] exit3={took:.1f}s against {C_S + WINDOW_S + T_L_S}s, " + f"spread={tries[-1]['t'] - tries[0]['t']:.1f}s against {WINDOW_S}s") + ids, gens = WRITE_ID_SAID.findall(w.err), GENERATION_SAID.findall(w.err) + assert ids and gens, f"the report names no LANES_WRITE_ID rerun: {w.err[-800:]}" + write_id = ids[-1] + assert write_id == w.write_ids()[0] + assert f"lanes-index journal --write-id {write_id}" in w.err + extra = {"LANES_WRITE_ID": write_id, "LANES_WRITE_GENERATION": gens[-1]} if rerun == "write-id" else {} + again = e.run(LA, argv, extra=extra) + assert again.code == 0, f"the {rerun} rerun exited {again.code}: {again.err[-800:]}" + assert set(again.write_ids()) <= {write_id}, f"the rerun minted a new id: {again.write_ids()}" + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + assert e.claimed(OBJ) == [(OBJ, LA)] and e.log_lines("CLAIMED", OBJ) == 1 + + +@CLIENT +@pytest.mark.parametrize("mode", ["after", "before", "timer"]) +def test_an_in_doubt_claim_never_leaves_two_claimed_lines_or_a_false_69(estates, mode): + """`claim` in doubt by `after`, `before` or `timer`: exit 0, never 69, + one entry, and the object has exactly one `CLAIMED` line.""" + e = estates() + arm = {"after": AFTER, "before": BEFORE, + "timer": {"mode": "timer", "match": "commit_write", "call": 1, "ms": 3 * C_S * 1000}}[mode] + w = e.run(LA, claim(OBJ, "--no-github"), arms=[arm]) + assert w.code == 0, f"exit {w.code}: {w.err[-800:]}" + one_entry(e, w) + assert e.claimed(OBJ) == [(OBJ, LA)] + assert e.log_lines("CLAIMED", OBJ) == 1 + + +# ------------------------------------------------------------ AT-12: a next attempt refused for another reason +@CLIENT +@pytest.mark.parametrize("reason,code", [("database", 69), ("register", 2), ("verb", 2)]) +def test_an_in_doubt_write_whose_next_attempt_is_refused_makes_its_lookup_before_it_exits(estates, reason, code): + """The first commit call is lost before it lands, so the write is in + doubt with no row; the second attempt is refused. [database]: `claim`, + whose second commit's entry breaks a CHECK (test 2's late failure), + 23514; [register]: a `HOLD` line, whose second snapshot is held while + another lane commits a `HOLD` for the same repository, LR005; [verb]: + `claim`, held the same way while another lane claims the object, refused + by the verb's own pre-check. In each the writer makes a `find_write` call + after the refused attempt, once the database's clock has passed the + first call's `not_after`, and exits with the refusal's own code; the + journal holds no row for its id, and the pending record is removed.""" + e = estates() + gate = fifo(e, "snapshot-2") + arms = [BEFORE] + if reason == "database": + arms.append({"mode": "edit-entry", "match": "commit_write", "call": 2, "field": "journal.kind", + "value": "t244-no-such-kind"}) + argv = claim(OBJ, "--no-github") + else: + arms.append({"mode": "hold", "match": SNAPSHOT_TEXT, "call": 2, "fifo": str(gate)}) + argv = (["append-line", kind_line(e, "HOLD", LA)] if reason == "register" + else claim(OBJ, "--no-github")) + w = e.launch(LA, argv, arms=arms) + if reason != "database": + held = hold_point(gate, w) + rival = (["append-line", kind_line(e, "HOLD", LB)] if reason == "register" + else claim(OBJ, "--no-github")) + assert e.run(LB, rival).code == 0 + held.close() + assert w.wait() == code, f"exit {w.code}, not {code}: {w.err[-800:]}" + # Before T415 the writer's 69 is T409's placeholder, which makes no + # commit call and equals [database]'s own 69: the case fails here, at its + # own assertion, not at `write_ids()[0]` (AN13b's cause 5; line AF13b). + assert w.commit_calls(), (f"exit {w.code}, but the writer made no commit call, so the " + f"write has no id (T415's primary branch): {w.err[-800:]}") + write_id = w.write_ids()[0] + stamps = w.stamps() + attempts = [c for c in stamps if (SNAPSHOT_TEXT if reason == "verb" else "commit_write") in (c.get("stdin") or "")] + assert len(attempts) >= 2, f"no second attempt: {len(attempts)}" + assert [t for t in lookups(w) if t["t"] > attempts[1]["t"]], "no find_write call after the refused attempt" + assert db_past(e, first_not_after(w)), "the writer exited before the first call's not_after" + assert not e.has_write(write_id) + assert pending(e) == [], f"the pending record was kept: {pending(e)}" + + +# ------------------------------------------------------------ AT-12: request-handoff runs none of its tail +@CLIENT +@pytest.mark.parametrize("state", ["tail-sent", "tail-done", "no-record"]) +def test_a_request_handoff_that_runs_none_of_its_tail_exits_2_and_writes_nothing(estates, state): + """GB §4.3's case (R29): `request-handoff`, with T248's fake `tmux` on + `PATH`, is killed after its commit call returned, so the row has landed + and its pending record holds a `call` line and no `tail` key. The rerun + of the same command line, with the record at `tail=sent`, `tail=done`, or + removed and rerun under `LANES_WRITE_ID` with its `LANES_WRITE_GENERATION` + (an id with no generation and no record is refused 2 before any call: + pending-record.md §5.2, R08; T244d), finds its write id and exits 2, + not 0; the journal holds one entry for the id (YS:685 as amended: the + write committed, so the refusal names it), the fake `tmux` logs no + `send-keys` from the rerun, and stderr names the tail it did not run: the + write's "after-COMMIT steps", the words every form of the engine's text + names it by (`8ba10b4e:lanes-edit.sh:10168`, `:10169` and `:10319`); it + never prints the word "tail" (T244g; PM33A-TRIAGE L1).""" + e = estates() + where = loggers(e) + gate = fifo(e, "commit-1") + argv = ["request-handoff", LC, "--session", session_of(LC)] + w = e.launch(LA, argv, arms=[{"mode": "timer", "match": "commit_write", "call": 1, "fifo": str(gate)}], + first=[where]) + write_id = until_seen("the first commit call", lambda: w.commit_calls()[:1] and w.write_ids()[0], w) + until_seen("the row landed", lambda: e.has_write(write_id), w) + os.killpg(w.proc.pid, signal.SIGKILL) + w.wait() + records = pending(e) + assert len(records) == 1, records + text = records[0].read_text(encoding="utf-8") + assert "\ncall=" in "\n" + text and "\ntail=" not in "\n" + text, text + seq = e.journal_after()[-1]["seq"] + extra = {} + if state == "no-record": + for path in records[0].parent.glob(records[0].name + "*"): + path.unlink() + extra = {"LANES_WRITE_ID": write_id, "LANES_WRITE_GENERATION": e.c.switch()["generation"]} + else: + with records[0].open("a", encoding="utf-8") as record: + record.write(f"tail=sent {seq} {e.utc()}\n" if state == "tail-sent" + else f"tail=done {seq} 0 {e.utc()}\n") + before = len(tool_calls(where, "tmux")) + again = e.run(LA, argv, extra=extra, first=[where]) + assert again.code == 2, f"the rerun exited {again.code}, not 2: {again.err[-800:]}" + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + sent = [c for c in tool_calls(where, "tmux")[before:] if "send-keys" in c["argv"]] + assert not sent, f"the rerun typed into a pane: {sent}" + assert "after-COMMIT steps" in again.err, f"stderr names no after-COMMIT steps: {again.err[-800:]}" + + +# ------------------------------------------------------------ section 4.3: the rival +@CLIENT +def test_a_lookup_never_mistakes_a_rivals_row_for_the_lost_write(estates): + """Section 4.3's rival: LA's first commit is lost before it lands, and + LA's next call, its lookup try or its retry's snapshot, is held while LB + commits the same line's text. A lookup by LA's id finds LA's write + absent, never LB's row: LA retries under its own id and exits 0, and + the journal holds each id once.""" + e = estates() + gate = fifo(e, "call-3") + argv = note(LA, "rival") + w = e.launch(LA, argv, arms=[BEFORE, {"mode": "hold", "call": 3, "fifo": str(gate)}]) + held = hold_point(gate, w) + rival = e.run(LB, argv) + assert rival.code == 0, rival.err[-800:] + held.close() + assert w.wait() == 0, w.err[-800:] + mine, theirs = w.write_ids()[0], rival.write_ids()[0] + assert mine != theirs + assert set(w.write_ids()) == {mine}, "the lost write's retry took another id" + ids = [r["write_id"] for r in e.journal_after()] + assert ids.count(mine) == 1 and ids.count(theirs) == 1, ids + + +# ============================================================ PHASE W: D8 and AT-F34 (T245) +# +# THE PENDING RECORD'S RERUN AND THE TAIL, ONCE (AT-D8, TP:3265-3528; AT-F34, +# TP:6941-7036), on T244's workstation and shim. The record is +# `/openRepoTools/register-pending/`, `key=value` lines with one +# `call= ` line per commit call that +# may have committed; its claim files are `.take.`; the done mark is +# `/openRepoTools/register-done/`. Every kill is sent on a +# FIFO signal, never after a sleep: the shim's `timer` joined to a FIFO +# blocks once the real `psql` has returned, and the test kills the run's +# process group there, so the row has landed and the writer cannot print. +# +# THE TAKEOVER (Q-L2-6; R26, lane 4's answer to 36): on this host a run that +# takes a killed run's record over waits past that call's `not_after`, +# whether its own snapshot passes or fails, on the record's clock when it +# has none of its own; `[snapshot-fails]` holds it. A record from another +# host keeps "calls at once" (T246's `[too-early]`). +# +# A `tail` RECORD A TAKEOVER CANNOT USE (R-F13c-23; lane 3's batch 8, #165 +# 6780753157, Q46, Q34 and text change 1): a takeover of a record holding +# `tail` whose first snapshot has no `found=` refuses 2 and keeps the record, +# giving the effect check and, where the effect is not there, the same command +# with no `LANES_WRITE_ID` once the record is `LANES_REGISTER_PENDING_MAX_AGE_S` +# past its `updated_utc`, printing that time, never `LANES_WRITE_ID=new` +# (Y:5207, Y:5215); a `tail` of neither Y:5179 form is a record that cannot be +# read, T412's 2, the record kept. +# +# `found`'s two lines (R-F13-2, R-F13-7; F13b-OWED items 11 and 15): every +# `found` answer the shim and the fakes give here carries `workstation=` and +# `utc=` after its seq line, as lane 1's T114-fu3 prints them (`found `, +# `workstation=`, `utc=`, then the `file=` rows), and AT-F34's +# no-record reruns choose Y:5347's text for this host or another by +# `workstation=`, naming the entry's UTC from `utc=`. +# +# THE TAIL'S `gh` CALL IS THE COMMENT (T245c; lane 4's triage, #165 +# 6833813092): a case counts only `gh issue comment` and `gh pr comment` +# (`comments`), never `claim`'s Rule 1 reads, which every run of `claim` +# makes before the seam, a killed one and a refused one included. +# +# They FAIL UNTIL L4-B2, L4-B5 and L4-B6 land; `[snapshot-fails]` until lane +# 4's T417 and lane 1's T114 take the record's clock (Q-L1-1); the texts for 3 +# until lane 4's T417, T433 and T434; the `tail` record a takeover cannot use +# until lane 4's T418 takes Q46 and Q34; and the `found` lines until lane 1's +# T114 and lane 5's T111 print them. Without PostgreSQL 16 each skips, except +# the texts for 3, which read the scripts and run here. + +DONE = Path("openRepoTools") / "register-done" +CALL_LINE = re.compile(r"^-?[0-9]+ \S+ \S+ [0-9a-f]{64} /\S+$") +NO_RECONNECT = {"mode": "no-reconnect", "as": "before", "match": "commit_write", "call": 1} +#: D8's text for 3 (R29; GB §4.3; W:459-460, W:5647-5649; R2's S5), byte for byte. +D8_TEXT_3 = "may not be written; run the printed `LANES_WRITE_ID` rerun, which writes it once or finds it" +_KILLS = itertools.count(1) + + +def killed(e: Estate, lane: str, argv: Sequence[str], **options) -> Tuple[Writer, str]: + """AT-D8's killed write: the first commit call's real `psql` returns, + the shim blocks on its FIFO, and the test kills the run's group there.""" + gate = fifo(e, f"killed-{next(_KILLS)}") + arms = [*options.pop("arms", ()), {"mode": "timer", "match": "commit_write", "call": 1, "fifo": str(gate)}] + w = e.launch(lane, argv, arms=arms, **options) + held = hold_point(gate, w) + os.killpg(w.proc.pid, signal.SIGKILL) + held.close() + w.wait() + write_id = w.write_ids()[0] + assert e.has_write(write_id), "the killed call's row has not landed" + return w, write_id + + +def exit3(e: Estate, lane: str, argv: Sequence[str], **options) -> Tuple[Writer, str, str]: + """A run ended 3 by `no-reconnect`: the writer, its id and generation, + read from its report.""" + w = e.run(lane, argv, arms=[NO_RECONNECT], **options) + assert w.code == 3, f"exit {w.code}, not 3: {w.err[-800:]}" + ids, gens = WRITE_ID_SAID.findall(w.err), GENERATION_SAID.findall(w.err) + assert ids and gens, f"the report names no LANES_WRITE_ID rerun: {w.err[-800:]}" + return w, ids[-1], gens[-1] + + +def the_record(e: Estate) -> dict: + """The one pending record: its path, its first value of each key, and + its `call` lines.""" + records = pending(e) + assert len(records) == 1, f"{len(records)} pending records" + fields: Dict[str, str] = {} + calls: List[str] = [] + for line in records[0].read_text(encoding="utf-8").splitlines(): + key, eq, value = line.partition("=") + if eq and key == "call": + calls.append(value) + elif eq: + fields.setdefault(key, value) + return {"path": records[0], "fields": fields, "calls": calls} + + +def take_files(e: Estate) -> List[str]: + where = e.state / PENDING + return sorted(p.name for p in where.glob("*.take.*")) if where.is_dir() else [] + + +def mark_of(e: Estate, write_id: str) -> Path: + return e.state / DONE / write_id + + +def gh_reads(where: Path) -> List[dict]: + """The fake `gh`'s `issue view` calls: `claim`'s Rule 1 reads, which + each run of the verb makes at its step 2, before the seam, an attempt's + child or a replay's (`gh_reads_live`; Y:5043, Y:5326).""" + return [c for c in tool_calls(where, "gh") if c["argv"][:2] == ["issue", "view"]] + + +def comments(where: Path) -> List[dict]: + """The fake `gh`'s comment calls, `issue comment` or `pr comment`: a + verb's tail. `claim`'s is its step 6, `gh_comment` once `write_event` + returns (`91bfa229:lanes-edit.sh:24731-24737`), which under the flag is + after the commit, from T414's seam: `commit_publish` returns through + `cp_seam` (`:3710-3716`), whose committed step writes `tail`, sets + `REGISTER_SEQ` and returns 0 (`:4247-4264`). This module counts those + calls and never reads their text; T421 changes that text, and the order + module's cases for T243 and T421 assert it (lane 4, #165 6852035770). + Never Rule 1's reads, `issue view --comments` and `pr list`, which + `claim` makes at its step 2, before the seam (`gh_reads_live`; Y:5043), + and which decide nothing (T245c; lane 4's triage, #165 6833813092; + T248a's `comment_calls` has the same shape).""" + return [c for c in tool_calls(where, "gh") if c["argv"][:1] in (["issue"], ["pr"]) + and c["argv"][1:2] == ["comment"]] + + +def sidecar_text(lane: str, generation: int, utc: str, state: str = "RUNNING") -> str: + """A whole lane-state snapshot, as `lane_state_put` writes one + (`c8acf67c:lanes-edit.sh:23002`): `schema: 1` and every key its writer + writes, none empty. A snapshot short of one is not a whole record, and + every writer refuses it (`lane_sidecar_schema_ok`, `:22826-22846`; #115), + so a sidecar of three keys was never followed (T245h; lane 4's triage, + #165 6833813092).""" + return (f"schema: 1\nlane: {lane}\nstate: {state}\ngeneration: {generation}\noperation: none\n" + f"owner: none\nagent: none\nprofile: none\nworkstation: none\nkind: none\nupdated: {utc}\n") + + +def sidecars(e: Estate, lanes: Sequence[str] = (LA, LB, LC)) -> Path: + root = e.root / "lane-state" + utc = e.utc() + for lane in lanes: + (root / lane).mkdir(parents=True, exist_ok=True) + (root / lane / "lane-state.yaml").write_text(sidecar_text(lane, 1, utc), encoding="utf-8") + return root + + +def generation_of(root: Path, lane: str) -> str: + for line in (root / lane / "lane-state.yaml").read_text(encoding="utf-8").splitlines(): + if line.startswith("generation:"): + return line.split(":", 1)[1].strip() + return "" + + +def tail_rerun(e: Estate, lane: str, argv: Sequence[str], where: Path, extra: Optional[dict] = None): + """AT-D8's tail oracle around a killed write: no effect before the rerun; + the rerun exits 0 with one row; the record wrote `tail=sent`, then + `tail=done`, and is removed; the mark holds `done`; a second rerun under + the id, with its generation (an id with no generation and no record is + refused 2 before any call: pending-record.md §5.2, R08; T245d), finds + the mark and runs none of the tail, so the effects stay at one + (TP:3366-3367): no comment and no `tmux` call of its own, and the + callers' checks of the sidecars and the handoff come after it. The + logging `git` wrapper logs every `git` call a run makes, so its count is + no oracle of the tail. Returns the rerun and the id.""" + _, write_id = killed(e, lane, argv, first=[where], extra=extra) + before = len(tool_calls(where)) + again = e.run(lane, argv, first=[where], extra=extra) + assert again.code == 0, f"the rerun exited {again.code}: {again.err[-800:]}" + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + assert pending(e) == [] and take_files(e) == [] + assert "done" in mark_of(e, write_id).read_text(encoding="utf-8") + effects = len(comments(where)), len(tool_calls(where, "tmux")) + third = e.run(lane, argv, first=[where], extra=dict(extra or {}, LANES_WRITE_ID=write_id, + LANES_WRITE_GENERATION=e.c.switch()["generation"])) + assert third.code == 0, f"the second rerun exited {third.code}: {third.err[-800:]}" + assert (len(comments(where)), len(tool_calls(where, "tmux"))) == effects, "a second rerun ran the tail again" + return again, write_id, before + + +# ------------------------------------------------------------ D8: the record +@CLIENT +def test_a_committed_write_leaves_no_pending_record(estates): + """After a committed write no record and no claim file remains, and the + done mark does: 0600 in a 0700 directory, holding the id, the seq and + the `tail` value, `done`.""" + e = estates() + w = e.run(LA, note(LA, "committed")) + assert w.code == 0, w.err[-800:] + write_id = one_entry(e, w) + assert pending(e) == [] and take_files(e) == [] + mark = mark_of(e, write_id) + assert mark.stat().st_mode & 0o777 == 0o600 and mark.parent.stat().st_mode & 0o777 == 0o700 + text = mark.read_text(encoding="utf-8") + assert write_id in text and str(e.journal_after()[-1]["seq"]) in text and "done" in text, text + + +@CLIENT +def test_a_plain_rerun_reuses_the_pending_write_id_once_and_says_so(estates): + """After exit 3 the record names the id and holds the call's `call` line + (base, clock, `not_after`, the `files` digest, the attempt directory), + and no other key says the write is in doubt. A plain rerun says on + stderr that it reuses the id, exits 0, leaves one row and removes the + record; a second plain rerun is a new write, under a new id.""" + e = estates() + argv = note(LA, "plain rerun") + _, write_id, _ = exit3(e, LA, argv) + rec = the_record(e) + assert rec["fields"].get("write_id", "").lower() == write_id + assert len(rec["calls"]) == 1 and CALL_LINE.match(rec["calls"][0]), rec["calls"] + again = e.run(LA, argv) + assert again.code == 0, again.err[-800:] + assert write_id in again.err and "reus" in again.err.lower(), again.err[-800:] + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + assert pending(e) == [] + third = e.run(LA, argv) + assert third.code == 0 and set(third.write_ids()) and write_id not in third.write_ids() + + +@CLIENT +def test_a_write_killed_by_an_outer_timer_is_retried_under_its_own_id(estates): + """A write killed after its call returned prints nothing; its plain + rerun reuses the id, exits 0, and leaves one row.""" + e = estates() + argv = note(LA, "killed") + _, write_id = killed(e, LA, argv) + again = e.run(LA, argv) + assert again.code == 0, again.err[-800:] + assert set(again.write_ids()) <= {write_id} + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + + +@CLIENT +def test_a_rerun_with_other_arguments_does_not_take_the_pending_id(estates): + """A rerun with other arguments is a write of its own: it never sends + the pending id, and the record stays.""" + e = estates() + _, write_id, _ = exit3(e, LA, note(LA, "first arguments")) + other = e.run(LA, note(LA, "other arguments")) + assert other.code == 0, other.err[-800:] + assert write_id not in other.write_ids() + assert the_record(e)["fields"].get("write_id", "").lower() == write_id + + +@CLIENT +def test_lanes_write_id_of_another_write_is_refused_2_and_the_pending_record_kept(estates): + """G6 (pending-record.md §5.2, T412's G6; lane 3's ruling "G6 stands", + #165 6834271321; T245g): after exit 3 the record of a command line holds + write P and its `call` line. `LANES_WRITE_ID`, set to a fresh id with + its generation, is refused with 2: the fresh id is never sent, no + commit call is made, the record still holds P and its `call` line, and + stderr names P and its lookup, `lanes-index journal --write-id P`. A + fresh id cannot win: it would drop another write's in-doubt record + without F3's lookup, or write with no record, and Y forbids both + (Y:5171, Y:5196-5198, Y:5215). The test was + `test_lanes_write_id_wins_over_a_pending_record`, from the test plan's + "wins over a record" (TP:3350 at B19, :3359 at `a91df2c4`), which + predates the contract and G6.""" + e = estates() + argv = note(LA, "explicit id") + _, pending_id, generation = exit3(e, LA, argv) + rec = the_record(e) + mine = str(uuid.uuid4()) + w = e.run(LA, argv, extra={"LANES_WRITE_ID": mine, "LANES_WRITE_GENERATION": generation}) + assert w.code == 2, f"exit {w.code}, not 2: {w.err[-800:]}" + sent = [c for c in w.calls() if mine in (c.get("stdin") or "")] + assert w.commit_calls() == [] and not sent, (w.commit_calls(), sent) + kept = the_record(e) + assert kept["fields"].get("write_id", "").lower() == pending_id and kept["calls"] == rec["calls"], kept + assert pending_id in w.err and f"lanes-index journal --write-id {pending_id}" in w.err, w.err[-800:] + + +@CLIENT +def test_lanes_write_id_of_the_records_own_write_takes_it_over_past_its_age(estates): + """The case that holds under G6 (pending-record.md §5.2, :225: an id + whose record holds it is "the record taken over, at any age"; T245g): + after exit 3, with the record past the maximum age + (`LANES_REGISTER_PENDING_MAX_AGE_S=2`), where a plain rerun writes anew + under a new id, the printed rerun, `LANES_WRITE_ID` of the record's own + write with its generation, takes it over: exit 0, every commit call + under that id, one row for it, and the record removed.""" + e = estates() + argv = note(LA, "own id past the age") + _, pending_id, generation = exit3(e, LA, argv) + time.sleep(3) + w = e.run(LA, argv, extra=dict(MAX_AGE, LANES_WRITE_ID=pending_id, LANES_WRITE_GENERATION=generation)) + assert w.code == 0, w.err[-800:] + assert set(w.write_ids()) == {pending_id}, w.write_ids() + assert [r["write_id"] for r in e.journal_after()].count(pending_id) == 1 + assert pending(e) == [] and take_files(e) == [] + + +@CLIENT +@pytest.mark.parametrize("case", ["absent", "found"]) +def test_status_lists_and_resolves_pending_records(estates, case): + """`lanes-index status` lists each pending record and reports whether + its id is in the journal: [absent], the first call lost before it + landed; [found], after it.""" + e = estates() + arm = dict(NO_RECONNECT, **{"as": "before" if case == "absent" else "after"}) + w = e.run(LA, note(LA, f"status {case}"), arms=[arm]) + assert w.code == 3, w.err[-800:] + write_id = WRITE_ID_SAID.findall(w.err)[-1] + env = dict(e.env, PATH=os.pathsep.join([str(e.psql_dir(f"status-{case}")), e.env["PATH"]])) + status = subprocess.run([str(e.bindir / "lanes-index"), "status"], capture_output=True, text=True, + env=env, check=False, timeout=120) + assert write_id in status.stdout, status.stdout[-800:] + line = next(l for l in status.stdout.splitlines() if write_id in l) + if case == "found": + assert str(e.journal_after()[-1]["seq"]) in line, line + else: + assert "absent" in line or "not in the journal" in line, line + + +# ------------------------------------------------------------ D8: each verb's tail +@CLIENT +def test_a_rerun_that_finds_its_write_committed_runs_the_verbs_after_commit_tail(estates): + """`rename-lane`'s tail: `lane_state_rename` moves the old control root + to the new name once; the handoff file at the new path holds one + `RENAMED from ` line, under its header, never two (TP:3384-3389), + moved there, with nothing left at the old path, in the lane checkouts + and on the lanes origin's `main`, where it is pushed; the fake `gh` + logs one comment per held claim (none here). The estate + holds the lane's handoff in its checkout (TP:3315): `seed_handoff`.""" + e = estates() + where, root = loggers(e), sidecars(e) + new = "repoA-9" + extra = {"LANES_LANE_STATE_ROOT": str(root)} + seed_handoff(e, LA) + again, _, _ = tail_rerun(e, LA, ["rename-lane", LA, new, "T245", "--no-github"], where, extra) + assert (root / new).is_dir() and not (root / LA).exists() + # The move and stamp are made in the lane's own register checkout, `PW_C`, + # `-lanes/` (`8ba10b4e:lanes-edit.sh:25251-25256`), which the + # estate makes at `co--lanes/`, never in W (T245l; PM33A-TRIAGE L2). + # It is a move (TP:3386-3387, "The handoff's move and stamp run once"), so + # no checkout that holds the moved file still holds the old one; and it is + # pushed (Y:5092's S3-S4 commit; `f42d2c85:lanes-edit.sh:21173-21177` warns + # where the push is not confirmed), so the lanes origin's `main` holds the + # moved file, stamped once, and nothing at the old path (RV20's NIT 15). + old, moved, stamp = lane_handoff(LA), lane_handoff(new), f"RENAMED from {LA} " + + def one_stamp(label: str, lines: List[str]) -> None: + assert sum(l.startswith(stamp) for l in lines) == 1 and lines[3:4] and lines[3].startswith(stamp), ( + f"{label} does not hold one Rule 3 stamp under its header: {lines[:6]}") + + handoffs = list(e.root.glob(f"co-*-lanes/*/{moved}")) + assert handoffs, f"no lane checkout holds {moved}" + for h in handoffs: + co = h.parents[len(Path(moved).parts) - 1] + assert not (co / old).exists(), f"{co} holds both {old} and {moved}: a copy, not a move" + one_stamp(str(h), h.read_text(encoding="utf-8").splitlines()) + on_main = run_git(e.origin, "ls-tree", "-r", "--name-only", "main", "--", "handoffs/", env=e.env).split() + assert moved in on_main and old not in on_main, f"the lanes origin's main holds {on_main}: the move is not pushed" + one_stamp(f"{e.origin} main:{moved}", run_git(e.origin, "show", f"main:{moved}", env=e.env).splitlines()) + assert comments(where) == [] + + +def by_hand(e: Estate, message: str, *change: str, emptied: str = "") -> None: + """A person's commit on the lanes origin, made from the estate's seed + clone brought up to the origin first: the file `emptied` names, if any, + emptied, and `git `, then commit and push. Every checkout a + later run makes holds it.""" + run_git(e.seed, "pull", "-q", "--ff-only", "origin", "main", env=e.env) + if emptied: + (e.seed / emptied).write_text("", encoding="utf-8") + run_git(e.seed, *change, env=e.env) + run_git(e.seed, "commit", "-q", "-m", message, env=e.env) + run_git(e.seed, "push", "-q", "origin", "main", env=e.env) + + +def lane_handoff(lane: str) -> str: + """A handoff named for its lane, `session-handoff--lane-.md`: + the one shape `rename-lane` moves, to the same date with `-lane-` + (Amendment 16(d); `8ba10b4e:lanes-edit.sh:28903-28925`). Test 4's rows + name `handoffs/.md` (`w_row`), which it stamps where it is and + never moves (`:29155-29156`).""" + return f"handoffs/session-handoff-2026-05-01-lane-{lane}.md" + + +def seed_handoff(e: Estate, lane: str) -> str: + """AT-D8's `rename-lane` estate, "the lane's handoff file in its + checkout" (TP:3315). A run looks for the handoff its row names in the + lane's own register checkout, `-lanes/`, cloned from the lanes + origin, and where that checkout holds none it moves and stamps nothing + (`8ba10b4e:lanes-edit.sh:25064-25065`, `:29133-29136`). So the handoff, + a header block and the blank line Rule 3's stamp goes under + (`rl_stamp_build`, `:25000-25021`), is committed on the lanes origin + from the estate's seed clone, before any run, and `lane`'s row is + pointed at it by `replace-in-row`, the verb the engine names for that + (`:24985`). Returns its path in a checkout.""" + rel = lane_handoff(lane) + (e.seed / rel).parent.mkdir(parents=True, exist_ok=True) + (e.seed / rel).write_text(f"# Handoff: lane {lane}\nT245's seed.\n\nNothing is owed.\n", encoding="utf-8") + run_git(e.seed, "add", "--", rel, env=e.env) + run_git(e.seed, "commit", "-q", "-m", f"handoff({lane}): T245's seed", env=e.env) + run_git(e.seed, "push", "-q", "origin", "main", env=e.env) + w = e.run(lane, ["replace-in-row", lane, f"handoffs/{lane}.md", rel, "T245: a handoff named for its lane"]) + assert w.code == 0, f"replace-in-row exited {w.code}: {w.err[-800:]}" + return rel + + +@CLIENT +@pytest.mark.parametrize("verb", ["RESUMED", "ENDED", "RESUMED-moved-since"]) +def test_the_after_commit_tail_of_log_runs_once_when_a_rerun_finds_the_write(estates, verb): + """`log`'s tail for a verb `lane_state_follow` moves: the sidecar goes + from g to g+1 once. [RESUMED-moved-since]: another act moved the lane + since the kill, the replay's fence refuses, the sidecar stays as that act + left it, a note says the lifecycle moved under the line, exit 0.""" + e = estates() + where, root = loggers(e), sidecars(e) + extra = {"LANES_LANE_STATE_ROOT": str(root)} + argv = ["log", verb.split("-")[0], f"lane:{LA}", f"T245 {verb}"] + _, write_id = killed(e, LA, argv, first=[where], extra=extra) + assert generation_of(root, LA) == "1", "the tail ran before the rerun" + if verb == "RESUMED-moved-since": + (root / LA / "lane-state.yaml").write_text(sidecar_text(LA, 7, e.utc()), encoding="utf-8") + again = e.run(LA, argv, first=[where], extra=extra) + assert again.code == 0, again.err[-800:] + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + if verb == "RESUMED-moved-since": + assert generation_of(root, LA) == "7" and "moved" in again.err, again.err[-800:] + else: + assert generation_of(root, LA) == "2" + + +@CLIENT +def test_the_after_commit_tail_of_release_runs_once_when_a_rerun_finds_the_write(estates): + """`release`: its one comment, once, after the rerun finds the write.""" + e = estates() + where = loggers(e) + assert e.run(LA, claim(OBJ, "--no-github")).code == 0 + tail_rerun(e, LA, ["release", OBJ, "T245", "--home", OBJ.split("#")[0]], where) + assert len(comments(where)) == 1, comments(where) + + +@CLIENT +def test_the_after_commit_tail_of_request_handoff_types_once_in_the_kept_child_and_polls(estates): + """`request-handoff`: the kept child ran the tail, so the fake `tmux` + logs one `send-keys`, and the poll reads and ends the run; the request is + in the register once. Run with `--wait 2` and `LANES_POLL_SECONDS=1`. + The fake `tmux` answers the bound window as live (T248b's + `loggers(e, tmux_live=True)`): the silent one answers no window read, so + the window never reads live and nothing is typed (T245m; PM33A-TRIAGE L3; + N2-17b's reading 2).""" + e = estates() + where = loggers(e, tmux_live=True) + w = e.run(LA, ["request-handoff", LC, "--session", session_of(LC), "--wait", "2"], arms=[AFTER], + first=[where], extra={"LANES_POLL_SECONDS": "1"}) + assert w.code in (0, 2), w.err[-800:] + one_entry(e, w) + sent = [c for c in tool_calls(where, "tmux") if "send-keys" in c["argv"]] + assert len(sent) == 1, sent + assert len(tool_calls(where, "tmux")) > 1, "the poll never read the pane" + + +#: The two rows `retire-rows` retires: lane B, and the first burst lane, which +#: no estate binds (T243g binds lane C, whose `STARTED` line `retire-rows` +#: refuses; T245k). +DORMANT = (LB, BURST[0]) + + +@CLIENT +def test_the_after_commit_tail_of_retire_rows_runs_once_when_a_rerun_finds_the_write(estates): + """`retire-rows`: each retired lane's sidecar goes through its follow + once, with its own pre-image. + + THE ROWS ARE DORMANT FIRST (Amendment 19(a); T245k; lane 4's triage, + #165 6833813092). `retire-rows` takes only a dormant row: its object log + carries no lane-kind line at all, an `ENDED` or `RETIRED` last line + included, which it refuses as a lane already CLOSED + (`c8acf67c:lanes-edit.sh:21966-21977`), and its state cell is not + Amendment 13(a)'s phrase naming anything but `ENDED`, `RETIRED` or + `MIGRATED` (`:21995-21999`). Test 4's rows read `LIVE` (`w_row`), so each + lane's cell is first set to `ENDED` by `set-row-state`, which replaces the + row line alone, writes no log line and has no tail (TP:3368-3371). Its + log keeps no lane-kind line. Lane C is not one of them: T243g's estate + binds it with a `STARTED` line.""" + e = estates() + where, root = loggers(e), sidecars(e, (LA, *DORMANT)) + for lane in DORMANT: + ended = e.run(lane, ["set-row-state", lane, f"ENDED {DOT} dormant, for T245's retire-rows tail"]) + assert ended.code == 0, f"set-row-state {lane} exited {ended.code}: {ended.err[-800:]}" + tail_rerun(e, LA, ["retire-rows", *DORMANT, "--reason", "T245"], where, {"LANES_LANE_STATE_ROOT": str(root)}) + assert [generation_of(root, lane) for lane in DORMANT] == ["2", "2"] + + +@CLIENT +def test_the_after_commit_tail_of_start_record_runs_once_when_a_rerun_finds_the_write(estates): + """`start-record`: `lane_state_follow` runs once after the lock: one + move of the lane's sidecar. + + The call is in T427's form, as the arm parses it + (`7893339:lanes-edit.sh:29679-29722`, `SR_USE` at `:15521`): the lane, + then the log line's verb, `RESUMED`, then `--state` for the row that is + there, then `--log` with `lane:`, the arrow and a payload. + `Estate.launch` sets `LANES_LANE` to the lane and `LANES_SESSION` to its + transcript uuid, which the arm asks for before any read + (`:29736-29743`). T245's `--session` was no word of the arm's: it + refused it as the verb, 2 (`:29686`; line N2-23's r1b). The lane's own + checkout comes from N2's T243t (`c900063a`, `write_lanes_of`'s + `start-record` arm; lane 3, #165 6859307928).""" + e = estates() + where, root = loggers(e), sidecars(e) + argv = ["start-record", LA, "RESUMED", "--state", f"LIVE {DOT} T245 start-record", + "--log", f"lane:{LA}", ARROW, "T245 start-record"] + tail_rerun(e, LA, argv, where, {"LANES_LANE_STATE_ROOT": str(root)}) + assert generation_of(root, LA) == "2" + + +@CLIENT +def test_a_rerun_after_a_3_replays_the_verbs_tail_once(estates): + """`no-reconnect` ends the first `claim` with 3 (its first call landed), + abandoning its kept child, with no comment made. A plain rerun finds the + record, its snapshot finds the id, it replays at `s-1` (one `--at` call) + and runs the tail: one comment, exit 0.""" + e = estates() + where = loggers(e) + argv = claim(OBJ) + w = e.run(LA, argv, arms=[dict(NO_RECONNECT, **{"as": "after"})], first=[where]) + assert w.code == 3 and comments(where) == [], w.err[-800:] + again = e.run(LA, argv, first=[where]) + assert again.code == 0, again.err[-800:] + assert len(comments(where)) == 1, comments(where) + + +@CLIENT +@pytest.mark.parametrize("state", ["sent-child-never-read", "owed-mark"]) +def test_a_sent_record_whose_child_never_read_committed_replays_and_an_owed_mark_runs_no_tail(estates, state): + """[sent-child-never-read]: a record holding `tail=sent` while the dead + child's `seam.out` says `none`: the owner removes `tail` and replays, + once, one comment, exit 0 (Y:5342; lane 3's rule, #165 6838365309 and + 6838735040). The takeover reads the `seam.out` in the directory of the + record's `call=` line (Y:5173), so the case writes `none` there: the + child, killed while its parent's commit call was held, wrote none + (it writes one only at its seam, `8ba10b4e:lanes-edit.sh:5150-5164`, + `:5453`). The replay ran once: its child made `claim`'s Rule 1 reads, + `gh issue view`, as many times as the killed run's child did, since a + replay reads `gh_reads` again (Y:5326) and the takeover's parent runs + no verb. Y:5043's cache, "in the write's directory across attempts", + does not reach the replay: the engine keeps it in the run's directory, + `//`, which every attempt of one run shares (T413; + `f42d2c85:lanes-edit.sh:17911-17912`), and a replay makes no commit + call, so it is no attempt, and runs in a fresh run segment, + `//1` (`:8548-8550`; lane 3, #165 6770688147, "The fresh + run segment and what the replay reads: confirmed"). Were the cache to + reach across a replay, its child would make no reads and this count + would fail, not pass (RV20's NIT 14). `tail` was removed: the done + mark holds the replayed tail's `done`, not the `sent` the record held, + which a run that keeps the `sent` row writes the mark from + (Y:5338-5342). [owed-mark]: a mark + holding `owed `; a rerun + under `LANES_WRITE_ID`, with its generation (T245d), runs no tail, adds + no comment, and names the steps as owed again. The mark is written in + its contract's form, one `key=value` a line, `write_id`, `seq`, `tail`, + `pid`, `host`, `container` and `utc` (pending-record.md §6), the form + the run reads (`wr_done_read`, `c8acf67c:lanes-edit.sh:7778`): one of + another form is no mark (T245f).""" + e = estates() + where = loggers(e) + argv = claim(OBJ) + _, write_id = killed(e, LA, argv, first=[where]) + rec = the_record(e) + if state == "sent-child-never-read": + with rec["path"].open("a", encoding="utf-8") as f: + f.write(f"tail=sent {e.journal_after()[-1]['seq']} {e.utc()}\n") + assert len(rec["calls"]) == 1, rec["calls"] + old = Path(rec["calls"][0].split(None, 4)[4].strip()) + assert old.is_dir(), f"the record's call= directory {old} is not there" + (old / "seam.out").write_text("none\n", encoding="utf-8") + killed_reads = len(gh_reads(where)) + again = e.run(LA, argv, first=[where]) + assert again.code == 0 and len(comments(where)) == 1, (again.err[-800:], comments(where)) + assert killed_reads > 0 and len(gh_reads(where)) == 2 * killed_reads, ( + f"the replay's child made {len(gh_reads(where)) - killed_reads} of claim's Rule 1 reads, " + f"the killed run's child {killed_reads}: not one replay") + mark = mark_of(e, write_id).read_text(encoding="utf-8") + assert pending(e) == [] and re.search(r"(?m)^tail=done ", mark), f"tail was not replayed to done: {mark!r}" + else: + mark = mark_of(e, write_id) + mark.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + fields, utc = rec["fields"], e.utc() + mark.write_text(f"write_id={write_id}\nseq={e.journal_after()[-1]['seq']}\ntail=owed {utc}\n" + f"pid={fields.get('pid', '')}\nhost={fields.get('host', '')}\n" + f"container={fields.get('container', '')}\nutc={utc}\n", encoding="utf-8") + mark.chmod(0o600) + rec["path"].unlink() + again = e.run(LA, argv, first=[where], extra={"LANES_WRITE_ID": write_id, + "LANES_WRITE_GENERATION": e.c.switch()["generation"]}) + assert again.code == 0 and comments(where) == [] and "owed" in again.err, again.err[-800:] + + +@CLIENT +@pytest.mark.parametrize("case", ["rename-lane-stops", "rename-lane-differs", "request-handoff-differs"]) +def test_a_replay_of_rename_lane_or_request_handoff_that_stops_or_differs_runs_none_of_the_tail(estates, case): + """The write is committed either way. [rename-lane-stops]: the handoff + file emptied, so the replay stops before its write; [rename-lane-differs]: + the handoff removed by hand, so the row's handoff cell is left as it is + and the change set differs first at `lanes/LANES.md` (TP:3447-3450 at + `f4591a08`; in Y's frame, TP:3424-3427 said the handoff "is moved to + the new path by hand"; lane 3, #165 6851669848). Each writes + the mark `owed `, removes the record, prints one WARNING, exits 0 + and runs no tail. [request-handoff-differs]: the fake `tmux` reports the + holder's window gone; the run exits 2, saying the request is written at + seq `` and it did not wait for the release. + + The two `rename-lane` forms need the lane's handoff in its checkout + (TP:3315), named for its lane so that the rename changes the row's + cell (`seed_handoff`, T245r). Each run clones its own W and lane + checkouts from the lanes origin (`Estate.launch`), so the rerun never + sees a change made in the killed run's checkouts: the handoff is + emptied or removed by hand on the origin (`by_hand`), before the rerun. + + [rename-lane-stops] empties the handoff, as lane 3 ruled (#165 + 6841366245) and as the test plan says since fold 1112p (`e7b1ed5e`): + the handoff file "is committed empty in the lane's checkout", and "the + replay's stamp proof (Y:5092) refuses it with 5 before the seam" + (TP:3442-3444 at `b8e3df74`; in Y's frame, TP:3419-3421 said "removed + from"). The handoff is still there, tracked under `handoffs/` with no + pending change, so the replay's checks pass and `rl_stamp_build`'s + proof refuses it with 5 before the seam + (`049c0da1:lanes-edit.sh:20870-20874`), Y:5092's "a handoff that + cannot be stamped refuses". A handoff removed instead + keeps Amendment 16(d)'s note on primary too, so its replay takes the + differs path (Y:5330; the same ruling). The case tells its form from + [rename-lane-differs] by Y:5329's text: "replayed at seq `` it now + stops before its write (exit 5: ``), so its after-COMMIT + steps did NOT run: ``". The WARNING carries only the child's last + line (`pw_last_line`, `f42d2c85:lanes-edit.sh:8581`, `:8629`), and + after the stamp proof's refusal, "the handoff at `` cannot take + Rule 3's stamp" (`:20908`), the rename's undo prints its note, "the + rename was rolled back: ... nothing was committed" (`rename_undo`, + `:2188`), as it does after `rename-lane`'s other refusals with 5 in + its transaction. So the case cannot tell the stamp proof's 5 from those + by the text, as RV20's NIT 13 asked (line ID-21's r02 saw the note): it + requires exit 5 with that note as the last line, and `` naming + steps (TP:3423, "names the steps that did NOT run"). And there is no + "another change set", the differs form's words. It fails until lane 4's + T422, whose stamp check runs before the seam on primary (tasks:1280 at + `717ed04d`, "the stamp's checks run before the seam"; lane 3, #165 + 6841366245). + + [rename-lane-differs] removes the handoff by a commit on the lanes + origin, as lane 3 ruled (b) (#165 6851669848): a source that is not + there passes the checks and takes Amendment 16(d)'s note, which leaves + the row's handoff cell as it is (`9a314c09:lanes-edit.sh:24914-24915`, + `:24981-24984`), so the change set differs first at `lanes/LANES.md`, + since `pw_first_diff` sorts bytewise (`:8718`). A hand move onto the + rename's new path, as TP:3424 had it, stops instead: `rename-lane` + refuses an occupied target with 2 before it reads the source + (`:24781-24782`), so this form's earlier passes, which checked no text, + went by the stop path (line ID-21's r01 and r06). The case tells this + form from the stops form by Y:5330's text: "replayed at seq `` it + now reaches the seam with another change set, the first path whose + digest differs being lanes/LANES.md" (`f42d2c85:lanes-edit.sh:8587`), + and no "now stops before its write" (RV20's finding 7). Its `` + names a step, as the stops form's does: the pattern ends with a word + character after "did NOT run: " (RV22's NIT 8). `pw_tail_owed` puts + `` there (`7893339:lanes-edit.sh:10905`), and `rename-lane`'s + row of it begins "lane 's lifecycle directory moved" (`:8360`).""" + e = estates() + where, root = loggers(e), sidecars(e) + extra = {"LANES_LANE_STATE_ROOT": str(root)} + argv = (["request-handoff", LC, "--session", session_of(LC), "--no-wait"] if case.startswith("request") + else ["rename-lane", LA, "repoA-9", "T245", "--no-github"]) + handoff = seed_handoff(e, LA) if case.startswith("rename") else "" + _, write_id = killed(e, LA, argv, first=[where], extra=extra) + seq = e.journal_after()[-1]["seq"] + if case == "rename-lane-stops": + by_hand(e, "T245: the handoff emptied by hand", "add", "--", handoff, emptied=handoff) + elif case == "rename-lane-differs": + by_hand(e, "T245: the handoff removed by hand", "rm", "-q", "--", handoff) + else: + (where / "tmux").write_text("#!/bin/sh\nexit 1\n", encoding="utf-8") + again = e.run(LA, argv, first=[where], extra=extra) + if case.startswith("request"): + assert again.code == 2 and f"seq {seq}" in again.err, again.err[-800:] + else: + assert again.code == 0 and again.err.count("WARNING") == 1, again.err[-800:] + assert "owed" in mark_of(e, write_id).read_text(encoding="utf-8") + assert (root / LA).is_dir(), "the tail ran" + if case == "rename-lane-stops": + stops = (re.escape(f"register write {write_id} is in the register at seq {seq}; replayed at seq {seq - 1} " + "it now stops before its write (exit 5: ") + + r"lanes-edit: the rename was rolled back: [^\n]*?nothing was committed\.[^\n]*?\), " + + r"so its after-COMMIT steps did NOT run: \w") + assert re.search(stops, again.err) and "another change set" not in again.err, ( + f"the replay did not stop before its write: {again.err[-800:]}") + if case == "rename-lane-differs": + differs = (re.escape(f"register write {write_id} is in the register at seq {seq}; replayed at seq {seq - 1} " + "it now reaches the seam with another change set, the first path whose digest " + f"differs being {LANES_MD}, so its after-COMMIT steps did NOT run: ") + + r"\w") + assert re.search(differs, again.err) and "now stops before its write" not in again.err, ( + f"the replay did not reach the seam with another change set: {again.err[-800:]}") + assert pending(e) == [] + + +# ------------------------------------------------------------ D8: the takeover, the call lines, the claims +@CLIENT +@pytest.mark.parametrize("when", ["before-the-send", "mid-call"]) +@pytest.mark.parametrize("snapshot", ["snapshot-passes", "snapshot-fails"]) +def test_a_run_killed_after_its_call_line_or_mid_call_is_taken_over_in_doubt_and_waits_past_not_after( + estates, when, snapshot): + """A `claim` killed after its `call` line is written ([before-the-send]: + the shim holds the call before `psql`) or mid-call (the admin session + holds the head row while the call runs, then rolls back). Another lane + then claims the object. A plain rerun takes the record over, in doubt + from its start. [snapshot-passes]: its verb refuses at the snapshot with + 2, sending no commit call, and it exits only after the database's clock + has passed the record's `not_after`. [snapshot-fails] (Q-L2-6; R26): the + shim fails the takeover's snapshot; with no clock of its own it waits on + the record's (`--clock` from the newest `call` line, `--clock-age` from + `updated_utc`), makes its lookup and ends by it, never LR011's 3. The + journal never holds two rows for the id; the record goes.""" + e = estates() + gate = fifo(e, "commit-1") + argv = claim(OBJ, "--no-github") + admin = e.hold_row_locked() if when == "mid-call" else None + arms = [{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(gate)}] if admin is None else [] + w = e.launch(LA, argv, arms=arms) + if admin is None: + held = hold_point(gate, w) + else: + until_seen("the shim logging the commit call's start", lambda: w.commit_calls()[:1], w) + rec = the_record(e) + assert len(rec["calls"]) == 1, "the call line was not written before the send" + os.killpg(w.proc.pid, signal.SIGKILL) + w.wait() + write_id = rec["fields"]["write_id"].lower() + if admin is not None: + e.unlock(admin) + # THE KILLED CALL'S BACKEND RUNS ON (T245n; PM33A-TRIAGE L4): its + # client is gone, but once the head row is let go it finishes the + # request it was sent, COMMIT included, so `landed` is read only once + # no backend carries the write's id. + until_seen("the killed call's backend ended", lambda: e.c.value( + f"SELECT count(*) FROM pg_stat_activity WHERE query LIKE '%{write_id}%' " + "AND pid <> pg_backend_pid();") == "0") + else: + held.close() + not_after = rec["calls"][0].split()[2] + landed = e.has_write(write_id) + rival = e.run(LB, argv) + arms = [{"mode": "fail-match", "match": SNAPSHOT_TEXT, "call": 1}] if snapshot == "snapshot-fails" else [] + again = e.run(LA, argv, arms=arms) + assert [r["write_id"] for r in e.journal_after()].count(write_id) <= 1 + if landed: + assert (rival.code, again.code) == (2, 0), (rival.err[-300:], again.err[-300:]) + else: + # ITS LAST ATTEMPT'S CODE (TP:3458-3460 at `a91df2c4`): the verb's 2 + # where the snapshot passes; where it fails, the snapshot's own 69 + # (`8ba10b4e:lanes-edit.sh:9083-9087`), after F3's lookup. R26 rules + # the wait, on the record's clock, and no code (research.md DEC-12, + # ERR-22; T245o; PM33A-TRIAGE L5). + code = 69 if snapshot == "snapshot-fails" else 2 + assert rival.code == 0 and again.code == code, again.err[-800:] + assert snapshot == "snapshot-fails" or again.commit_calls() == [] + assert db_past(e, not_after), "the takeover exited before the record's not_after" + assert "LR011" not in again.err + assert pending(e) == [] + + +@CLIENT +@pytest.mark.parametrize("case", ["before-the-send", "lost-turn", "refusal", "in-doubt"]) +def test_the_pending_records_call_lines_are_written_before_the_send_and_dropped_only_for_a_call_that_did_not_commit( + estates, case): + """[before-the-send]: while the shim holds the first commit call, the + record already holds its line. [lost-turn]: after a 40001 from a rival's + commit, the record read while the next call is held holds one line, the + next call's, with base n+1. [refusal]: after a refusal the record is + gone. [in-doubt]: after a call in doubt it keeps the line, and the + attempt directory keeps the files `snapshot` and `files`, and + `preimages/`, a directory (B19 tasks:1104), where the attempt made one: + only a write with a lane-state sidecar does (`lane_state_preimage`, + `c8acf67c:lanes-edit.sh:23056`), and a `NOTE` line has none (T245f; + lane 4's triage, #165 6833813092). `` is the line's rest after its + four first fields (pending-record.md §3.2).""" + e = estates() + if case == "before-the-send": + gate = fifo(e, "commit-1") + w = e.launch(LA, note(LA, case), arms=[{"mode": "hold", "match": "commit_write", "call": 1, + "fifo": str(gate)}]) + held = hold_point(gate, w) + assert len(the_record(e)["calls"]) == 1 + held.close() + assert w.wait() == 0 + elif case == "lost-turn": + gates = fifo(e, "commit-1"), fifo(e, "commit-2") + w = e.launch(LA, note(LA, case), arms=[ + {"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(gates[0])}, + {"mode": "hold", "match": "commit_write", "call": 2, "fifo": str(gates[1])}]) + first = hold_point(gates[0], w) + base = int(the_record(e)["calls"][0].split()[0]) + assert e.run(LB, note(LB, "the rival")).code == 0 + first.close() + second = hold_point(gates[1], w) + calls = the_record(e)["calls"] + assert len(calls) == 1 and int(calls[0].split()[0]) == base + 1, calls + second.close() + assert w.wait() == 0 + elif case == "refusal": + assert e.run(LB, claim(OBJ, "--no-github")).code == 0 + w = e.run(LA, claim(OBJ, "--no-github")) + assert w.code == 2 and pending(e) == [] + else: + w, _, _ = exit3(e, LA, note(LA, case)) + calls = the_record(e)["calls"] + assert len(calls) == 1 + attempt = Path(calls[0].split(" ", 4)[4]) + assert all((attempt / name).is_file() for name in ("snapshot", "files")), sorted(attempt.iterdir()) + assert not (attempt / "preimages").exists() or (attempt / "preimages").is_dir(), sorted(attempt.iterdir()) + + +@CLIENT +@pytest.mark.parametrize("case", ["take-0", "take-1", "two-reruns"]) +def test_a_pending_record_is_owned_through_numbered_claim_files_and_two_reruns_never_both_take_it(estates, case): + """[take-0]: a run with no record creates `.take.0`, read while the + shim holds the first commit call, holding the run's pid, start time, + host, container and UTC. [take-1]: after a kill a rerun creates + `.take.1`. [two-reruns]: two printed reruns under `LANES_WRITE_ID` + released together against a dead owner, in 10 rounds: one create of + `.take.1` succeeds, one comment and one row each round, both exit 0. + After a run's end no record and no claim file remains.""" + e = estates() + if case == "take-0": + gate = fifo(e, "commit-1") + w = e.launch(LA, note(LA, case), arms=[{"mode": "hold", "match": "commit_write", "call": 1, + "fifo": str(gate)}]) + held = hold_point(gate, w) + takes = take_files(e) + assert [t.rsplit(".", 1)[1] for t in takes] == ["0"], takes + text = (e.state / PENDING / takes[0]).read_text(encoding="utf-8") + assert str(w.proc.pid) in text or "pid" in text, text + held.close() + assert w.wait() == 0 + elif case == "take-1": + argv = note(LA, case) + killed(e, LA, argv) + assert [t.rsplit(".", 1)[1] for t in take_files(e)] == ["0"] + assert e.run(LA, argv).code == 0 + else: + where = loggers(e) + for k in range(10): + obj = f"opensoft/r9#{100 + k}" + argv = claim(obj) + _, write_id = killed(e, LA, argv, first=[where]) + generation = e.c.switch()["generation"] + (e.root / f"round-{k}").mkdir() + barrier = Barrier(e.root / f"round-{k}", 2) + before = len(comments(where)) + pair = [e.launch(LA, argv, barrier=barrier, first=[where], + extra={"LANES_WRITE_ID": write_id, "LANES_WRITE_GENERATION": generation}) + for _ in range(2)] + barrier.release() + assert [p.wait() for p in pair] == [0, 0], [p.err[-300:] for p in pair] + assert len(comments(where)) - before == 1, f"round {k}: {len(comments(where)) - before} comments" + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + assert pending(e) == [] and take_files(e) == [] + + +# ------------------------------------------------------------ AT-F34: the tail, once +#: [lr006]'s seam cap with the kept child gone (`learnt`): 10 s leaves the next +#: attempt's child 9 s to reach its seam, and holds its commit call about +#: 4 to 6 s where the gap between the two children's seams is runs r5's and +#: r7b's 7 s and 5.4 s (T245i, T245j). +LR006_SEAM_CAP_S = 10 + + +def learnt(e: Estate, way: str, where: Path, *, gone: bool = False, script: Optional[Path] = None) -> Writer: + """A `claim` whose first commit call committed while in doubt, learnt by + [snapshot] (`after`, then the next snapshot), [lr006] (the first call + forked and held as AT-F3's orphan, the shim exiting 2 at once; the + orphan let go once the next attempt's snapshot has ended, having read + the write absent, and committed before that attempt's commit call, which + so gets LR006, as AT-LR006-1 orders them) or [lookup] (`after`, the next + snapshot failed, so AT-F3's lookup finds it). The call that learns it is + held; with `gone`, until the kept child's `seam.out` says `none` under + `LANES_REGISTER_SEAM_CAP_S=3`, or `LR006_SEAM_CAP_S` for [lr006]. + + [lr006]'s timing (T245f, T245i, T245j). The orphan carries the first + snapshot's clock plus 10 s as its `not_after` (`lanes-index`'s + `not_after=`), so it must commit within about 10 s of that snapshot: + let go only once the next commit call was held, it ended LR008 in line + ID17's run r7b, at 11 s on py-bench. With `gone`, the next attempt's + child must reach its seam before the parent's wait ends, 1 s before the + kept child's cap (Y:5004; `c8acf67c:lanes-edit.sh:9192`), and its commit + call, sent at that seam and held until the kept child's `none`, should + stay under its own 8 s timer, `C`: the cap must exceed the gap between + the two children's seams by more than 1 s, and by less than about `C`. + Runs r5 and r7b measured that gap at about 7 s and 5.4 s, so 3 s and 7 s + were short and 12 s near `C`. Where the hold does pass `C`, that call is + in doubt too, and the next snapshot learns the write instead.""" + gate = fifo(e, f"learn-{way}") + if way == "lr006": + orphan = fifo(e, "orphan") + arms = [{"mode": "fork", "match": "commit_write", "call": 1, "fifo": str(orphan), "exit": True}, + {"mode": "hold", "match": "commit_write", "call": 2, "fifo": str(gate)}] + elif way == "snapshot": + arms = [AFTER, {"mode": "hold", "match": SNAPSHOT_TEXT, "call": 2, "fifo": str(gate)}] + else: + arms = [AFTER, {"mode": "fail-match", "match": SNAPSHOT_TEXT, "call": 2}, + {"mode": "hold", "match": "find_write", "call": 1, "fifo": str(gate)}] + extra = {"LANES_REGISTER_SEAM_CAP_S": str(LR006_SEAM_CAP_S if way == "lr006" else 3)} if gone else {} + w = e.launch(LA, claim(OBJ), first=[where], arms=arms, extra=extra, script=script) + if way == "lr006": + until_seen("the next attempt's snapshot ended", lambda: len(w.snapshot_calls()) >= 2 + and w.snapshot_calls()[1]["call"] in w.ends(), w) + hold_point(orphan, w).close() + until_seen("the orphan's commit", lambda: e.journal_after(), w) + held = hold_point(gate, w) + if gone: + until_seen("the kept child's seam.out saying none", lambda: any( + p.read_text(encoding="utf-8").strip() == "none" for p in e.state.rglob("seam.out")), w) + held.close() + return w + + +@CLIENT +@pytest.mark.parametrize("way", ["snapshot", "lr006", "lookup"]) +def test_a_call_in_doubt_that_committed_runs_its_tail_once_from_the_kept_child(estates, way): + """The call in doubt committed, learnt by [snapshot], [lr006] or + [lookup] (`learnt`). The fake `gh` comments once, after the COMMIT, and + the `seam.out` files it saw then hold the kept child's `tail`, once, + and no other child's anything but `none`; the mark keeps `done`; exit + 0, one row. The seams are read as the comment saw them, during the run + (the logging stand-in reads every `seam.out` under the state directory + at each call): at its end the run removes its own `//`, its + attempt directories and their `seam.out` with it (B19 tasks:1179, T415; + T245e; lane 4's triage, #165 6833813092).""" + e = estates() + where = loggers(e) + w = learnt(e, way, where) + assert w.wait() == 0, w.err[-800:] + write_id = one_entry(e, w) + said = comments(where) + assert len(said) == 1, said + seams = sorted(text.strip() for text in said[0]["seams"].values()) + assert seams.count("tail") == 1 and set(seams) <= {"tail", "none"}, seams + assert "done" in mark_of(e, write_id).read_text(encoding="utf-8") + + +@CLIENT +@pytest.mark.parametrize("way", ["snapshot", "lr006", "lookup"]) +def test_with_the_kept_child_gone_a_replay_runs_the_tail_once(estates, way): + """The same three, with the cap ending the kept child first. The run + replays at `s-1` (lanes-index's `register snapshot --tree --at + `, which the shim does not see, so its count is lane 4's to log) + and sends no commit call after it; the fake `gh` is called once.""" + e = estates() + where = loggers(e) + w = learnt(e, way, where, gone=True) + assert w.wait() == 0, w.err[-800:] + one_entry(e, w) + assert len(comments(where)) == 1, comments(where) + + +@CLIENT +def test_two_matching_calls_of_one_change_set_leave_the_replays_tail_owed(estates): + """NIT-4 (b) (lane 3, #165 6851422597; T418 `91bfa229`): a takeover whose + record holds two `call` lines whose `` is the entry's, and whose + own commit never answered `committed`, does not replay. Y:5317 takes + "the `call` line whose `` is the entry's", one line. Two are + resends of one change set whose snapshots read the same database + second; their `preimages/` need not agree, and which of their calls + committed is not known. So the tail is owed: one WARNING naming the two + lines, exit 0, the mark `owed `, the record removed, no comment, + and no replay child, so `claim`'s Rule 1 reads (`gh issue view`) stay + at the killed run's count. T418 ends it before `pw_wdir_ok` and the + `replayed` token (`91bfa229:lanes-edit.sh:8470-8471`). + + No fixture makes two such calls today: the `[lr006]` fixture moves the + clock past a second (TP:6981-6982). So the case writes the second line + itself. The killed `claim` leaves one `call=` line and no `tail` + (`killed`). The case adds a line with the same ` + `, naming its own attempt directory, `/2`, as a + resend in the same second would, since matching lines are never + byte-identical (lane 3, the same post). That directory holds a copy of + the first's `snapshot`, `files` and `preimages` (Y:5177), mode 0700, so + it passes `pw_wdir_ok`, and the record's `attempts` goes up by one. The + rerun's snapshot finds the write, and with the killed child gone + `pw_tail` goes to the replay (`:8367`). + + It fails until T418 `91bfa229`. Before NIT-4 the replay took the last + matching line (`9a314c09:lanes-edit.sh:8546`), so it replayed from the + second directory and ran the tail: one comment and `tail=done`. Its + name holds `tail`, so T418's verify selects it and T417's, which + leaves out `tail`, does not (tasks:1231 and :1199 at `94fab177`). + + The exception, where the run whose own commit answered `committed` + keeps that call's line (Y:5306), is not built here. It needs that run's + own in-memory `call` lines to hold a second matching line, from an + earlier call of its own in the same database second, with its kept + child gone. A line written into the record file never reaches a live + run, and no fixture pins two calls to one second.""" + e = estates() + where = loggers(e) + argv = claim(OBJ) + _, write_id = killed(e, LA, argv, first=[where]) + seq = e.journal_after()[-1]["seq"] + rec = the_record(e) + assert len(rec["calls"]) == 1 and "tail" not in rec["fields"], rec + base, clock, not_after, files, first = rec["calls"][0].split(None, 4) + first_dir = Path(first.strip()) + assert first_dir.name.isdigit() and (first_dir / "files").is_file(), f"{first_dir} is not a kept call's directory" + second = first_dir.parent / str(int(first_dir.name) + 1) + second.mkdir(mode=0o700) + second.chmod(0o700) + kept = [first_dir / "snapshot", first_dir / "files", first_dir / "preimages"] + if kept[2].is_dir() and not kept[2].is_symlink(): + kept += sorted(kept[2].rglob("*")) + for src in kept: + if src.is_symlink() or not src.exists(): + continue + dst = second / src.relative_to(first_dir) + if src.is_dir(): + dst.mkdir(mode=0o700, parents=True, exist_ok=True) + elif src.is_file(): + dst.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + dst.write_bytes(src.read_bytes()) + dst.chmod(src.stat().st_mode & 0o777) + text = re.sub(r"(?m)^attempts=(\d+)$", lambda m: f"attempts={int(m.group(1)) + 1}", + rec["path"].read_text(encoding="utf-8")) + rec["path"].write_text(f"{text}call={base} {clock} {not_after} {files} {second}\n", encoding="utf-8") + assert len(the_record(e)["calls"]) == 2 + killed_reads = len(gh_reads(where)) + again = e.run(LA, argv, first=[where]) + # The one WARNING must be the owed tail's own, so this assert alone tells + # the build before NIT-4, whose replay printed the comment's WARNING + # instead, from T418's (lane 2's RV23 NIT 12). + warned = [line for line in again.err.splitlines() if "WARNING" in line] + assert (again.code == 0 and again.err.count("WARNING") == 1 + and "2 call lines of its pending record" in warned[0]), ( + f"not one WARNING, the owed tail's: {again.err[-800:]}") + owed = (re.escape(f"register write {write_id} is in the register at seq {seq}; 2 call lines of its pending " + "record sent the entry's change set") + + r"[^\n]*?" + re.escape("so it cannot be replayed, so its after-COMMIT steps did NOT run: ") + r"\w") + assert re.search(owed, again.err) and "replayed at seq" not in again.err, ( + f"the replay was not left owed for two matching call lines: {again.err[-800:]}") + assert comments(where) == [] and len(gh_reads(where)) == killed_reads, ( + f"a replay ran: {len(comments(where))} comment(s), {len(gh_reads(where)) - killed_reads} new Rule 1 read(s)") + mark = mark_of(e, write_id).read_text(encoding="utf-8") + assert re.search(r"(?m)^tail=owed ", mark), f"the mark does not hold owed: {mark!r}" + assert pending(e) == [] + + +@CLIENT +def test_a_replay_of_request_handoff_types_into_no_pane(estates): + """With the kept child gone, `request-handoff`'s replay runs its tail + with no `guard_type`: the fake `tmux` logs no `send-keys`, and the poll + runs.""" + e = estates() + where = loggers(e) + argv = ["request-handoff", LC, "--session", session_of(LC), "--wait", "2"] + killed(e, LA, argv, first=[where]) + before = len(tool_calls(where, "tmux")) + again = e.run(LA, argv, first=[where], extra={"LANES_POLL_SECONDS": "1"}) + assert again.code in (0, 2), again.err[-800:] + later = tool_calls(where, "tmux")[before:] + assert not [c for c in later if "send-keys" in c["argv"]], later + assert later, "the poll never ran" + + +@CLIENT +def test_a_rerun_that_finds_tail_sent_runs_no_tail_and_writes_the_done_mark(estates): + """The record holds `tail=sent` from a run killed after `sent` and + before the child's exit. A plain rerun takes it over, finds its id at + the snapshot and `tail` sent, runs no tail, writes the mark with `sent`, + removes the record, exits 0, and says the steps may have run at the + record's UTC in its pid.""" + e = estates() + where = loggers(e) + argv = claim(OBJ) + _, write_id = killed(e, LA, argv, first=[where]) + rec = the_record(e) + utc = e.utc() + with rec["path"].open("a", encoding="utf-8") as f: + f.write(f"tail=sent {e.journal_after()[-1]['seq']} {utc}\n") + again = e.run(LA, argv, first=[where]) + assert again.code == 0 and comments(where) == [], again.err[-800:] + assert "sent" in mark_of(e, write_id).read_text(encoding="utf-8") + assert pending(e) == [] and utc in again.err, again.err[-800:] + + +def replay_copy(e: Estate, control: Path) -> Path: + """AT-F34's test copy (TP:6962-6965): a `lanes-edit.sh` whose runs under + the seam's scratch flag read `control` once the test writes it: `stops` + exits 1 before `ready`, and `differs` adds one more word to the line, so + a replay stops or reaches the seam with another change set.""" + text = e.lanes_edit.read_text(encoding="utf-8") + shebang, rest = text.split("\n", 1) + quoted = "'" + str(control).replace("'", "'\\''") + "'" + hook = (f'if [ -n "${{LANES_REGISTER_SCRATCH:-}}" ] && [ -f {quoted} ]; then\n' + f' case "$(cat {quoted})" in\n' + ' stops) echo "T245: the replay stops" >&2; exit 1 ;;\n' + ' differs) set -- "$@" "T245 differs" ;;\n' + ' esac\nfi\n') + copy = e.bindir / "lanes-edit-t245.sh" + copy.write_text(shebang + "\n" + hook + rest, encoding="utf-8") + copy.chmod(0o755) + return copy + + +@CLIENT +@pytest.mark.parametrize("case", ["stops", "differs"]) +def test_a_replay_that_stops_or_differs_runs_no_tail_and_warns(estates, case): + """With the kept child gone and the test copy's control file written, + the replay exits 1 before `ready` ([stops]) or reaches the seam with one + more word ([differs]). The run exits 0 with one WARNING naming the seq; + the fake `gh` is not called, and the record is removed.""" + e = estates() + where = loggers(e) + control = e.root / "t245-replay" + script = replay_copy(e, control) + argv = claim(OBJ) + killed(e, LA, argv, first=[where], script=script) + seq = e.journal_after()[-1]["seq"] + control.write_text(case + "\n", encoding="utf-8") + again = e.run(LA, argv, first=[where], script=script) + assert again.code == 0 and again.err.count("WARNING") == 1 and str(seq) in again.err, again.err[-800:] + assert comments(where) == [] and pending(e) == [] + + +@CLIENT +@pytest.mark.parametrize("case", ["done-mark", "this-host", "other-host"]) +def test_a_run_with_no_record_runs_no_tail_and_names_it(estates, case): + """A rerun under `LANES_WRITE_ID` of a write committed and settled, with + no record here, and so with its `LANES_WRITE_GENERATION` (an id with no + generation and no record is refused 2 before any call: pending-record.md + §5.2, R08; T245d), exits 0 and runs no tail. [done-mark]: it says what the + mark's `tail` says, that the steps ran at `` in pid `

` (TP:7048-7049 + at `a91df2c4`), the mark's `tail=done ` and `pid=` (T245i; + line ID17's run r3 printed "its after-COMMIT steps ran at in pid +

"); [this-host]: with the mark removed and + `journal.workstation` this host (`found`'s `workstation=`), it says the + steps may already have run here, naming the entry's UTC from `utc=`; + [other-host]: the write made from ws2, it names ws2 and says to rerun + there if that run ended 3.""" + e = estates() + where = loggers(e) + argv = claim(OBJ) + extra = {"LANES_WORKSTATION": "ws2"} if case == "other-host" else {} + w = e.run(LA, argv, first=[where], extra=extra) + assert w.code == 0 + write_id = w.write_ids()[-1] + said = mark_of(e, write_id).read_text(encoding="utf-8") + if case != "done-mark": + mark_of(e, write_id).unlink() + before = len(comments(where)) + again = e.run(LA, argv, first=[where], extra={"LANES_WRITE_ID": write_id, + "LANES_WRITE_GENERATION": e.c.switch()["generation"]}) + assert again.code == 0 and len(comments(where)) == before, again.err[-800:] + if case == "other-host": + assert "ws2" in again.err and "rerun" in again.err, again.err[-800:] + elif case == "this-host": + assert "may already have run" in again.err, again.err[-800:] + else: + mark = dict(line.split("=", 1) for line in said.splitlines() if "=" in line) + tail = mark.get("tail", "").split() + assert tail[:1] == ["done"] and len(tail) == 4, f"the mark's tail is not done : {said}" + assert f"ran at {tail[3]}" in again.err and f"pid {mark.get('pid')}" in again.err, ( + f"the text does not say the steps ran at {tail[3]} in pid {mark.get('pid')}: {again.err[-800:]}") + + +# ------------------------------------------------------------ D8: a `tail` record a takeover cannot use +#: The maximum age these cases set (`LANES_REGISTER_PENDING_MAX_AGE_S`, Y:5207), +#: so the time the refusal prints, the record's `updated_utc` plus the age, is +#: the test's to compute, in the record's own UTC form. +TAIL_AGE_S = 600 +TAIL_AGE = {"LANES_REGISTER_PENDING_MAX_AGE_S": str(TAIL_AGE_S)} +UTC_FORM = "%Y-%m-%dT%H:%M:%SZ" + + +@CLIENT +@pytest.mark.parametrize("case", ["no-found", "tail-of-another-form"]) +def test_a_tail_record_a_takeover_cannot_use_is_refused_2_and_kept(estates, case): + """A `claim` ended 3 by `no-reconnect`, and its record given a `tail` + by hand (R-F13c-23; lane 3's batch 8, Q46 and Q34). [no-found]: its + call never landed (`before`), and the record holds `tail=sent `, + as a damaged record does, so the takeover's first snapshot answers with + no `found=`. A plain rerun refuses 2, sending no commit call and running + no tail, and leaves the record as it was. Its text gives Y:5211-5213's + effect check and, where the effect is not there, the same command again + with no `LANES_WRITE_ID` once the record is + `LANES_REGISTER_PENDING_MAX_AGE_S` past its `updated_utc`, printing that + time, and never `LANES_WRITE_ID=new`, which replaces only a record of + another generation (Y:5215). With `updated_utc` set past the age, that + rerun writes anew by Y:5207's aged plan: it warns, prints the lookup of + the record's id, and commits once under a new id, its tail run once. + [tail-of-another-form] (Q34): its call landed (`after`), so a snapshot + would find it, and the record's `tail` is `owed `, a done mark's + value and neither Y:5179 form, `sent ` or `done + `: a record that cannot be read, T412's 2, with no commit call and + no tail, the record kept.""" + e = estates() + where = loggers(e) + argv = claim(OBJ) + arm = dict(NO_RECONNECT, **{"as": "before" if case == "no-found" else "after"}) + w = e.run(LA, argv, arms=[arm], first=[where]) + assert w.code == 3 and comments(where) == [], f"exit {w.code}, not 3: {w.err[-800:]}" + rec = the_record(e) + write_id = rec["fields"]["write_id"].lower() + assert e.has_write(write_id) == (case != "no-found"), f"[{case}]: the first call's outcome is not the case's" + tail = f"sent {e.c.head() + 1} {e.utc()}" if case == "no-found" else f"owed {e.utc()}" + with rec["path"].open("a", encoding="utf-8") as f: + f.write(f"tail={tail}\n") + kept, journal = rec["path"].read_bytes(), e.journal_after() + again = e.run(LA, argv, first=[where], extra=TAIL_AGE) + assert again.code == 2, f"[{case}] the takeover exited {again.code}, not 2: {again.err[-800:]}" + assert again.commit_calls() == [] and comments(where) == [], again.err[-800:] + assert rec["path"].read_bytes() == kept, f"[{case}] the refused run changed the record" + assert e.journal_after() == journal + if case == "tail-of-another-form": + assert "cannot be read" in again.err, f"the refusal is not T412's 2: {again.err[-800:]}" + return + assert f"lanes-edit.sh history {LA} --since" in again.err or f"lanes-edit.sh register-row {LA}" in again.err + aged_at = (datetime.datetime.strptime(rec["fields"]["updated_utc"], UTC_FORM) + + datetime.timedelta(seconds=TAIL_AGE_S)).strftime(UTC_FORM) + assert aged_at in again.err, f"the text does not print {aged_at}, updated_utc plus the age: {again.err[-800:]}" + assert "LANES_WRITE_ID=new" not in again.err and not WRITE_ID_SAID.search(again.err), ( + f"the text offers a LANES_WRITE_ID rerun: {again.err[-800:]}") + set_field(rec["path"], "updated_utc", e.utc((TAIL_AGE_S + 60) / 60)) + anew = e.run(LA, argv, first=[where], extra=TAIL_AGE) + assert anew.code == 0, f"the rerun past the age exited {anew.code}: {anew.err[-800:]}" + assert anew.write_ids() and write_id not in anew.write_ids(), anew.err[-800:] + assert "WARNING" in anew.err and f"--write-id {write_id}" in anew.err, anew.err[-800:] + ids = [r["write_id"] for r in e.journal_after()] + assert ids.count(write_id) == 0 and ids.count(anew.write_ids()[-1]) == 1, ids + assert len(comments(where)) == 1 and pending(e) == [], (comments(where), pending(e)) + + +# ------------------------------------------------------------ GB §4.3: the texts for 3 (runs here) +#: Each site's file, and the clause it must hold: the writer's exit table's +#: row 3 primary clause (lane 4's T417, R21); `lane-rename`'s header and +#: default arm (T433); `lane-worktrees`' note (T434). +TEXTS_FOR_3 = {"exit-table": "lanes-edit.sh", "lane-rename": "lane-rename", "lane-worktrees": "lane-worktrees"} + + +@pytest.mark.parametrize("site", list(TEXTS_FOR_3)) +def test_the_texts_for_3_say_may_not_be_written_and_relay_the_writers_rerun(site): + """On exit 3 on primary each site prints D8's text for 3, byte for byte + with its backticks, relays the writer's printed rerun verbatim, and never + prints its own hint, "was NOT added" or "NOT WRITTEN". Read from the + scripts, with no engine: each must hold D8's text and none of the + refused hints, where its exit-3 arm stands. A site may split the text + over adjacent string literals on consecutive source lines (T434's + `lane-worktrees` does, in one parenthesised Python string), so the + search also reads the file with each such seam joined. The refused hints + are looked for in what a site can print, so its full-line comments are + left out there: one may name a refused hint to forbid it, as T434's + `lane-worktrees:192` does at 3a5b303f.""" + from sql_register_helpers import REPO + path = REPO / TEXTS_FOR_3[site] + assert path.is_file(), f"{path.name} is not in the code root" + raw = path.read_text(encoding="utf-8") + views = [raw, joined_literals(raw)] + forms = [D8_TEXT_3, D8_TEXT_3.replace("`", "\\`")] + assert any(form in text for text in views for form in forms), ( + f"{path.name} does not hold D8's text for 3: {D8_TEXT_3!r}") + for refused in ("was NOT added", "NOT WRITTEN"): + for text in map(uncommented, views): + near = [m.start() for form in forms for m in re.finditer(re.escape(form), text)] + assert not any(refused in text[max(0, k - 400):k + 400] for k in near), ( + f"{path.name} prints {refused!r} beside D8's text for 3") + + +def joined_literals(text: str) -> str: + """`text` with each seam of two adjacent string literals on consecutive + source lines joined: a closing quote, the line's end and the next line's + indent, and an opening quote of the same kind, are dropped, as Python + joins `("a " NEWLINE "b")`.""" + return re.sub(r"""(["'])[ \t]*\n[ \t]*\1""", "", text) + + +def uncommented(text: str) -> str: + """`text` without its full-line `#` comments, which a site never prints.""" + return re.sub(r"(?m)^[ \t]*#.*\n?", "", text) + + +# ============================================================ PHASE W (and N): AT-F3 (T246) +# +# A WRITE IN DOUBT AT THE DEADLINE (AT-F3, TP:5564-5780). AT-F3's estate is +# test 12's half-open case at `LANES_REGISTER_DEADLINE_S=1` (`HalfOpen`): the +# first commit call lost by `before`; the test's session, as `lanes_w_fx` +# with `idle_in_transaction_session_timeout = 0`, replaying that call's script +# without its COMMIT, ended in a `;` that `psql` sends (`without_commit`, +# T244c), so it holds the head row with the row inserted; the +# next snapshot held until the replay returned; and the lookup's first try +# held on a FIFO while the case decides. `` is the first call's +# `not_after`. Its timer is D9's 9 s (open point 37). +# +# AT MOST ONE RETRY (TP:5634-5636): at a 1 s deadline the lost first call +# may end past it, and then no attempt starts and the writer goes straight +# to the lookup, with no next snapshot (T246c; on `pair-m23t` `c8acf67c` the +# shim logged the lookup's two tries after the lost call, and no snapshot). +# So `HalfOpen` waits for the writer's next call after its first commit +# call, that snapshot or the lookup's first try, and releases the snapshot's +# hold only where the snapshot came. +# +# THE TRIES (R26, lane 4's answer to 40): each try starts at least 0.75 s +# after the last one's start, never before the last one has ended, the k-th +# at least k - 1.25 s after the first's, and any up to the window's 10 s +# mark, on the monotonic clock. The 0.75 s and 1.25 s are lane 2's (NOTES +# question 34); the exact spacing is lane 1's virtual-clock check (T114). +# The try endings the shim cannot make (57014, an exit 1, a signal, an exit 3 +# with no SQLSTATE, an exit 0 whose answer cannot be read; ERR-50, R-F11-5) +# come from `_ANSWER`, a stand-in put first on `PATH` that answers one +# lookup try as taught and hands every other call on. +# +# TOO EARLY (R26, 36): `[too-early]` takes over a record written on another +# host, so the run has no clock to age and calls at once: LR011's 3, the +# record kept. On this host the takeover waits (T245's `[snapshot-fails]`). +# The killed run's claim names that host, so it counts as live for the +# seam's cap, 120 s, from the later of its `utc` and the record's +# `updated_utc` (Y:5220; pending-record.md §4.3), and the rerun sets the +# cap's test variable, `LANES_REGISTER_SEAM_CAP_S`, to 1 s to take it over +# at once (T246d; lane 4's triage, #165 6833813092). +# +# They FAIL UNTIL L1-A6, L1-A12 and L4-B5 land. Without PostgreSQL 16 each +# skips; the direct calls (LR011 at once, LR008's engine half) are phase N on +# T222's clones. + +DEADLINE = {"LANES_REGISTER_DEADLINE_S": "1"} +PAST = "2000-01-01 00:00:00+00" +ENTRY_IN_SCRIPT = re.compile(r"'((?:[^']|'')*)'::jsonb") +_HALVES = itertools.count(1) + +_ANSWER = r'''#!/usr/bin/env python3 +# T246's answer stand-in: the k-th call carrying MATCH answered as taught, +# sending nothing; every other call handed to the next psql on PATH. +import json, os, signal, subprocess, sys +HERE = os.path.dirname(os.path.abspath(__file__)) +rule = json.load(open(os.path.join(HERE, "answer.json"), encoding="utf-8")) +args = sys.argv[1:] +nxt = next(os.path.join(d, "psql") for d in os.environ["PATH"].split(os.pathsep) + if d and os.path.abspath(d) != HERE and os.access(os.path.join(d, "psql"), os.X_OK)) +if args == ["--version"]: + os.execv(nxt, [nxt] + args) +data = sys.stdin.buffer.read() +if rule["match"].encode() in data: + count = os.path.join(HERE, "answer.count") + seen = int(open(count).read()) + 1 if os.path.exists(count) else 1 + open(count, "w").write(str(seen)) + if seen == rule.get("call", 1): + with open(os.path.join(HERE, "answer.log"), "a", encoding="utf-8") as log: + log.write(json.dumps({"t": __import__("time").time(), "call": seen}) + "\n") + sys.stdout.write(rule.get("stdout", "")) + sys.stderr.write(rule.get("stderr", "")) + sys.stdout.flush() + sys.stderr.flush() + if rule.get("signal"): + os.kill(os.getpid(), getattr(signal, "SIG" + rule["signal"])) + sys.exit(rule.get("exit", 0)) +done = subprocess.run([nxt] + args, input=data, capture_output=True) +sys.stdout.buffer.write(done.stdout) +sys.stderr.buffer.write(done.stderr) +sys.exit(done.returncode if done.returncode >= 0 else 128 - done.returncode) +''' + +#: The try endings with no answer (ERR-50; lane 3, #165 6755922262; R-F11-5). +NO_ANSWER = { + "57014": {"exit": 3, "stderr": "ERROR: 57014: canceling statement due to statement timeout\n"}, + "exit-1": {"exit": 1, "stderr": "psql: error: out of memory\n"}, + "signal": {"signal": "KILL"}, + "exit-3-no-sqlstate": {"exit": 3, "stderr": "ERROR: the lookup failed\n"}, + "unreadable-exit-0": {"exit": 0, "stdout": "not an answer\n"}, +} + + +def answering(e: Estate, rule: dict) -> Path: + where = e.root / f"answer-{next(_HALVES)}" + where.mkdir() + (where / "psql").write_text(_ANSWER, encoding="utf-8") + (where / "psql").chmod(0o755) + (where / "answer.json").write_text(json.dumps(dict(rule, match="find_write")), encoding="utf-8") + return where + + +def another_writes_script(script: str) -> str: + """The call's script with the entry's `args_sha256` changed: another + write's entry under the same id (AT-F3's [another-write]).""" + m = ENTRY_IN_SCRIPT.search(script, script.find("commit_write(")) + entry = json.loads(m.group(1).replace("''", "'")) + entry["journal"]["args_sha256"] = sha256(b"another write, T246") + text = json.dumps(entry, ensure_ascii=False, separators=(",", ":")).replace("'", "''") + return script[:m.start()] + "'" + text + "'::jsonb" + script[m.end():] + + +class HalfOpen: + """AT-F3's estate (see the section's head): `w`, the writer; `session`, + the old connection; `write_id`; `n`, the first call's `not_after`; and + `held`, the lookup's first try on its FIFO, where `hold_lookup`.""" + + def __init__(self, e: Estate, argv: Optional[Sequence[str]] = None, *, arms: Sequence[dict] = (), + hold_lookup: bool = True, alter=None, first: Sequence[Path] = (), extra: Optional[dict] = None): + k = next(_HALVES) + self.e, self.first = e, list(first) + self.argv = list(argv or note(LA, f"F3 {k}")) + snap, look = fifo(e, f"f3-snapshot-{k}"), fifo(e, f"f3-lookup-{k}") + held = [{"mode": "hold", "match": "find_write", "call": 1, "fifo": str(look)}] if hold_lookup else [] + self.w = e.launch(LA, self.argv, first=first, extra=dict(DEADLINE, **(extra or {})), + arms=[BEFORE, {"mode": "hold", "match": SNAPSHOT_TEXT, "call": 2, "fifo": str(snap)}, + *held, *arms]) + call = until_seen("the first commit call", lambda: self.w.commit_calls()[:1], self.w)[0] + # The replay waits for that call's end. The shim logs a call at its + # start, before the real `psql` runs, so a replay sent then could take + # the head row first: the writer's own `commit_write` then waits out + # the commit row's 2000 ms `lock_timeout` and ends 55P03, a lost turn, + # and with the 1 s deadline past the run ends 75, "register busy", + # before any next call (round 6h's `[connect]`; lane 2's TR21, 2c). + # The `before` arm's lost call ends 2, its backend gone and its + # transaction rolled back, so the head row is free for the replay. + # THAT RACE IS READ FROM THE CODE, NOT SEEN: no run has caught it, and + # the runs since the wait came in (ID-25's four) show only that it + # keeps the flow, not that it closed the race. The first commit call + # is always the `before` arm's, since this estate puts BEFORE first + # among every writer's arms, so the 2 asserted here holds for each + # caller (lane 2's RV23 NIT 11). + ended = until_seen("the first commit call's end", lambda: self.w.ends().get(call["call"]), self.w) + assert ended.get("exit") == 2, f"the first commit call ended {ended}, not 2, the `before` arm's lost call" + script = without_commit(call["stdin"]) + assert "commit_write" in script and script.rstrip().endswith(";") and not script.rstrip().endswith("\\;"), ( + f"the replay does not end in a `;` that psql sends: {script[-80:]!r}") + self.session = e.c.db.session(WRITER_LOGIN, "t246-held") + self.session.stdin.write("SET idle_in_transaction_session_timeout = 0;\n" + + (alter(script) if alter else script).rstrip() + "\n") + self.session.stdin.flush() + until_seen("the replay holding the head row", lambda: e.c.value( + "SELECT count(*) FROM pg_stat_activity WHERE application_name = 't246-held' " + "AND state = 'idle in transaction';") == "1", self.w) + self.write_id, self.n = self.w.write_ids()[0], first_not_after(self.w) + after = until_seen("the writer's next call after its first commit call: its next snapshot, held, " + "or its lookup's first try", self.next_call, self.w) + if SNAPSHOT_TEXT in (after.get("stdin") or ""): + hold_point(snap, self.w).close() + self.held = hold_point(look, self.w) if hold_lookup else None + + def next_call(self) -> Optional[dict]: + """The writer's first snapshot or lookup try after its first commit + call, as the shim logs each at its start, before any hold: a next + attempt's snapshot, or, where the deadline passed with no attempt + started, the lookup's first try (at most one retry, TP:5634-5636; + T246c).""" + calls = self.w.calls() + commits = [k for k, c in enumerate(calls) if "commit_write" in (c.get("stdin") or "")] + later = calls[commits[0] + 1:] if commits else [] + return next((c for c in later if SNAPSHOT_TEXT in (c.get("stdin") or "") + or "find_write" in (c.get("stdin") or "")), None) + + def end(self, how: str) -> None: + self.session.stdin.write(f"{how};\n") + self.session.stdin.flush() + + def release(self) -> None: + if self.held is not None: + self.held.close() + self.held = None + + def finish(self) -> int: + self.release() + code = self.w.wait() + if self.session.poll() is None: + self.end("ROLLBACK") + self.session.stdin.close() + self.session.stdin = None + self.session.communicate(timeout=30) + return code + + def tries(self) -> List[dict]: + """Each lookup try's start: the shim's stamps, and the tries the + answer stand-in answered before the shim saw them.""" + answered = [] + for where in self.first: + log = where / "answer.log" + if log.exists(): + answered += [json.loads(l) for l in log.read_text(encoding="utf-8").splitlines() if l] + return sorted(lookups(self.w) + answered, key=lambda t: t["t"]) + + def n_epoch(self) -> float: + return float(self.e.c.value(f"SELECT extract(epoch FROM '{self.n}'::timestamptz);")) + + def tries_obey_the_window(self, least: int = 2) -> List[dict]: + """The tries' timing rules, on the stamps (section 4's rules 2 and 3, + with lane 2's tolerance), the spread recorded against 10 s.""" + tries = self.tries() + assert len(tries) >= least, f"{len(tries)} lookup tries, not at least {least}" + for k, (last, this) in enumerate(zip(tries, tries[1:]), 2): + assert this["t"] - last["t"] >= GAP_S, f"try {k} began {this['t'] - last['t']:.2f} s after the last" + assert this["t"] - tries[0]["t"] >= k - LATE_S, f"try {k} began early on the first's start" + assert tries[-1]["t"] - tries[0]["t"] <= 2 * WINDOW_S, "a try began past twice the window" + print(f"[T246 tries] n={len(tries)} last={tries[-1]['t'] - tries[0]['t']:.2f}s against {WINDOW_S}s") + return tries + + +def busy(w: Writer) -> bool: + return w.code == 75 and all(word in w.err for word in ("register busy", "nothing", "retry")) + + +@CLIENT +@pytest.mark.parametrize("case", ["found", "absent", "held", "no-answer"]) +def test_a_write_in_doubt_at_the_deadline_makes_one_lookup(estates, case): + """While the first try is held: [found] the session commits, exit 0, one + row, the record removed; [absent] it rolls back, exit 75 with the busy + text, no row, the record removed; [held] it stays open, every try meets + 55P03, at least two tries by the window's rules, exit 3 naming the id, + never 75, the record kept, and after the rollback a plain rerun writes + it once; [no-answer] every try fails as a failed connect, exit 3, the + record kept. The first try starts no earlier than `` less 3 s.""" + e = estates() + arms = [{"mode": "fail-match", "match": "find_write"}] if case == "no-answer" else [] + h = HalfOpen(e, arms=arms, hold_lookup=case != "no-answer") + if case in ("found", "absent"): + h.end("COMMIT" if case == "found" else "ROLLBACK") + h.release() + code = h.w.wait() + assert h.tries()[0]["t"] >= h.n_epoch() - 3, "the first try began before less 2 s" + rows = [r["write_id"] for r in e.journal_after()].count(h.write_id) + if case == "found": + assert (code, rows, pending(e)) == (0, 1, []), h.w.err[-800:] + elif case == "absent": + assert busy(h.w) and rows == 0 and pending(e) == [], h.w.err[-800:] + else: + assert code == 3 and h.write_id in h.w.err and len(pending(e)) == 1, h.w.err[-800:] + h.tries_obey_the_window() + h.finish() + if case == "held": + again = e.run(LA, h.argv) + assert again.code == 0, again.err[-800:] + assert [r["write_id"] for r in e.journal_after()].count(h.write_id) == 1 + + +@CLIENT +def test_the_deadlines_lookup_waits_past_the_largest_not_after(estates): + """[held]'s first try is seen in `PgSleep` until the database's clock + passes ``, then waiting on the head row's lock (lane 1's v2.6, + behaviour 1); its script calls `find_write` with the write id, ``, + and the generation, lane, verb and `args_sha256` the commit calls sent. + The session then rolls back, and the writer exits 75.""" + e = estates() + h = HalfOpen(e, hold_lookup=False) + slept = until_seen("the lookup in PgSleep", lambda: e.c.value( + "SELECT count(*) FROM pg_stat_activity WHERE query LIKE '%find_write%' " + "AND wait_event = 'PgSleep' AND pid <> pg_backend_pid();") != "0", h.w) + assert slept and not db_past(e, h.n) + until_seen("the lookup waiting on a Lock", lambda: e.c.value( + "SELECT count(*) FROM pg_stat_activity WHERE query LIKE '%find_write%' " + "AND wait_event_type = 'Lock' AND pid <> pg_backend_pid();") != "0", h.w) + assert db_past(e, h.n) + script = lookups(h.w)[0]["stdin"] + entry = json.loads(ENTRY_IN_SCRIPT.search(h.w.commit_calls()[0]["stdin"]).group(1).replace("''", "'")) + for value in (h.write_id, entry["journal"]["lane"], entry["journal"]["verb"], + entry["journal"]["args_sha256"]): + assert value in script, f"the lookup's script does not carry {value!r}" + h.end("ROLLBACK") + assert h.finish() == 75, h.w.err[-800:] + + +@CLIENT +@pytest.mark.parametrize("ending", ["timer", "55p03", "connect", *NO_ANSWER]) +def test_a_lookup_try_without_an_answer_is_made_again_inside_the_window(estates, ending): + """The first try ends with no answer: [timer] held past `T_L`; [55p03] + the session holds the head row until the shim's ends show the try's + 55P03; [connect] a failed connect; and [57014], [exit-1], [signal], + [exit-3-no-sqlstate] and [unreadable-exit-0] (ERR-50; R-F11-5). The + session rolls back, and a second try, by the window's rules, takes the + lock and finds the write absent: exit 75, and the record removed.""" + e = estates() + first: List[Path] = [answering(e, NO_ANSWER[ending])] if ending in NO_ANSWER else [] + arms = [{"mode": "fail-match", "match": "find_write", "call": 1}] if ending == "connect" else [] + h = HalfOpen(e, arms=arms, first=first, hold_lookup=ending in ("timer", "55p03")) + if ending == "55p03": + h.release() + until_seen("the first try's end in 55P03", lambda: lookups(h.w) and lookups(h.w)[0]["call"] in h.w.ends(), h.w) + elif ending != "timer": + until_seen("the first try", lambda: h.tries(), h.w) + h.end("ROLLBACK") + code = h.w.wait() + h.finish() + assert code == 75 and pending(e) == [], f"exit {code}: {h.w.err[-800:]}" + h.tries_obey_the_window(least=2) + + +@CLIENT +def test_a_lookup_refused_for_another_generation_exits_2_and_keeps_the_record(estates): + """[generation]: while the first try is held the switch row's generation + becomes a new uuid, as a re-import's bootstrap sets it, and the session + rolls back. One try: LR007 `generation`; exit 2 with the server's + message, the record kept; a plain rerun then refuses with 2 at its first + snapshot, sending nothing.""" + e = estates() + argv = note(LA, "F3 generation") + h = HalfOpen(e, argv) + e.c.db.psql(f"UPDATE {REGISTER}.register_switch SET generation = '{uuid.uuid4()}';") + h.end("ROLLBACK") + assert h.finish() == 2 and "LR007" in h.w.err and len(pending(e)) == 1, h.w.err[-800:] + assert len(lookups(h.w)) == 1 + again = e.run(LA, argv) + assert again.code == 2 and again.commit_calls() == [], again.err[-800:] + + +@CLIENT +def test_a_lookup_refused_for_another_write_exits_2_and_removes_the_record(estates): + """[another-write]: the session's replay carries another `args_sha256` + under the same id and commits while the first try is held. One try: + LR007 `another-write`; exit 2, the record removed; the journal holds + only the session's entry for the id.""" + e = estates() + h = HalfOpen(e, alter=another_writes_script) + h.end("COMMIT") + assert h.finish() == 2 and "LR007" in h.w.err and pending(e) == [], h.w.err[-800:] + assert len(lookups(h.w)) == 1 + rows = e.rows(f"SELECT args_sha256 FROM {REGISTER}.journal WHERE write_id = '{h.write_id}'") + assert rows == [{"args_sha256": sha256(b"another write, T246")}], rows + + +@CLIENT +def test_a_lookup_made_too_early_exits_3_and_keeps_the_record(estates): + """[too-early]: a write killed once its in-doubt call returned, on + another host, keeps its `call` line. A plain rerun here, whose snapshot + the shim fails, takes the record over with no clock to age and calls at + once: LR011, exit 3 naming the id with the server's message, the record + kept, one try. Once snapshots pass, a plain rerun exits 0 with one row. + The other host's claim lives the seam's cap, so the first rerun runs at + `LANES_REGISTER_SEAM_CAP_S=1` (see the section's head; T246d), and so + does the last: the first rerun's 3 removes its own claim + (`c8acf67c:lanes-edit.sh:9734`), so the last meets the other host's + claim again, its cap counted afresh from the record's `updated_utc`, + which the takeover rewrote (T246e).""" + e = estates() + argv = note(LA, "F3 too early") + cap = {"LANES_REGISTER_SEAM_CAP_S": "1"} + _, write_id = killed(e, LA, argv, extra={"LANES_HOST": "eagle-2"}) + early = e.run(LA, argv, arms=[{"mode": "fail-match", "match": SNAPSHOT_TEXT, "call": 1}], extra=cap) + assert early.code == 3 and write_id in early.err and "LR011" in early.err, early.err[-800:] + assert len(lookups(early)) == 1 and len(pending(e)) == 1 + again = e.run(LA, argv, extra=cap) + assert again.code == 0 and [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + + +@pytest.mark.parametrize("case", ["missing", "ahead"]) +def test_find_write_refuses_lr011_at_once(clones, case): + """Phase N, direct calls: `p_not_after` NULL, or the clock plus 5 s, + under a 2 s `statement_timeout`, so a 3 s wait would end 57014 instead: + LR011 (lane 1's v2.6, behaviour 2).""" + c, w = at_lr011(clones) + sql = LOOKUP_SQL.replace("'6000ms'", "'2000ms'") + if case == "missing": + sql = sql.replace(":'not_after'::timestamptz", "nullif(:'not_after', '')::timestamptz") + proc = c.call(sql, appname="t246-lookup", **c.lookup_variables( + w["id"], "" if case == "missing" else c.clock(5), None, L1, VERB, w["args"])) + c.check_refused(proc, None, "LR011", None) + + +@CLIENT +def test_an_orphaned_commit_call_released_after_its_not_after_writes_nothing(estates): + """The shim forks the first commit call into its own session, held on a + FIFO before `psql`; the writer's 8 s timer kills the shim, not the call. + The write reaches its deadline, the lookup finds it absent and the + writer exits 75; then the held call is released: `commit_write` reaches + step 9 past `not_after`, raises LR008, and the head, the journal and the + lines are as before; the id stays absent.""" + e = estates() + orphan = fifo(e, "orphan") + w = e.run(LA, note(LA, "orphan"), extra=DEADLINE, + arms=[{"mode": "fork", "match": "commit_write", "call": 1, "fifo": str(orphan)}]) + assert w.code == 75, w.err[-800:] + write_id = w.write_ids()[0] + before = e.c.state() + hold_point(orphan, w).close() + until_seen("the orphan's end", lambda: e.c.value( + f"SELECT count(*) FROM pg_stat_activity WHERE query LIKE '%{write_id}%' " + "AND pid <> pg_backend_pid();") == "0") + assert e.c.state() == before and not e.has_write(write_id) + + +@pytest.mark.parametrize("case", ["past", "within"]) +def test_a_commit_past_its_not_after_raises_lr008_and_one_within_it_commits(clones, case): + """LR008's engine half, by direct calls: an entry whose `not_after` is + the clock plus 2 s, sent 3 s later, gets LR008 and changes nothing; the + same with the clock plus 8 s commits.""" + c, _ = at_lr011(clones) + entry = c.write_entry(seconds=2 if case == "past" else 8) + time.sleep(3) + if case == "past": + c.refused(entry, "LR008", None, "reached its end") + else: + assert c.committed(entry)[0] == c.head() + + +@CLIENT +def test_lr008_inside_the_deadline_is_a_lost_turn_and_the_write_exits_0(estates): + """LR008's client half (lane 4, 6709038351): the first commit call's + `not_after` is set past by the shim, so it answers LR008 inside its + timer. The writer drops that call's line, takes a fresh snapshot, makes + a new attempt under the same id, commits, exits 0 with one row, and + makes no lookup.""" + e = estates() + w = e.run(LA, note(LA, "lr008 inside"), + arms=[{"mode": "edit-entry", "match": "commit_write", "call": 1, "field": "not_after", "value": PAST}]) + assert w.code == 0, w.err[-800:] + assert len(w.commit_calls()) >= 2 and lookups(w) == [] + one_entry(e, w) + + +@CLIENT +@pytest.mark.parametrize("case", ["found", "absent", "held"]) +def test_lr008_at_the_deadline_leaves_the_exit_to_find_write(estates, case): + """The first call in doubt (AT-F3's estate) and each later call ending + LR008: the writer makes F3's lookup and exits as it says: [found] 0, + [absent] 75, [held] 3.""" + e = estates() + h = HalfOpen(e, arms=[{"mode": "edit-entry", "match": "commit_write", "from": 2, "field": "not_after", + "value": PAST}]) + if case != "held": + h.end("COMMIT" if case == "found" else "ROLLBACK") + code = h.finish() + else: + h.release() + code = h.w.wait() + h.finish() + assert lookups(h.w), "no lookup" + assert code == {"found": 0, "absent": 75, "held": 3}[case], h.w.err[-800:] + + +@CLIENT +def test_a_write_whose_every_call_ended_lr008_exits_75_with_no_lookup(estates): + """Every commit call answers LR008, so each dropped its `call` line and + the write was never in doubt: exit 75 with no lookup in the shim's log.""" + e = estates() + w = e.run(LA, note(LA, "lr008 every"), extra=DEADLINE, + arms=[{"mode": "edit-entry", "match": "commit_write", "field": "not_after", "value": PAST}]) + assert w.code == 75 and lookups(w) == [], w.err[-800:] + assert not e.has_write(w.write_ids()[0]) and pending(e) == [] + + +@CLIENT +@pytest.mark.parametrize("case", ["first-call", "every-call"]) +def test_lr008_alone_never_gives_3_or_69(estates, case): + """LR008 on the first call only, or on every call, at a 1 s deadline: + the exit is decided by a later attempt or the lookup, never 3 or 69.""" + e = estates() + arm = {"mode": "edit-entry", "match": "commit_write", "field": "not_after", "value": PAST} + if case == "first-call": + arm["call"] = 1 + w = e.run(LA, note(LA, f"lr008 {case}"), extra=DEADLINE, arms=[arm]) + assert w.code not in (3, 69), w.err[-800:] + + +# ============================================================ PHASE W: AT-F10 (T247) +# +# A PENDING RECORD AND AN UNRELATED IDENTICAL COMMAND (AT-F10, TP:6443-6580). +# The record's name `` is the entry's `journal.args_sha256`, the sha256 +# of `lanes-pending/1`, `LANES_LANE`, `LANES_SESSION`, the verb and each +# argument, NUL-separated; it is owned by the run in its highest-numbered +# claim file `.take.`, whose liveness is tested on that claim's pid +# and start time, never on the record's `pid`. The claims the test writes +# hold `pid=`, `pid_start=`, `host=`, `container=` and `utc=`, one per line, +# in that order (pending-record.md §4.1, the key `lanes-edit.sh` reads, +# `wr_claim_read`; T247b, lane 4's triage, #165 6833813092), each owner a live +# stand-in (`Owner`) where it is on this host. +# +# A CLAIM WITH NO RECORD BESIDE IT (Q-L2-6; R26, lane 4's answer to 42): +# `[live-at-deadline]` asserts the 3's text: it names the claim by pid, host +# and container and the UTC it was claimed; says the record is not yet +# written, so its write id is not known, and that this run sent nothing; and +# gives the effect check, `lanes-edit.sh history --since ` or +# `lanes-edit.sh register-row `. `[write-id-live-at-deadline]`: a run +# under `LANES_WRITE_ID` prints its own id again with its generation. +# +# They FAIL UNTIL L4-B2 lands, and the record-less text until lane 4's T417. +# Without PostgreSQL 16 each skips. + +MAX_AGE = {"LANES_REGISTER_PENDING_MAX_AGE_S": "2"} + + +class Owner: + """A live stand-in owner on this host: a process blocked on its stdin.""" + + def __init__(self) -> None: + self.proc = subprocess.Popen([sys.executable, "-I", "-c", "import sys; sys.stdin.read()"], + stdin=subprocess.PIPE) + self.pid, self.start = self.proc.pid, _proc_start(self.proc.pid) + self.ended_at: Optional[float] = None + + def end(self) -> None: + if self.proc.poll() is None: + self.proc.stdin.close() + self.proc.wait(timeout=30) + self.ended_at = time.time() + + +#: How long the stand-in owner lives once an identical run is launched (T247c). +OWNER_LIVES_S = 3 + + +def owner_lives(w: Writer) -> None: + """Let the run meet the live owner's claim for `OWNER_LIVES_S`, then + check it is still waiting, having sent nothing (T247c; PM33A-TRIAGE L6). + The run takes and waits on its claim before its first snapshot + (`pw_run`: `wr_claim_take`, then `pw_owner_live_end`; + `8ba10b4e:lanes-edit.sh:10820`), so it makes no shim call while the owner + lives: ending the owner on the run's first call waited for a call that + never came, and the run ended 3 at its deadline.""" + time.sleep(OWNER_LIVES_S) + assert w.ended is None, f"{w.name} exited {w.code} while the owner lived: {w.err[-400:]}" + assert w.calls() == [], f"{w.name} sent a call while the owner lived: {w.calls()[:1]}" + + +def dead_pid() -> int: + gone = subprocess.Popen([sys.executable, "-I", "-c", "pass"]) + gone.wait() + return gone.pid + + +def digest(lane: str, argv: Sequence[str]) -> str: + return sha256("\0".join(["lanes-pending/1", lane, session_of(lane), *argv]).encode("utf-8")) + + +def write_claim(e: Estate, d: str, k: int, pid: int, start: str = "", *, host: str = "eagle", + container: str = "none", utc: Optional[str] = None) -> Path: + where = e.state / PENDING + where.mkdir(mode=0o700, parents=True, exist_ok=True) + claim_file = where / f"{d}.take.{k}" + claim_file.write_text(f"pid={pid}\npid_start={start}\nhost={host}\ncontainer={container}\n" + f"utc={utc or e.utc()}\n", encoding="utf-8") + return claim_file + + +def set_field(path: Path, key: str, value: str) -> None: + lines = path.read_text(encoding="utf-8").splitlines() + path.write_text("\n".join(f"{key}={value}" if l.startswith(key + "=") else l for l in lines) + "\n", + encoding="utf-8") + + +def snapshot_state(e: Estate) -> Dict[str, str]: + where = e.state / PENDING + return {p.name: p.read_text(encoding="utf-8") for p in where.iterdir()} if where.is_dir() else {} + + +@CLIENT +def test_an_expired_pending_record_is_not_reused(estates): + """A record from a write that committed, older than the maximum age + (`LANES_REGISTER_PENDING_MAX_AGE_S=2`): the identical command warns, + prints the lookup, mints a new id and writes.""" + e = estates() + argv = note(LA, "expired record") + _, old = killed(e, LA, argv) + time.sleep(3) + w = e.run(LA, argv, extra=MAX_AGE) + assert w.code == 0 and "WARNING" in w.err and f"--write-id {old}" in w.err, w.err[-800:] + assert old not in w.write_ids() + ids = [r["write_id"] for r in e.journal_after()] + assert ids.count(old) == 1 and len(ids) == 2, ids + + +@CLIENT +@pytest.mark.parametrize("case", ["ends-in-time", "live-at-deadline"]) +def test_a_live_pending_record_is_waited_for_not_shared(estates, case): + """The second of two identical runs waits while the first's owner lives. + [ends-in-time]: the first ends inside the deadline, and the second writes + under its own id: two entries. [live-at-deadline]: at a 1 s deadline the + second exits 3, never 75, saying an identical write is still running in + the first's pid (write id, start UTC), with no commit call of its own, and + the record and its claims as the first left them.""" + e = estates() + gate = fifo(e, "first") + argv = note(LA, f"identical {case}") + first = e.launch(LA, argv, arms=[{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(gate)}]) + held = hold_point(gate, first) + left = snapshot_state(e) + second = e.launch(LA, argv, extra=DEADLINE if case == "live-at-deadline" else None) + if case == "ends-in-time": + until_seen("the second's first call", lambda: second.calls(), second) + held.close() + assert first.wait() == 0 and second.wait() == 0, second.err[-800:] + ids = {r["write_id"] for r in e.journal_after()} + assert len(ids) == 2 and set(second.write_ids()) & ids and not set(second.write_ids()) & set(first.write_ids()) + else: + assert second.wait() == 3, second.err[-800:] + assert "an identical write is still running in pid" in second.err and str(first.proc.pid) in second.err + assert first.write_ids()[0] in second.err and second.commit_calls() == [] + assert snapshot_state(e) == left + held.close() + assert first.wait() == 0 + + +@CLIENT +@pytest.mark.parametrize("case", ["host-now", "container-now", "claim-old-record-fresh", "both-old"]) +def test_a_claim_from_another_host_or_container_counts_as_live_for_120_s(estates, case): + """The highest claim names another host or container, with a pid dead + here; it counts as live for 120 s from the later of its UTC and the + record's `updated_utc`. [host-now], [container-now] and + [claim-old-record-fresh]: a plain rerun waits and at a 1 s deadline exits + 3, sending nothing, naming the claim's pid. [both-old]: it creates the + next claim and takes the record over.""" + e = estates() + argv = note(LA, f"other host {case}") + exit3(e, LA, argv) + rec = the_record(e)["path"] + pid, old = dead_pid(), e.utc(121 / 60) + other = {"container": "ctr-2"} if case == "container-now" else {"host": "eagle-2"} + write_claim(e, rec.name, 1, pid, utc=old if case in ("claim-old-record-fresh", "both-old") else e.utc(), **other) + if case == "both-old": + set_field(rec, "updated_utc", old) + gate = fifo(e, "both-old") + w = e.launch(LA, argv, arms=[{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(gate)}]) + held = hold_point(gate, w) + assert f"{rec.name}.take.2" in take_files(e), take_files(e) + held.close() + assert w.wait() == 0, w.err[-800:] + else: + w = e.run(LA, argv, extra=DEADLINE) + assert w.code == 3 and str(pid) in w.err and w.commit_calls() == [], w.err[-800:] + + +@CLIENT +@pytest.mark.parametrize("case", ["ends-in-time", "live-at-deadline"]) +def test_a_lanes_write_id_run_waits_for_a_live_owner(estates, case): + """A record whose highest claim's owner is a live stand-in, met by the + printed rerun under `LANES_WRITE_ID`. [live-at-deadline]: exit 3, no + commit call, the record and its claims unchanged. [ends-in-time]: the + owner ends; the run goes on, its one commit call after the owner's end, + and the write commits once under that id.""" + e = estates() + argv = note(LA, f"write id {case}") + _, write_id, generation = exit3(e, LA, argv) + rec = the_record(e)["path"] + owner = Owner() + try: + write_claim(e, rec.name, 1, owner.pid, owner.start) + left = snapshot_state(e) + w = e.launch(LA, argv, extra=dict(DEADLINE if case == "live-at-deadline" else {}, + LANES_WRITE_ID=write_id, LANES_WRITE_GENERATION=generation)) + if case == "ends-in-time": + owner_lives(w) + owner.end() + code = w.wait() + finally: + owner.end() + if case == "live-at-deadline": + assert code == 3 and w.commit_calls() == [] and snapshot_state(e) == left, w.err[-800:] + else: + assert code == 0, w.err[-800:] + commits = [c for c in w.stamps() if "commit_write" in (c.get("stdin") or "")] + assert len(commits) == 1 and commits[0]["t"] >= owner.ended_at + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + + +@CLIENT +@pytest.mark.parametrize("case", ["ends-in-time", "live-at-deadline", "write-id-live-at-deadline"]) +def test_a_claim_with_no_record_beside_it_is_waited_for(estates, case): + """`.take.0` with no record, its owner a live stand-in. At a 1 s + deadline an identical run exits 3 with no commit call and no claim of + its own; [live-at-deadline]'s text names the claim by pid, host, + container and UTC, says the record is not yet written so its write id is + not known and this run sent nothing, and gives the effect check; + [write-id-live-at-deadline] prints its own id again with its generation. + [ends-in-time]: the owner ends, and the run creates `.take.1`, writes + and commits once.""" + e = estates() + argv = note(LA, f"no record {case}") + d = digest(LA, argv) + owner = Owner() + utc = e.utc() + extra: Dict[str, str] = {} if case == "ends-in-time" else dict(DEADLINE) + mine = str(uuid.uuid4()) + if case == "write-id-live-at-deadline": + extra.update(LANES_WRITE_ID=mine, LANES_WRITE_GENERATION=e.c.switch()["generation"]) + try: + write_claim(e, d, 0, owner.pid, owner.start, utc=utc) + w = e.launch(LA, argv, extra=extra) + if case == "ends-in-time": + owner_lives(w) + owner.end() + code = w.wait() + finally: + owner.end() + if case == "ends-in-time": + assert code == 0 and len(e.journal_after()) == 1, w.err[-800:] + return + assert code == 3 and w.commit_calls() == [], w.err[-800:] + assert take_files(e) == [f"{d}.take.0"], take_files(e) + if case == "live-at-deadline": + for said in (str(owner.pid), "eagle", utc, "not yet written", "sent nothing"): + assert said in w.err, f"{said!r} is not in the 3's text: {w.err[-800:]}" + assert f"lanes-edit.sh history {LA} --since" in w.err or f"lanes-edit.sh register-row {LA}" in w.err + else: + assert f"LANES_WRITE_ID={mine}" in w.err and extra["LANES_WRITE_GENERATION"] in w.err, w.err[-800:] + + +@CLIENT +def test_a_reused_pid_in_a_pending_record_reads_as_dead(estates): + """The highest claim's pid is live, but its start time differs: the + owner reads as dead, so the run creates the next claim and takes the + record over.""" + e = estates() + argv = note(LA, "reused pid") + _, write_id, _ = exit3(e, LA, argv) + rec = the_record(e)["path"] + owner = Owner() + try: + write_claim(e, rec.name, 1, owner.pid, "1") + w = e.run(LA, argv) + finally: + owner.end() + assert w.code == 0, w.err[-800:] + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + + +@CLIENT +@pytest.mark.parametrize("case", ["record-pid-live", "record-pid-dead"]) +def test_liveness_is_the_highest_claims_never_the_records_pid(estates, case): + """[record-pid-live]: the record's `pid` is live, its highest claim's + owner dead: taken over. [record-pid-dead]: the converse is waited for, + and at a 1 s deadline the run exits 3 with no commit call.""" + e = estates() + argv = note(LA, f"liveness {case}") + _, write_id, _ = exit3(e, LA, argv) + rec = the_record(e)["path"] + owner = Owner() + try: + if case == "record-pid-live": + set_field(rec, "pid", str(owner.pid)) + write_claim(e, rec.name, 1, dead_pid()) + else: + set_field(rec, "pid", str(dead_pid())) + write_claim(e, rec.name, 1, owner.pid, owner.start) + w = e.run(LA, argv, extra=DEADLINE if case == "record-pid-dead" else None) + finally: + owner.end() + if case == "record-pid-live": + assert w.code == 0 and [r["write_id"] for r in e.journal_after()].count(write_id) == 1, w.err[-800:] + else: + assert w.code == 3 and w.commit_calls() == [], w.err[-800:] + + +@CLIENT +def test_the_pending_digest_covers_the_session_and_equals_args_sha256(estates): + """The record is 0600 in a 0700 directory, and its name equals the + entry's `args_sha256`, the digest of `lanes-pending/1`, the lane, the + session, the verb and each argument, NUL-separated. Two runs differing + only in `LANES_SESSION` have different digests, and neither takes the + other's record: two records while both are held, two entries after.""" + e = estates() + argv = note(LA, "digest") + gates = fifo(e, "digest-1"), fifo(e, "digest-2") + one = e.launch(LA, argv, arms=[{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(gates[0])}]) + first = hold_point(gates[0], one) + rec = the_record(e)["path"] + entry = json.loads(ENTRY_IN_SCRIPT.search(one.commit_calls()[0]["stdin"]).group(1).replace("''", "'")) + assert rec.name == entry["journal"]["args_sha256"] == digest(LA, argv) + assert rec.stat().st_mode & 0o777 == 0o600 and rec.parent.stat().st_mode & 0o777 == 0o700 + two = e.launch(LA, argv, extra={"LANES_SESSION": str(uuid.uuid4())}, + arms=[{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(gates[1])}]) + second = hold_point(gates[1], two) + assert len(pending(e)) == 2, pending(e) + first.close() + second.close() + assert (one.wait(), two.wait()) == (0, 0) + assert len({r["write_id"] for r in e.journal_after()}) == 2 + + +@CLIENT +@pytest.mark.parametrize("case", ["arguments", "verb", "generation"]) +def test_a_write_id_of_other_arguments_verb_or_generation_is_refused_2_at_the_snapshot(estates, case): + """A `LANES_WRITE_ID` naming a committed entry of other arguments, or + of another verb, or a printed rerun whose `LANES_WRITE_GENERATION` is + not the switch's: exit 2 at the snapshot, naming the id (and what + differs), with no commit call, and nothing written.""" + e = estates() + w = e.run(LA, note(LA, "the entry")) + assert w.code == 0 + write_id = w.write_ids()[0] + generation = e.c.switch()["generation"] if case != "generation" else str(uuid.uuid4()) + argv = {"arguments": note(LA, "other arguments"), "verb": ["log", "NOTED", f"lane:{LA}", "T247 other verb"], + "generation": note(LA, "the entry")}[case] + before = e.journal_after() + again = e.run(LA, argv, extra={"LANES_WRITE_ID": write_id, "LANES_WRITE_GENERATION": generation}) + assert again.code == 2 and again.commit_calls() == [] and write_id in again.err, again.err[-800:] + assert e.journal_after() == before + + +@CLIENT +@pytest.mark.parametrize("case", ["fresh", "past-max-age"]) +def test_a_record_of_another_generation_is_refused_2_until_lanes_write_id_new(estates, case): + """A record of another generation, fresh or past the maximum age: a + plain rerun refuses with 2 at its first snapshot, with no commit call; + its text names the write id, `created_utc` and both generations, the + effect check, and `LANES_WRITE_ID=new`; the record is left as it is, + `lanes-index status` lists it, and a second plain rerun refuses the same + way.""" + e = estates() + argv = note(LA, f"generation {case}") + _, write_id, generation = exit3(e, LA, argv) + rec = the_record(e) + other = str(uuid.uuid4()) + set_field(rec["path"], "generation", other) + extra = dict(MAX_AGE) if case == "past-max-age" else {} + if extra: + time.sleep(3) + left = snapshot_state(e) + for _ in range(2): + w = e.run(LA, argv, extra=extra) + assert w.code == 2 and w.commit_calls() == [], w.err[-800:] + for said in (write_id, rec["fields"].get("created_utc", "created_utc"), other, generation, + "LANES_WRITE_ID=new"): + assert said in w.err, f"{said!r} is not in the refusal: {w.err[-800:]}" + assert f"lanes-edit.sh history {LA} --since" in w.err or f"lanes-edit.sh register-row {LA}" in w.err + assert snapshot_state(e) == left + env = dict(e.env, PATH=os.pathsep.join([str(e.psql_dir(f"status-{case}")), e.env["PATH"]])) + status = subprocess.run([str(e.bindir / "lanes-index"), "status"], capture_output=True, text=True, + env=env, check=False, timeout=120) + assert write_id in status.stdout, status.stdout[-800:] + + +@CLIENT +def test_a_write_id_already_journaled_for_the_same_arguments_exits_0_at_the_snapshot(estates): + """A `LANES_WRITE_ID` rerun of a write already in the journal with the + same arguments exits 0 at the snapshot, with no commit call, and the + journal holds it once.""" + e = estates() + argv = note(LA, "journaled") + w = e.run(LA, argv) + assert w.code == 0 + write_id = w.write_ids()[0] + again = e.run(LA, argv, extra={"LANES_WRITE_ID": write_id, + "LANES_WRITE_GENERATION": e.c.switch()["generation"]}) + assert again.code == 0 and again.commit_calls() == [], again.err[-800:] + assert [r["write_id"] for r in e.journal_after()].count(write_id) == 1 + + +# ============================================================ PHASE N: AT-LR006 and AT-F33 (T251) +# +# TWO ATTEMPTS OF ONE WRITE MEETING AT THE COMMIT (AT-LR006, TP:4595-4754; +# AT-F33, TP:4755-4786), on T222's clones, as the writer login in the commit +# call's own form. Attempts A and B carry one write W: the same id, lane, +# verb and `args_sha256`, and a `not_after` well past the case. A is a +# session of its own whose transaction is held open on its stdin; B's +# snapshot read is one `REPEATABLE READ READ ONLY` transaction as the +# writer; every barrier is a state polled in `pg_stat_activity` by +# `application_name` (A `idle in transaction`, or B waiting on a `Lock` with +# `pg_blocking_pids` naming A). The no-lookup control is `commit_write`'s +# own text from `pg_get_functiondef`, step 1's lookup cut and nothing else, +# created in the case's clone as `lanes_register_owner` and granted to the +# writer. The client's halves (`register commit` exits 0, 75 or 2, and the +# write exits 0 once its next snapshot finds W) stand as the next snapshot +# read finding W at n+1. +# +# They FAIL UNTIL L1-A6 (T107), with L4-B3 and L4-B5 for the client's +# halves. Without PostgreSQL 16 each skips. + +_SESSIONS = itertools.count(1) + + +class Two: + """AT-LR006's fixture on one clone at `primary` with W's entry made: + the commit calls of A and B, their sessions and their outcomes.""" + + def __init__(self, c: Clone, fn: str = "commit_write") -> None: + self.c, self.fn = c, fn + self.entry = c.write_entry(seconds=600) + self.wid, self.n = str(uuid.uuid4()), c.head() + self.gen, self.client = c.switch()["generation"], c.client() + self.outcomes: List[Tuple[str, int, str]] = [] + + def sql(self, base: int, *, entry: Optional[dict] = None, level: str = "SERIALIZABLE", + gen: Optional[str] = None, commit: bool = True) -> str: + body = json.dumps(entry or self.entry, ensure_ascii=False) + return (f"BEGIN ISOLATION LEVEL {level};\n" + f"SELECT {REGISTER}.{self.fn}({base}, '{self.wid}'::uuid, {self.client}, " + f"'{gen or self.gen}'::uuid, $t251${body}$t251$::jsonb);\n" + ("COMMIT;\n" if commit else "")) + + def session(self, name: str) -> subprocess.Popen: + appname = f"t251-{name}-{next(_SESSIONS)}" + proc = subprocess.Popen(self.c.argv({}), stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, env=self.c.db.env(WRITER_LOGIN, appname)) + proc.appname = appname + return proc + + def state_of(self, appname: str) -> str: + return self.c.value(f"SELECT coalesce(string_agg(state, ','), '') FROM pg_stat_activity " + f"WHERE application_name = '{appname}';") + + def hold_a(self, entry: Optional[dict] = None) -> subprocess.Popen: + """A runs `commit_write(n, W)` and stays idle in transaction.""" + a = self.session("a") + a.stdin.write(self.sql(self.n, entry=entry, commit=False)) + a.stdin.flush() + until(self.c, lambda: self.state_of(a.appname) == "idle in transaction", "A idle in transaction") + return a + + def finish(self, proc: subprocess.Popen, who: str, tail: str = "") -> Tuple[int, str]: + if tail: + proc.stdin.write(tail) + proc.stdin.close() + proc.stdin = None + _, err = proc.communicate(timeout=30) + self.outcomes.append((who, proc.returncode, err)) + return proc.returncode, err + + def call_b(self, base: int, **options) -> Tuple[int, str]: + proc = self.c.call(self.sql(base, **options), appname="t251-b") + self.outcomes.append(("B", proc.returncode, proc.stderr)) + return proc.returncode, proc.stderr + + def start_b(self, base: int, **options) -> subprocess.Popen: + appname = f"t251-bw-{next(_SESSIONS)}" + proc = self.c.start(self.sql(base, **options), appname) + proc.appname = appname + return proc + + def b_waits_on(self, b: subprocess.Popen, a: subprocess.Popen) -> None: + until(self.c, lambda: self.c.value( + "SELECT count(*) FROM pg_stat_activity w JOIN pg_stat_activity h " + f"ON h.application_name = '{a.appname}' WHERE w.application_name = '{b.appname}' " + "AND w.wait_event_type = 'Lock' AND h.pid = ANY (pg_blocking_pids(w.pid));") == "1", + "B waiting on a Lock that A blocks") + + def ended(self, proc: subprocess.Popen, who: str) -> Tuple[int, str]: + _, err = proc.communicate(timeout=30) + self.outcomes.append((who, proc.returncode, err)) + return proc.returncode, err + + def snapshot(self) -> str: + """B's snapshot read: W's seq, or `absent`.""" + proc = self.c.call("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY;\n" + f"SELECT coalesce((SELECT seq FROM {REGISTER}.journal WHERE write_id = " + f"'{self.wid}')::text, 'absent');\nCOMMIT;\n", appname="t251-snap") + assert proc.returncode == 0, proc.stderr + return proc.stdout.strip() + + def rows(self) -> int: + return int(self.c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{self.wid}';")) + + +def code_of(rc: int, err: str) -> str: + if rc == 0: + return "committed" + m = re.match(r"(?:ERROR|FATAL): ([0-9A-Z]{5}):", err.splitlines()[0] if err else "") + return m.group(1) if m else f"exit {rc}" + + +def control(c: Clone) -> str: + """The no-lookup control, `commit_write_nolookup`, in this clone only.""" + text = c.value(f"SELECT pg_get_functiondef('{REGISTER}.commit_write'::regproc);") + cut = re.subn(r"SELECT \* INTO v_old FROM journal WHERE write_id = p_write_id;.*?ERRCODE = 'LR006'; END IF;", + "", text, count=1, flags=re.S) + assert cut[1] == 1, "commit_write holds no step-1 lookup to cut for the control" + made = cut[0].replace(f"FUNCTION {REGISTER}.commit_write(", f"FUNCTION {REGISTER}.commit_write_nolookup(", 1) + done = c.db.psql(f"SET ROLE lanes_register_owner;\n{made};\nRESET ROLE;\n" + f"GRANT EXECUTE ON FUNCTION {REGISTER}.commit_write_nolookup(bigint, uuid, integer, uuid, jsonb) " + "TO lanes_register_writer;\n") + assert done.returncode == 0, done.stderr + return "commit_write_nolookup" + + +def two(clones, fn: str = "commit_write", c: Optional[Clone] = None) -> Two: + if c is None: + c = clones() + c.to_primary() + return Two(c, control(c) if fn != "commit_write" else fn) + + +# -------------------------------------------- the scenarios, each run by its case and by AT-F33's matrix +def case_0(t: Two) -> str: + """A commits before B's snapshot read; B's call made anyway.""" + t.finish(t.hold_a(), "A", "COMMIT;\n") + assert t.snapshot() == str(t.n + 1) + return code_of(*t.call_b(t.n + 1)) + + +def lr006_1(t: Two, variant: str) -> Tuple[str, str]: + if variant == "held": + a = t.hold_a() + said = t.snapshot() + t.finish(a, "A", "COMMIT;\n") + else: + said = t.snapshot() + t.finish(t.hold_a(), "A", "COMMIT;\n") + rc, err = t.call_b(t.n) + return said, err.splitlines()[0] if rc == 3 and err else code_of(rc, err) + + +def lr006_2(t: Two, base: str) -> str: + t.finish(t.hold_a(), "A", "COMMIT;\n") + return code_of(*t.call_b(t.n if base == "stale-base" else t.n + 1)) + + +def lr006_3(t: Two, level: str, resend: bool = False) -> List[str]: + a = t.hold_a() + assert t.snapshot() == "absent" + b = t.start_b(t.n, level=level) + t.b_waits_on(b, a) + t.finish(a, "A", "COMMIT;\n") + codes = [code_of(*t.ended(b, "B"))] + if resend: + codes.append(code_of(*t.call_b(t.n + 1, level=level))) + return codes + + +def lr006_4(t: Two, resend: bool = False) -> List[str]: + a = t.hold_a() + b = t.session("b4") + b.stdin.write("BEGIN ISOLATION LEVEL SERIALIZABLE;\nSELECT pg_current_snapshot();\n") + b.stdin.flush() + until(t.c, lambda: t.state_of(b.appname) == "idle in transaction", "B's snapshot taken") + t.finish(a, "A", "COMMIT;\n") + rest = t.sql(t.n).split("\n", 1)[1] + codes = [code_of(*t.finish(b, "B", rest))] + if resend: + codes.append(code_of(*t.call_b(t.n + 1))) + return codes + + +def lr006_5(t: Two, resend: bool = False) -> List[str]: + a = t.hold_a() + b = t.start_b(t.n) + t.b_waits_on(b, a) + codes = [code_of(*t.ended(b, "B"))] + t.finish(a, "A", "COMMIT;\n") + if resend: + codes.append(code_of(*t.call_b(t.n + 1))) + return codes + + +def lr006_6(t: Two, variant: str) -> Tuple[str, str]: + a = t.hold_a() + if variant == "rollback": + b = t.start_b(t.n) + t.b_waits_on(b, a) + a_code = code_of(*t.finish(a, "A", "ROLLBACK;\n")) + return a_code, code_of(*t.ended(b, "B")) + until(t.c, lambda: t.state_of(a.appname) == "", "A's session ended by the 5 s idle bound", deadline=15) + rc, err = t.finish(a, "A", "COMMIT;\n") + assert rc == 2 and "25P03" in err, (rc, err) + assert t.snapshot() == "absent" + return "25P03", code_of(*t.call_b(t.n)) + + +# ------------------------------------------------------------ AT-LR006 +@pytest.mark.parametrize("variant", ["held", "whole"]) +def test_an_attempt_committed_after_the_snapshot_read_gives_lr006(clones, variant): + """B's snapshot read finds W absent at head n; A commits W ([held]: A's + transaction open across the read; [whole]: A runs whole after it); B's + commit call exits 3 with exactly `ERROR: LR006: write W is in the + journal at seq n+1`; one row for W at n+1, and the head is n+1.""" + t = two(clones) + said, first = lr006_1(t, variant) + assert said == "absent" + assert first == f"ERROR: LR006: write {t.wid} is in the journal at seq {t.n + 1}", first + assert t.rows() == 1 and t.c.head() == t.n + 1 + + +@pytest.mark.parametrize("base", ["stale-base", "fresh-base"]) +def test_lr006_comes_before_lr001(clones, base): + """W committed at n+1: a commit call with the stale base n, or the fresh + n+1, gives LR006, never LR001, and writes nothing.""" + t = two(clones) + t.finish(t.hold_a(), "A", "COMMIT;\n") + before = t.c.state() + rc, err = t.call_b(t.n if base == "stale-base" else t.n + 1) + assert code_of(rc, err) == "LR006" and t.c.state() == before, err + + +@pytest.mark.parametrize("level", ["SERIALIZABLE", "REPEATABLE READ"], ids=["serializable", "repeatable-read"]) +def test_an_attempt_committed_while_the_commit_waits_gives_40001_then_exit_0(clones, level): + """A holds; B's snapshot read; B's commit call waits on a lock A blocks; + A commits. B gets 40001, never LR006 or 23505; the next snapshot read + finds W at n+1; one row for W.""" + t = two(clones) + assert lr006_3(t, level) == ["40001"] + assert t.snapshot() == str(t.n + 1) and t.rows() == 1 + + +def test_an_attempt_committed_after_the_commits_snapshot_gives_40001_without_a_wait(clones): + """A holds; B begins SERIALIZABLE and takes its snapshot with `SELECT + pg_current_snapshot()`; A commits; then B calls `commit_write`: 40001, + with no lock wait. The next snapshot finds W; one row.""" + t = two(clones) + assert lr006_4(t) == ["40001"] + assert t.snapshot() == str(t.n + 1) and t.rows() == 1 + + +def test_an_attempt_holding_past_lock_timeout_gives_55p03_then_exit_0(clones): + """A holds and neither commits nor rolls back; B's commit call, seen + waiting, gets 55P03 at its 2000 ms `lock_timeout`. After A commits the + next snapshot finds W; one row.""" + t = two(clones) + assert lr006_5(t) == ["55P03"] + assert t.snapshot() == str(t.n + 1) and t.rows() == 1 + + +@pytest.mark.parametrize("variant", ["rollback", "idle-bound"]) +def test_an_attempt_that_never_commits_leaves_the_write_to_the_retry(clones, variant): + """[rollback]: A holds, B is seen waiting, A rolls back: B commits W at + n+1. [idle-bound]: the role's 5 s idle bound ends A's session; A's + COMMIT then exits 2 with 25P03 on its `FATAL:` line; B's snapshot finds + W absent, and B commits W at n+1. One row, no 23505.""" + t = two(clones) + _, b = lr006_6(t, variant) + assert b == "committed" and t.rows() == 1 + assert int(t.c.value(f"SELECT seq FROM {REGISTER}.journal WHERE write_id = '{t.wid}';")) == t.n + 1 + assert not any("23505" in err for _, _, err in t.outcomes) + + +def test_the_no_lookup_control_gives_lr001_where_the_lookup_gives_lr006(clones): + """AT-LR006-1's interleaving against the control: LR001, where + `commit_write` gives LR006 (so the test detects the lookup's absence); + and AT-LR006-2's fresh base against it: 23505 on + `journal_write_id_key`.""" + t = two(clones, "control") + _, first = lr006_1(t, "held") + assert first.startswith("ERROR: LR001: "), first + u = two(clones, "control", c=t.c) + u.finish(u.hold_a(), "A", "COMMIT;\n") + rc, err = u.call_b(u.n + 1) + assert code_of(rc, err) == "23505" and "journal_write_id_key" in err, err + + +def test_another_writes_entry_committed_after_the_snapshot_read_gives_lr007_another_write(clones): + """A carries W's id with other arguments and commits after B's snapshot + read: B exits 3, `ERROR: LR007: write W is seq n+1, another write's + (F10)` then exactly `DETAIL: another-write`; the journal holds A's one + row. Under `VERBOSITY=sqlstate` the same call prints `ERROR: LR007` + alone, with no `DETAIL:` line.""" + t = two(clones) + other = json.loads(json.dumps(t.entry)) + other["journal"]["args_sha256"] = sha256(b"another write, T251") + assert t.snapshot() == "absent" + t.finish(t.hold_a(entry=other), "A", "COMMIT;\n") + before = t.c.state() + rc, err = t.call_b(t.n) + t.c.check_refused(subprocess.CompletedProcess([], rc, "", err), before, "LR007", "another-write", + f"write {t.wid} is seq {t.n + 1}, another write's (F10)") + assert t.rows() == 1 + terse = subprocess.run([a if a != "VERBOSITY=verbose" else "VERBOSITY=sqlstate" for a in t.c.argv({})], + input=t.sql(t.n), capture_output=True, text=True, timeout=60, + env=t.c.db.env(WRITER_LOGIN, "t251-terse"), check=False) + assert terse.stderr.splitlines()[:1] == ["ERROR: LR007"] and "DETAIL:" not in terse.stderr, terse.stderr + + +def test_a_generation_change_after_the_snapshot_read_gives_lr007_generation(clones): + """B's snapshot read; then the restore form's after-loss cutover commits + at n+1 (a new generation, the marker `adding`); then B's commit call + with the generation it read: LR007 `generation`, not LR002 or LR001; + no row for W.""" + t = two(clones) + assert t.snapshot() == "absent" + t.c.committed(t.c.operator_entry(after_loss=True, database_label=RESTORED_LABEL)) + assert t.c.switch()["generation"] != t.gen + before = t.c.state() + rc, err = t.call_b(t.n) + t.c.check_refused(subprocess.CompletedProcess([], rc, "", err), before, "LR007", "generation") + assert t.rows() == 0 + + +# ------------------------------------------------------------ AT-F33 +def matrix(clones, fn: str) -> List[Tuple[str, int, str]]: + """Case 0 and AT-LR006-1 to -6, each on a clone of its own, with the + fresh-base resend after each 40001 or 55P03, run twice. Returns every + session's outcome, and checks one row for W after each case.""" + seen: List[Tuple[str, int, str]] = [] + runs = [lambda t: case_0(t)] + runs += [lambda t, v=v: lr006_1(t, v) for v in ("held", "whole")] + runs += [lambda t, b=b: lr006_2(t, b) for b in ("stale-base", "fresh-base")] + runs += [lambda t, lv=lv: lr006_3(t, lv, resend=True) for lv in ("SERIALIZABLE", "REPEATABLE READ")] + runs += [lambda t: lr006_4(t, resend=True), lambda t: lr006_5(t, resend=True)] + runs += [lambda t, v=v: lr006_6(t, v) for v in ("rollback", "idle-bound")] + for _ in range(2): + for run in runs: + t = two(clones, fn) + run(t) + seen += t.outcomes + if fn == "commit_write": + assert t.rows() == 1, f"{t.rows()} rows for W" + assert t.c.value(f"SELECT count(*) = count(DISTINCT write_id) FROM {REGISTER}.journal;") == "t" + return seen + + +def test_no_interleaving_of_two_attempts_of_one_write_reaches_23505(clones): + """Every B call ends committed, LR006, 40001 or 55P03; A's committed, + rolled back, or 25P03; 23505 occurs in no session's output; one row for + W after each case.""" + seen = matrix(clones, "commit_write") + for who, rc, err in seen: + allowed = {"committed", "LR006", "40001", "55P03"} if who == "B" else {"committed", "25P03"} + assert code_of(rc, err) in allowed, (who, rc, err[:300]) + assert not any("23505" in err for _, _, err in seen) + + +def test_the_no_lookup_control_reaches_23505_so_the_oracle_can_fail(clones): + """The same matrix against the no-lookup control shows 23505 on + `journal_write_id_key` at least once, or the oracle above could not + have caught the branch.""" + seen = matrix(clones, "control") + hits = [err for _, _, err in seen if "23505" in err and "journal_write_id_key" in err] + print(f"[T251 control] 23505 on journal_write_id_key: {len(hits)}") + assert hits, "the control never reached 23505" diff --git a/tests/test_sql_register_order.py b/tests/test_sql_register_order.py new file mode 100644 index 00000000..36b921cb --- /dev/null +++ b/tests/test_sql_register_order.py @@ -0,0 +1,4758 @@ +# SPDX-License-Identifier: Apache-2.0 +"""AT-LR5's `LANDING EXPIRED` (F13) and take-with-no-UTC (U3) cases (#165, L2-W4). + +PHASE N, ON A REAL POSTGRESQL 16: `commit_write`'s step 7 is lane 1's SQL +(design.md §1.3's step 7, §1.3.1), so each case calls it directly, with no +verb; the verb is AT-F13's. The rest of AT-LR5 is T232's, in this module too. + +THE FIXTURE, AT-LR006'S, ON A CLONE OF ITS OWN: each case clones +`tmpl_imported` (section 3.1; T227), runs §1.4's two database-level +statements on the clone, since a clone does not copy `pg_database.datacl`, +and drops it `WITH (FORCE)` at the end. `Clone.to_primary` then brings it to +`primary` with the marker `present` by the documented functions, as +REGISTER-WRITER: `commit_write` with a `cutover` entry over the template's +import, then `record_marker` (TP:4610–4615; the plan's fixtures at TP:4354 +and :4806). Every call logs in as the fixture's writer login, `lanes_w_fx` +(T228), whose privileges come through REGISTER-WRITER, in the commit call's +own form: + + psql -X -q -A -t -v ON_ERROR_STOP=1 -v VERBOSITY=verbose -v SHOW_CONTEXT=never + BEGIN ISOLATION LEVEL SERIALIZABLE; + SELECT lanes_register.commit_write(:base, :'wid'::uuid, :client, :'gen'::uuid, :'entry'::jsonb); + COMMIT; + +with the script on stdin, so a refusal's first stderr line is +`ERROR: : ` and its second `DETAIL: ` (U7). + +EACH ENTRY IS AN APPEND OF ONE LINE TO `lanes/LANES.md`, built as AT-LR006 +builds one: the edit, the file's digest after it (`render_file`'s bytes, +computed here from `doc_lines` as the superuser reads them), and the line's +`register_lines` row, whose fields the test sets as the client would parse +them. Lines name `opensoft/r9`, which `lanes/repos.tsv` maps from `r9`, so a +row's `repo_spellings` is `{opensoft/r9,r9}` where the case says both. A +take's `utc_ts` is set from the database's clock at the snapshot, past Rule +6's 30 minutes where the case says so, so no case waits for the window. + +THE ORACLE OF A REFUSAL: `psql` exits 3; its first stderr line is the +code's, naming the rule; its second is exactly the rule's `DETAIL:` token; +and the head, the journal, its edits, the files, `doc_lines`, +`register_lines` and the switch equal their values before, with the write id +absent. + +Every case FAILS UNTIL lane 1's T107 (L1-A6, `commit_write` and its step 7) +and T118 (L1-A16, the kinds' slots), and lane 2's T227 (`tmpl_imported`) and +T228 (`lanes_w_fx`) land. Without PostgreSQL 16 each skips. + +THE MODULE ALSO HOLDS, each at its own section below, in this order: T243's +test 4, C10, D19 and section 4.1 (phase W, the client's writers on a real +register); T248's AT-F14 (phase W); T233's AT-F5 group A (phase N, the act's +client half, its off-primary and walk cases with no engine); T252's AT-F5 +group B (phase W); +T232's AT-LR5 and AT-LR009 (phase N, `commit_write` called directly on this +section's `Clone`, as here), whose rollback entry is T304's form; and, last, +T243d's `register commit` path refusal (lane 1's follow-up 2: phase W, as +T243's, but with no engine). +""" + +from __future__ import annotations + +import atexit +import errno +import hashlib +import itertools +import json +import math +import os +import re +import shlex +import shutil +import subprocess +import sys +import tempfile +import threading +import time +import uuid +from pathlib import Path +from typing import Dict, List, Optional, Sequence, Tuple + +import pytest + +from pgcluster import SUPERUSER, Database +from sql_register_helpers import (LANES_INDEX, PSQL_SHIM, PSQL_SHIM_CONTROL, PSQL_SHIM_ENDS, + PSQL_SHIM_LOG, REPO, flipped_copy, guard_session) +from test_lane_start_claude_current import Sandbox, _write + +#: THE LANDING GATE'S TABLE (T280's `SQL_REGISTER_GATE_TABLE`; task sets +#: version 12b's rule 11, lane 3, opensoft/openRepoTools#165 6877673742): +#: the 007 tasks each id waits for, from the Orders of the module's six +#: tasks, T223, T232, T233, T243, T248 and T252 (tasks:2140, :2239, :2247, +#: :2341, :2384 and :2397 at `204145ae`). Each row names its task and the +#: clause it rests on. A key is a test's name, or one id, `name[param]`, +#: whose row takes the place of its function's; the module has no `"*"` +#: row, since its waits share no task. The table is lane 2's GP-24 plan's +#: (`GP-24-gate-tables-plan.md`, sha256 `16d738b9`), with T415 added to two +#: T233 rows ahead of T233's Order, since each writes on primary through +#: T415's `pw_run` (lane 4, opensoft/openRepoTools#165 6879601079; T223c). +#: Only where the gate is active (`tests/sql_register_gate.py`) does it skip +#: an id until all its tasks have landed; elsewhere it changes nothing. +SQL_REGISTER_GATE_TABLE = { + "test_a_child_at_the_seam_stops_with_none_and_runs_no_tail": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_a_handoff_request_never_refuses_the_holders_own_handoff": ("T107", "T108", "T111", "T112", "T410", "T415"), # T252; tasks:2397 + "test_a_hold_line_that_names_no_repository_is_refused_with_2": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_a_landing_expired_closes_only_the_holders_landings_for_that_pr_and_repository": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_a_landing_expired_for_a_stale_landing_commits_and_frees_main_in_main_slots": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_a_landing_expired_that_names_no_stale_landing_is_lr005_and_writes_nothing": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_a_line_deriving_a_key_that_is_not_a_scope_exits_2_on_lr010_once": ("T107", "T108", "T111", "T112", "T410", "T415"), # T252; tasks:2397 + "test_a_malformed_act_is_refused_lr004_before_the_head_lock": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_a_refresh_or_a_line_of_no_kind_with_no_utc_commits_and_a_take_with_none_loads_at_files": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_a_refused_writer_replays_its_output_then_the_refusal": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_a_second_reach_of_the_seam_in_one_run_exits_2": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_a_serialization_failure_is_retried_never_reported_as_69": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_a_side_effect_runs_once_after_commit_and_never_for_a_refusal": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_a_slot_line_of_another_shape_commits_0": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_a_star_key_after_the_base_is_reported_as_the_first_listed_scope": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_a_stray_import_stores_its_keys_and_a_stray_revert_none": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_a_write_that_cannot_get_its_turn_exits_75_register_busy": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_a_writer_prints_its_output_once_however_many_attempts_it_made": ("T413", "T414", "T415", "T421"), # T248; tasks:2384 + "test_act_begin_on_primary_prints_id_seq_and_generation": ("T111", "T410", "T411"), # T233; tasks:2247 + "test_act_begin_prints_its_base_and_touches_no_database_off_primary": ("T111", "T410", "T411"), # T233; tasks:2247 + "test_act_begin_rebase_keeps_the_id_and_takes_the_new_base": ("T111", "T410", "T411", "T415"), # T233; tasks:2247, and T415 ahead of its Order (lane 4, opensoft/openRepoTools#165 6879601079) + "test_act_begin_rebase_refuses_with_2": ("T111", "T410", "T411"), # T233; tasks:2247 + "test_act_id_is_the_acts_id_on_a_write_and_null_on_every_other_entry": ("T107", "T108", "T227", "T228", "T304"), # T232; tasks:2239 + "test_act_variables_change_nothing_in_files_mode": ("T107", "T108", "T111", "T112", "T410", "T415"), # T252; tasks:2397 + "test_an_aborted_cutover_and_a_reimport_leave_no_scopes_at_a_deleted_seq": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_an_act_that_decided_on_canon_lane_is_refused_by_a_row_added_on_its_walk": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417"), # T252; tasks:2397 + "test_an_acts_own_earlier_write_never_refuses_its_later_one": ("T107", "T108", "T111", "T112", "T410", "T415"), # T252; tasks:2397 + "test_an_alias_edit_racing_a_claim_cannot_let_two_claims_of_one_object_pass": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_an_empty_expectation_records_act_id_and_checks_nothing": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_an_entry_outside_any_act_refuses_an_acts_write_lr009": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_an_import_a_cutover_and_a_rollback_store_only_the_star_key": ("T107", "T108", "T227", "T228", "T304"), # T232; tasks:2239 + "test_canon_lane_walk_prints_each_hop_with_canon_lanes_exit_code": ("T411",), # T233; tasks:2247 + "test_canon_lane_walk_whose_register_read_fails_off_primary_exits_1_and_prints_nothing": ("T411",), # T233; tasks:2247 + "test_claim_prints_its_register_seq": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_each_claim_and_main_rule_lr005_carries_its_detail_token": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_each_entry_stores_the_keys_its_lines_derive": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_each_line_kind_takes_or_releases_main_as_q17_b_says": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_each_of_the_three_conditions_holds": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417"), # T252; tasks:2397 + "test_lanes_act_is_unset_before_a_launch": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417"), # T252; tasks:2397 + "test_lr002_comes_before_lr009_after_a_rollback": ("T107", "T108", "T227", "T228", "T304"), # T232; tasks:2239 + "test_lr005_does_not_fire_on_a_released_claims_spellings": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_lr005_refuses_a_claim_whose_spellings_meet_another_lanes_live_claim": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_lr005_refuses_a_hold_over_another_lanes_open_taker": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_lr005_rolls_back_the_head_and_every_row": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_lr009_names_the_first_changed_scope_its_seq_lane_and_verb": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_on_primary_a_take_with_no_readable_utc_is_lr005_take_no_utc_and_writes_nothing": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_register_commit_refuses_an_unrenderable_path_with_2_before_the_store_opens": ("T107", "T108", "T112", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_seam_out_is_written_once_before_the_child_acts": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_ten_writers_released_together_all_commit_in_order": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_the_act_id_checks_refuse_an_act_id_on_another_kind_and_a_malformed_one": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_the_burst_window_is_effective_a_short_deadline_sends_writers_busy": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_the_claim_behind_the_alias_is_refused_by_lr005_on_its_spellings": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_the_client_refuses_a_bad_act_with_2_before_any_call": ("T111", "T410", "T411", "T415"), # T233; tasks:2247, and T415 ahead of its Order (lane 4, opensoft/openRepoTools#165 6879601079) + "test_the_client_refuses_an_acts_stale_base_with_2_at_the_snapshot": ("T107", "T108", "T111", "T112", "T410", "T415"), # T252; tasks:2397 + "test_the_commits_act_backstops_are_lr007_act_generation_and_lr004": ("T107", "T227", "T228"), # T252; tasks:2397 + "test_the_default_deadline_is_30_s_for_a_write_that_never_gets_its_turn": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_the_for_lane_checks_refuse_a_for_lane_off_a_landing_expired_and_a_landing_expired_that_closes_no_landing": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_the_holder_may_write_its_own_landing_expired": ("T107", "T108", "T115", "T118", "T227", "T228"), # T223; tasks:2140 + "test_the_image_cache_stays_content_addressed_under_the_burst_and_a_corrupt_object_is_refetched": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_the_later_hold_exits_2_and_prints_the_earlier_line": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_the_scratch_flag_is_honoured_only_for_its_own_attempt_directory": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_the_scratch_flag_never_reaches_git_gh_or_tmux": ("T413", "T414", "T415", "T423"), # T248; tasks:2384 + "test_two_claimants_one_claimed_and_the_other_exits_2": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_two_spellings_of_one_object_claimed_at_once_leave_one_claimed_and_exit_2": ("T107", "T108", "T227", "T228", "T302", "T413", "T414", "T415", "T421", "T422"), # T243; tasks:2341 + "test_two_spellings_of_one_repository_hold_one_main": ("T107", "T108", "T227", "T228"), # T232; tasks:2239 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[guard-answer-2-child]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T502", "T510"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-end-retire-rows]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-end-w13-set-row-state]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-end-w14-log-ended]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-handoff-late-paused]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-handoff-w11-append-line]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-handoff-w12-set-row-state]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-handoff-w9-log-paused]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-request-handoff]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T432"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-start-start-record]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T427", "T428"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-start-w1-add-row]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-start-w1-start-record]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[lane-start-w8-request-handoff]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_an_act_whose_scope_changed_before_its_commit_writes_nothing[skill-step-4-writes]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T431"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[guard-answer-2-child]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T502", "T510"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-end-retire-rows]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-end-w13-set-row-state]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-end-w14-log-ended]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-handoff-late-paused]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-handoff-w11-append-line]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-handoff-w12-set-row-state]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-handoff-w9-log-paused]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-request-handoff]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T432"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-start-start-record]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T427", "T428"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-start-w1-add-row]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-start-w1-start-record]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[lane-start-w8-request-handoff]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_an_act_whose_scopes_held_commits[skill-step-4-writes]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T431"), # T252; tasks:2397 + "test_each_of_the_three_conditions_holds[lane-end-gate]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T430"), # T252; tasks:2397 + "test_each_of_the_three_conditions_holds[lane-start-reread]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_each_of_the_three_conditions_holds[skill-step-4]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T431"), # T252; tasks:2397 + "test_lanes_act_is_unset_before_a_launch[exec]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T428"), # T252; tasks:2397 + "test_lanes_act_is_unset_before_a_launch[respawn-pane]": ("T107", "T108", "T410", "T411", "T413", "T414", "T415", "T417", "T429"), # T252; tasks:2397 +} + +REGISTER = "lanes_register" +IMPORTED = "tmpl_imported" +WRITER_LOGIN = "lanes_w_fx" +CONNECT_TO = "lanes_register_writer, lanes_register_reader, lanes_indexer" +LANES_MD, STORE = "lanes/LANES.md", "lanes/STORE" +WS, AUTHOR = "ws-t223", "T223 Fixture " +OPERATOR = "operator-t223" +MARKER_COMMIT = "c0ffee" + "0" * 34 +GIT_COMMIT, GIT_TREE = "1" * 40, "2" * 40 + +#: The lanes and the repository of the cases: L1 holds, L2 writes the +#: release, L3 holds nothing on `opensoft/r9`. +L1, L2, L3 = "L1-t223", "L2-t223", "L3-t223" +KEY, ALIAS, OTHER_KEY = "opensoft/r9", "r9", "opensoft/r8" +BOTH = [KEY, ALIAS] +PR, OTHER_PR = 7, 8 +BAD_STAMP = "2026-13-45T99:99Z" + +#: Q17 (B)'s slots for the kinds these cases write (lane 1's REGISTER_SLOTS). +SLOTS = {"HOLD": ("take", "hold"), "LANDING": ("take", "landing"), + "LANDED": ("close", "landing"), "LANDING EXPIRED": ("close", "landing"), + "HOLD (refresh)": (None, None), None: (None, None)} + +COMMIT_SQL = ("BEGIN ISOLATION LEVEL SERIALIZABLE;\n" + f"SELECT {REGISTER}.commit_write(:base, :'wid'::uuid, :client, :'gen'::uuid, :'entry'::jsonb);\n" + "COMMIT;\n") +MARKER_SQL = f"SELECT {REGISTER}.record_marker(:client, :seq, :'commit', :'ws');\n" + +#: Everything a refused call must leave as it was. +STATE_SQL = f""" +SELECT md5(concat_ws('|', + (SELECT seq::text FROM {REGISTER}.register_head), + (SELECT string_agg(j::text, ',' ORDER BY j.seq) FROM {REGISTER}.journal j), + (SELECT string_agg(e::text, ',' ORDER BY e.seq, e.ord) FROM {REGISTER}.journal_edits e), + (SELECT string_agg(f::text, ',' ORDER BY f.path) FROM {REGISTER}.files f), + (SELECT string_agg(d::text, ',' ORDER BY d.line_id) FROM {REGISTER}.doc_lines d), + (SELECT string_agg(r::text, ',' ORDER BY r.line_id) FROM {REGISTER}.register_lines r), + (SELECT string_agg(s::text, ',') FROM {REGISTER}.register_switch s))); +""" + +_NAMES = itertools.count(1) + + +def sha256(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def rendered(lines: Sequence[str], final_newline: bool) -> bytes: + """`render_file`'s bytes: the lines joined by LF, and a last LF where + the file ends with one and holds a line.""" + text = "\n".join(lines) + ("\n" if final_newline and lines else "") + return text.encode("utf-8") + + +class Clone: + """One clone of `tmpl_imported`, and the documented functions called on + it as REGISTER-WRITER, through `lanes_w_fx` (AT-LR006's fixture).""" + + def __init__(self, cluster, name: str) -> None: + self.cluster, self.name = cluster, name + self.db = Database(cluster, name) + + # -------------------------------------------------- the superuser's reads + def value(self, sql: str) -> str: + return self.db.psql(sql).stdout.strip() + + def json(self, sql: str): + return json.loads(self.value(sql)) + + def switch(self) -> dict: + return self.json(f"SELECT row_to_json(s) FROM {REGISTER}.register_switch s;") + + def head(self) -> int: + return int(self.value(f"SELECT seq FROM {REGISTER}.register_head;")) + + def client(self) -> int: + return int(self.value(f"SELECT version FROM {REGISTER}.schema_version;")) + + def state(self) -> str: + return self.value(STATE_SQL) + + def stamp(self, minutes_ago: float) -> Tuple[str, str]: + """The database's clock less `minutes_ago`: the line's UTC, as a line + spells it, and `utc_ts`.""" + said = self.value( + "SELECT to_char(t AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI\"Z\"') || '|' || t::text " + f"FROM (SELECT clock_timestamp() - make_interval(secs => {minutes_ago * 60}) AS t) s;") + utc, utc_ts = said.split("|") + return utc, utc_ts + + def not_after(self, seconds: float = 60) -> str: + return self.value(f"SELECT (clock_timestamp() + make_interval(secs => {seconds}))::text;") + + def file(self, path: str) -> Tuple[Optional[bool], List[str]]: + """`files.final_newline` (None where there is no such file) and the lines.""" + said = self.json( + f"SELECT json_build_object('nl', (SELECT final_newline FROM {REGISTER}.files WHERE path = '{path}'), " + f"'lines', (SELECT coalesce(jsonb_agg(text ORDER BY line_no), '[]') FROM {REGISTER}.doc_lines " + f"WHERE path = '{path}'));") + return said["nl"], said["lines"] + + def open_takes(self, lane: str, slot_kind: str) -> List[List[str]]: + out = self.value(f"SELECT coalesce(json_agg(json_build_array(repo_key, pr) ORDER BY line_id), '[]') " + f"FROM {REGISTER}.main_slots WHERE lane = '{lane}' AND slot_kind = '{slot_kind}';") + return json.loads(out) + + def no_taker(self) -> None: + """The cases' lines must meet no take of the template's own.""" + held = self.value(f"SELECT count(*) FROM {REGISTER}.main_slots WHERE repo_key IN " + f"('{KEY}', '{ALIAS}', '{OTHER_KEY}') OR repo_spellings && " + f"ARRAY['{KEY}', '{ALIAS}', '{OTHER_KEY}'];") + assert held == "0", f"{IMPORTED} holds {held} open take(s) on {KEY}, which every case here writes" + + # -------------------------------------------------- the calls, as the writer + def call(self, sql: str, **variables) -> subprocess.CompletedProcess: + argv = [self.db.psql_bin, "-X", "-q", "-A", "-t", "-v", "ON_ERROR_STOP=1", + "-v", "VERBOSITY=verbose", "-v", "SHOW_CONTEXT=never"] + for name, value in variables.items(): + argv += ["-v", f"{name}={value}"] + return subprocess.run(argv, input=sql, capture_output=True, text=True, timeout=60, + env=self.db.env(WRITER_LOGIN), check=False) + + def commit(self, entry: dict, *, base: Optional[int] = None, write_id: Optional[str] = None, + generation: Optional[str] = None) -> Tuple[subprocess.CompletedProcess, str]: + write_id = write_id or str(uuid.uuid4()) + proc = self.call(COMMIT_SQL, base=self.head() if base is None else base, wid=write_id, + client=self.client(), gen=generation or self.switch()["generation"], + entry=json.dumps(entry, ensure_ascii=False)) + return proc, write_id + + def committed(self, entry: dict, **options) -> int: + proc, _ = self.commit(entry, **options) + assert proc.returncode == 0, ( + f"commit_write as {WRITER_LOGIN} exited {proc.returncode}: {proc.stderr.strip()[-800:]}") + return int(proc.stdout.strip()) + + # -------------------------------------------------- entries + def journal(self, kind: str, verb: str, lane: str, **extra) -> dict: + row = {"kind": kind, "verb": verb, "lane": lane, "workstation": WS, "author": AUTHOR, + "message": f"{verb} by {lane} (T223)"} + row.update(extra) + return row + + def appended(self, path: str, texts: Sequence[str], *, create: bool = False) -> Tuple[list, list, int]: + """The edits and the files row of appending `texts` to `path`, and the + first appended line's number.""" + final_newline, lines = (None, []) if create else self.file(path) + edits = [] + if final_newline is None: + edits.append({"ord": 1, "op": "create", "path": path}) + final_newline = True + for text in texts: + edits.append({"ord": len(edits) + 1, "op": "append", "path": path, "new_text": text}) + files = [{"path": path, "present": True, "sha256": sha256(rendered([*lines, *texts], final_newline)), + "final_newline": final_newline}] + return edits, files, len(lines) + 1 + + def append(self, lane: str, line: str, row: dict, *, verb: str = "append-line") -> dict: + """A write entry: one line appended to `lanes/LANES.md`, and its row.""" + edits, files, line_no = self.appended(LANES_MD, [line]) + args = sha256(f"{lane}\0{verb}\0{line}".encode("utf-8")) + return {"journal": self.journal("write", verb, lane, args_sha256=args), + "not_after": self.not_after(), "edits": edits, "files": files, + "register_lines": [dict(row, path=LANES_MD, line_no=line_no, raw=line, workstation=WS)]} + + def write(self, lane: str, line_row: Tuple[str, dict]) -> int: + line, row = line_row + return self.committed(self.append(lane, line, row)) + + def to_primary(self, confirm: bool = True) -> int: + """A `cutover` entry over the template's import, then `record_marker`: + `primary`, the marker `present`. Returns the cutover's seq. With + `confirm` False, no `record_marker`: the cutover is left unconfirmed, + `primary` with the marker `adding`, which `switch_to_files` aborts.""" + sw = self.switch() + assert (sw["mode"], sw["marker_state"]) == ("files", "absent"), sw + head = self.head() + last_import = int(self.value(f"SELECT coalesce(max(seq), -2) FROM {REGISTER}.journal WHERE kind = 'import';")) + assert last_import == head >= 0, f"{IMPORTED}'s head {head} is not its import ({last_import})" + edits, files, _ = self.appended(STORE, ["primary"]) + seq = self.committed({"journal": self.journal("cutover", "cutover", OPERATOR), + "not_after": self.not_after(), "edits": edits, "files": files}) + if not confirm: + sw = self.switch() + assert (sw["mode"], sw["marker_state"], sw["marker_seq"]) == ("primary", "adding", seq), sw + return seq + marked =self.call(MARKER_SQL, client=self.client(), seq=seq, commit=MARKER_COMMIT, ws=WS) + assert (marked.returncode, marked.stdout.strip()) == (0, "present"), marked.stderr + sw = self.switch() + assert (sw["mode"], sw["marker_state"]) == ("primary", "present"), sw + return seq + + def refused(self, entry: dict, code: str, detail: Optional[str], *says: str) -> List[str]: + """The call ends with `code` and `detail`, and writes nothing.""" + before = self.state() + proc, write_id = self.commit(entry) + lines = proc.stderr.splitlines() + assert proc.returncode == 3, f"exit {proc.returncode}, not 3: {proc.stderr.strip()}" + assert lines and lines[0].startswith(f"ERROR: {code}: "), lines + if detail is not None: + assert lines[1:2] == [f"DETAIL: {detail}"], lines + for text in says: + assert text in lines[0], f"{text!r} is not in {lines[0]!r}" + assert self.state() == before, "the refused call changed the register" + assert self.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{write_id}';") == "0" + return lines + + +# ------------------------------------------------------------ the lines +def line(c: Clone, kind: Optional[str], lane: str, *, repo: str = KEY, key: Optional[str] = None, + spellings: Optional[List[str]] = None, pr: Optional[int] = PR, age: Optional[float] = 0, + stamp: Optional[str] = None, for_lane: Optional[str] = None, + slot: Optional[Tuple[Optional[str], Optional[str]]] = None, text: Optional[str] = None) -> Tuple[str, dict]: + """A LANES.md line of `kind` and its `register_lines` row. `age` is in + minutes by the database's clock; None, a line with no UTC, or with + `stamp`, a UTC that cannot be read.""" + utc, utc_ts = c.stamp(age) if age is not None else (stamp, None) + tail = f" — {utc}" if utc else "" + pr_part = f"PR #{pr} into {repo} main" if pr is not None else f"{repo} main" + if text is None: + if kind == "LANDING EXPIRED": + names = f" for lane {for_lane}" if for_lane else "" + text = f"{kind} — lane {lane} — {pr_part}{names}{tail}" + elif kind == "LANDED": + text = f"{kind} — lane {lane} — {pr_part} → 0123abc{tail}" + else: + text = f"{kind} — lane {lane} — {pr_part}{tail}" + slot_role, slot_kind = slot if slot is not None else SLOTS[kind] + row = {"kind": kind, "slot_role": slot_role, "slot_kind": slot_kind, "lane": lane, "repo": repo, + "repo_key": key or repo, "repo_spellings": spellings, "pr": pr, "tip": None, "utc": utc, + "utc_ts": utc_ts, "session": None, "for_lane": for_lane} + return text, row + + +def landing(c: Clone, lane: str = L1, **options) -> Tuple[str, dict]: + return line(c, "LANDING", lane, **options) + + +def hold(c: Clone, lane: str, **options) -> Tuple[str, dict]: + return line(c, "HOLD", lane, pr=None, **options) + + +def expired(c: Clone, lane: str = L2, for_lane: str = L1, **options) -> Tuple[str, dict]: + return line(c, "LANDING EXPIRED", lane, for_lane=for_lane, **options) + + +@pytest.fixture() +def clones(pg_cluster): + """Clones of `tmpl_imported`, or of the template a case names (the act + backstops clone `tmpl`; T252r), each the case's own, dropped at the end.""" + made: List[str] = [] + + def clone(template: str = IMPORTED) -> Clone: + found = pg_cluster.psql(f"SELECT datname FROM pg_database WHERE datname = '{template}';").stdout.split() + assert found == [template], f"no {template} on the session cluster: lane 2's T227 builds it" + name = f"t223_{next(_NAMES)}" + pg_cluster.psql(f"CREATE DATABASE {name} TEMPLATE {template};") + made.append(name) + pg_cluster.psql(f"REVOKE ALL ON DATABASE {name} FROM PUBLIC;\n" + f"GRANT CONNECT ON DATABASE {name} TO {CONNECT_TO};\n") + return Clone(pg_cluster, name) + + yield clone + for name in made: + pg_cluster.drop_database(name) + + +def primary(clones) -> Clone: + c = clones() + c.to_primary() + c.no_taker() + return c + + +# ------------------------------------------------------------ F13 +def test_a_landing_expired_for_a_stale_landing_commits_and_frees_main_in_main_slots(clones): + """[stale]: a stale LANDING still takes `main`, so L2's HOLD is refused + `main-held`; L2's LANDING EXPIRED for L1 commits at the next seq, frees + `main` in `main_slots` and holds `for_lane` L1; L2's HOLD then commits.""" + c = primary(clones) + c.write(L1, landing(c, age=40)) + hold_line, hold_row = hold(c, L2) + c.refused(c.append(L2, hold_line, hold_row), "LR005", "main-held", "(D18)") + + head = c.head() + seq = c.write(L2, expired(c)) + assert (seq, c.head()) == (head + 1, head + 1) + assert c.open_takes(L1, "landing") == [] + assert c.value(f"SELECT for_lane FROM {REGISTER}.register_lines WHERE seq = {seq};") == L1 + c.committed(c.append(L2, hold_line, hold_row)) + assert c.open_takes(L2, "hold") == [[KEY, None]] + + +def test_a_landing_expired_closes_only_the_holders_landings_for_that_pr_and_repository(clones): + """[closes-only-its-own]: the release for L1's PR closes both of that + PR's posts, as L1's LANDED would; L1's HOLD, its other PR's LANDING and + L3's LANDING on another repository stay open, and no LANDED is written.""" + c = primary(clones) + c.write(L1, hold(c, L1, age=50)) + c.write(L1, landing(c, age=50)) + c.write(L1, landing(c, age=40)) + c.write(L1, landing(c, pr=OTHER_PR, age=40)) + c.write(L3, landing(c, L3, repo=OTHER_KEY, age=40)) + head = c.head() + + c.write(L2, expired(c)) + assert c.open_takes(L1, "landing") == [[KEY, OTHER_PR]] + assert c.open_takes(L1, "hold") == [[KEY, None]] + assert c.open_takes(L3, "landing") == [[OTHER_KEY, PR]] + assert c.value(f"SELECT count(*) FROM {REGISTER}.register_lines WHERE kind = 'LANDED' AND seq > {head};") == "0" + + +def test_the_holder_may_write_its_own_landing_expired(clones): + """[holder-writes]: a close is matched by `coalesce(for_lane, lane)`, so + L1's own LANDING EXPIRED, `for_lane` L1, commits and frees `main`.""" + c = primary(clones) + c.write(L1, landing(c, age=40)) + seq = c.write(L1, expired(c, L1, for_lane=L1)) + assert seq == c.head() + assert c.open_takes(L1, "landing") == [] + + +def _imported_take(c: Clone) -> dict: + """Test 7's imported LANDING whose UTC cannot be read, still open.""" + takes = c.json(f"SELECT coalesce(json_agg(json_build_object('lane', t.lane, 'pr', t.pr, 'key', t.repo_key, " + f"'spellings', t.repo_spellings, 'line_id', t.line_id) ORDER BY t.line_id), '[]') " + f"FROM {REGISTER}.register_lines t JOIN {REGISTER}.main_slots m ON m.line_id = t.line_id " + f"WHERE t.slot_role = 'take' AND t.slot_kind = 'landing' AND t.utc_ts IS NULL;") + assert takes, f"{IMPORTED} holds no open LANDING whose UTC cannot be read (test 7's register; T227)" + take = takes[-1] + newer = c.value(f"SELECT count(*) FROM {REGISTER}.register_lines WHERE slot_role = 'take' " + f"AND slot_kind = 'landing' AND lower(lane) = lower('{take['lane']}') AND pr = {take['pr']} " + f"AND line_id > {take['line_id']};") + assert newer == "0", f"a newer LANDING follows the imported one: {take}" + return take + + +def _setup(c: Clone, case: str) -> Tuple[str, dict, str, Tuple[str, ...]]: + """Each case's lines, then the LANDING EXPIRED it sends, its token and + what its message says.""" + young = ("is too young (F13)", "its age by the database's clock is ") + if case == "fresh": + c.write(L1, landing(c, age=0)) + return (*expired(c), "expired-young", young) + if case == "posted-again": + c.write(L1, landing(c, age=40)) + c.write(L1, landing(c, age=10)) + return (*expired(c), "expired-young", young) + if case in ("posted-again-two-spellings", "newest-keyed-another-way"): + c.write(L1, landing(c, key=KEY, spellings=BOTH, age=40)) + c.write(L1, landing(c, repo=ALIAS, key=ALIAS, spellings=BOTH, age=10)) + if case == "posted-again-two-spellings": + return (*expired(c, repo=ALIAS, key=ALIAS, spellings=BOTH), "expired-young", young) + return (*expired(c, key=KEY, spellings=BOTH), "expired-none", + (f"no open LANDING by {L1} for PR #{PR} under repo_key {KEY} (F13)", + f"its newest is keyed {ALIAS}")) + if case == "unreadable-utc": + take = _imported_take(c) + text, row = expired(c, for_lane=take["lane"], pr=take["pr"], repo=take["key"], key=take["key"], + spellings=take["spellings"]) + return text, row, "expired-young", ("is too young (F13)", + "is unknown, since its UTC cannot be read") + if case == "wrong-lane": + c.write(L1, landing(c, age=40)) + return (*expired(c, for_lane=L3), "expired-none", + (f"no open LANDING by {L3} for PR #{PR} under repo_key {KEY} (F13)",)) + if case == "wrong-pr": + c.write(L1, landing(c, age=40)) + return (*expired(c, pr=OTHER_PR), "expired-none", + (f"no open LANDING by {L1} for PR #{OTHER_PR} under repo_key {KEY} (F13)",)) + if case == "other-spelling": + c.write(L1, landing(c, key=KEY, spellings=BOTH, age=40)) + return (*expired(c, repo=ALIAS, key=ALIAS, spellings=BOTH), "expired-none", + (f"under repo_key {ALIAS} (F13)", f"its newest is keyed {KEY}")) + if case == "already-closed": + c.write(L1, landing(c, age=40)) + c.write(L1, line(c, "LANDED", L1, age=20)) + return (*expired(c), "expired-closed", (f"is already closed, by a LANDED of {L1} (F13)",)) + if case == "closed-under-another-spelling": + c.write(L1, landing(c, key=KEY, spellings=BOTH, age=40)) + c.write(L1, line(c, "LANDED", L1, repo=ALIAS, key=ALIAS, spellings=BOTH, age=20)) + return (*expired(c, key=KEY, spellings=BOTH), "expired-closed", + (f"is already closed, by a LANDED of {L1} (F13)",)) + if case == "already-released": + c.write(L1, landing(c, age=40)) + c.write(L2, expired(c)) + return (*expired(c), "expired-closed", (f"is already closed, by a LANDING EXPIRED of {L2} (F13)",)) + raise AssertionError(case) + + +@pytest.mark.parametrize("case", [ + "fresh", "posted-again", "posted-again-two-spellings", "unreadable-utc", + "wrong-lane", "wrong-pr", "other-spelling", "newest-keyed-another-way", + "already-closed", "closed-under-another-spelling", "already-released", +]) +def test_a_landing_expired_that_names_no_stale_landing_is_lr005_and_writes_nothing(clones, case): + """Each of F13's eleven: LR005, its reason's token (`expired-young`, + `expired-none`, `expired-closed`) and nothing written.""" + c = primary(clones) + text, row, token, says = _setup(c, case) + lines = c.refused(c.append(L2, text, row), "LR005", token, *says) + if case == "fresh": + assert "unknown" not in lines[0], lines[0] + + +# ------------------------------------------------------------ U3 +@pytest.mark.parametrize("case", ["hold", "landing", "bad-stamp", "behind-d18"]) +def test_on_primary_a_take_with_no_readable_utc_is_lr005_take_no_utc_and_writes_nothing(clones, case): + """On primary a take whose UTC cannot be read is LR005 `take-no-utc`, + the message quoting its line; behind another lane's HOLD it is still + `take-no-utc`, not `main-held`, since U3's test comes first.""" + c = primary(clones) + if case == "behind-d18": + c.write(L1, hold(c, L1, age=0)) + if case in ("hold", "behind-d18"): + text, row = hold(c, L2, age=None) + kind = "HOLD" + else: + text, row = landing(c, L2, age=None, stamp=BAD_STAMP if case == "bad-stamp" else None) + kind = "LANDING" + if case == "bad-stamp": + assert BAD_STAMP in text and row["utc_ts"] is None + c.refused(c.append(L2, text, row), "LR005", "take-no-utc", + f"a {kind} take with no readable UTC (U3): {text}") + + +@pytest.mark.parametrize("case", ["hold-refresh", "no-kind", "import-at-files"]) +def test_a_refresh_or_a_line_of_no_kind_with_no_utc_commits_and_a_take_with_none_loads_at_files(clones, case): + """A `HOLD (refresh)` with no UTC takes nothing and a line of no kind is + no take, so each commits on primary; at `files`, before the clone's + cutover, an `import` carrying the takes with no UTC commits, each line + loaded as it stands, since U3 binds only a write on primary.""" + if case != "import-at-files": + c = primary(clones) + kind = "HOLD (refresh)" if case == "hold-refresh" else None + text = f"HOLD (refresh) — lane {L2} — {KEY} main" if kind else f"HOLD REQUESTED — lane {L2} — {KEY} main" + text, row = line(c, kind, L2, pr=None, age=None, text=text) + seq = c.write(L2, (text, row)) + said = c.json(f"SELECT json_build_array(kind, slot_role, utc_ts, mode) FROM {REGISTER}.register_lines " + f"WHERE seq = {seq};") + assert said == [kind, None, None, "primary"], said + return + + c = clones() + sw = c.switch() + assert (sw["mode"], sw["marker_state"]) == ("files", "absent"), sw + takes = [hold(c, L2, age=None), landing(c, L2, age=None), landing(c, L3, age=None, stamp=BAD_STAMP)] + edits, files, _ = c.appended(LANES_MD, [t for t, _ in takes], create=True) + rows = [dict(row, path=LANES_MD, line_no=n, raw=text, workstation=WS) + for n, (text, row) in enumerate(takes, start=1)] + seq = c.committed({"journal": c.journal("import", "import", OPERATOR, git_commit=GIT_COMMIT, git_tree=GIT_TREE), + "not_after": c.not_after(), "edits": edits, "files": files, "register_lines": rows}) + said = c.json(f"SELECT json_agg(json_build_array(kind, slot_role, utc, utc_ts, mode, raw) ORDER BY line_id) " + f"FROM {REGISTER}.register_lines WHERE seq = {seq};") + assert said == [[r["kind"], "take", r["utc"], None, "files", r["raw"]] for r in rows], said + + +# ------------------------------------------------------------ the table's checks +@pytest.mark.parametrize("case", ["for-lane-on-a-hold", "no-for-lane", "closes-a-hold"]) +def test_the_for_lane_checks_refuse_a_for_lane_off_a_landing_expired_and_a_landing_expired_that_closes_no_landing( + clones, case): + """`register_lines`' CHECKs: `for_lane` on a line of another kind, a + LANDING EXPIRED with none, and one that closes a HOLD are each 23514 at + the insert, and the whole entry rolls back.""" + c = primary(clones) + if case == "for-lane-on-a-hold": + text, row = hold(c, L2) + row["for_lane"] = L1 + elif case == "no-for-lane": + text, row = expired(c, for_lane=None) + row["for_lane"] = None + else: + c.write(L1, hold(c, L1, age=0)) + text, row = expired(c, pr=None, slot=("close", "hold")) + c.refused(c.append(L2, text, row), "23514", None, + 'new row for relation "register_lines" violates check constraint') + + +# ============================================================ PHASE W: test 4, C10, D19 and 4.1 (T243) +# +# THE CLIENT'S WRITERS ON A REAL REGISTER (AT-4's fixture, TP:876-898). Each +# case's register is a clone of `tmpl` (T227), brought by AT-LR006's route, as +# T222f's cases bring theirs: an `import` entry at seq 0 holding the case's +# files (`Clone.import_files`), then `Clone.to_primary`'s `cutover` entry at +# seq 1 and `record_marker`, so the first write is seq 2 and the objects are +# free. The workstation (`Estate`): +# * one configuration at `mode=primary`, naming the clone through T228's +# `lanes_w_fx`, and one `XDG_STATE_HOME`, so every writer shares one +# `lanes-index` state directory and one image cache, `register-images/`; +# * T203's flipped copy of `lanes-index` beside `lanes-edit.sh`, +# `repos.tsv` and `lane-start`; +# * a bare origin at the register's files with `lanes/repos.tsv` beside +# them, and one checkout per writer (a reading: AT-4 names one +# configuration, and the writers' git fetches are kept off one index); +# * beside each writer's checkout W, the own register checkout `` of +# each lane its write is made for, `-lanes/`, a clone of W's +# origin made with W (lane 3's (119); `write_lanes_of`, below); +# * a fake `gh` that logs each call with its body (AT-D8's form); +# * the test's own `PATH` behind it, every directory kept in its place, +# with `psql`, `lanes-index` and every `pg_*` masked out (`_masked_path`); +# * per writer, a `psql` directory of its own: a stamp wrapper, which logs +# each call's start time, in front of T203's shim (section 3.2), so each +# writer's arms, log and counters are its own; +# * the start barrier: each writer is launched through a launcher that blocks +# on a FIFO the test opens only once all N launchers wait (TP:886-888). +# The window W is the shim's `window` arm on `commit_write`, the attempt's +# second call (TP:889-893). Lines are written by the client's own verbs. +# +# They FAIL UNTIL lane 1's T107 (L1-A6), lane 4's attempt loop on primary +# (L4-B3) and `claim`'s register seq (L4-B8), lane 1's T302 (the image cache), +# and lane 2's T227 (`tmpl`) and T228 (`lanes_w_fx`) land. Without PostgreSQL +# 16 each skips. + +EM, ARROW, DOT = chr(0x2014), chr(0x2192), chr(0x00B7) +W_TEMPLATE = "tmpl" +W_WS = "Eagle" +REPOS_PATH = "lanes/repos.tsv" +#: Test 4's lanes: ten burst writers, and the pairs of C10, D19 and (a)-(h). +BURST = [f"repoW{k:02d}-1" for k in range(1, 11)] +LA, LB, LC = "repoA-1", "repoB-1", "repoC-1" +W_LANES = [LA, LB, LC, *BURST] +#: Object O, and X: X's repository becomes an alias of O's at T1 (C10, D19). +ALIAS_REPO = "oldname/r9" +OBJ = f"{KEY}#41" +OBJ_X = f"{ALIAS_REPO}#41" +REPOS_T0 = f"# alias\towner/repo\n{ALIAS}\t{KEY}\n" +REPOS_T1 = REPOS_T0 + f"{ALIAS_REPO}\t{KEY}\n" +#: The busy text's words (D7; P231-233) and the default's (AT-4 (h)). +BUSY_WORDS = ("register busy", "nothing", "retry") +D7_S, TWICE_D7_S = 30, 60 +#: AT-4 (c)'s measuring burst (T243u): a deadline none of its writers is +#: meant to reach, four times D7's and inside `Writer.wait`'s 150 s, so all +#: ten stay in the load until each commits, which the case checks by their +#: exit codes before it takes the p95 (T243w); and the factor of the runner's +#: own D7, `max(30 s, 10 x the runner's attempt p95)` (Y:5906; tasks:2337 at +#: `8e8d1b49`). +MEASURE_DEADLINE_S, D7_PER_P95 = 120, 10 +UUID_RE = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" +SEQ_SAID = re.compile(r"register seq ([0-9]+)") + + +def session_of(lane: str) -> str: + k = int(hashlib.sha256(lane.encode()).hexdigest()[:8], 16) + return f"{k:08x}-0243-4000-8000-{k:012x}" + + +def w_row(lane: str) -> str: + return (f"| `{lane}` | harness `{session_of(lane)}` | {W_WS} / test / brett | 2026-05-01T00:00Z " + f"| none | handoffs/{lane}.md | LIVE {DOT} 2026-05-01T00:00:00Z {DOT} working |") + + +def bound_line(lane: str) -> str: + """A `STARTED` line binding `lane` on this workstation, as `lane-start` + writes one with Amendment 18(a)'s sub-fields: host `eagle` (the estate's + `LANES_HOST`), no container, and a window with its `@id`. `request-handoff` + asks only a bound lane: one whose last lane-kind line is none at all is + FREE, exit 8 (`lanes-edit.sh`'s `request-handoff` arm; T243g).""" + k = W_LANES.index(lane) + 1 if lane in W_LANES else 99 + return (f"STARTED {EM} lane {lane}, session {session_of(lane)}@{W_WS}, 2026-05-01T00:00:00Z, " + f"lane:{lane} {ARROW} home {KEY}; dir /work/{lane}; profile team-01a; window t243:{k} @{k}; " + f"host eagle; container none; os linux\n") + + +def estate_files(lanes: Sequence[str], bound: Sequence[str] = ()) -> Dict[str, str]: + """The register's render set: a row per lane, an empty event list, the + aliases table and a log per lane; each lane of `bound` is bound, its log + holding `bound_line` (T243g).""" + table = "\n".join([f"# LANES.md {EM} test 4's register (T243)", "", + "| lane | session id | workstation / env / user | started (UTC) " + "| objects owned | handoff path | state |", + "|---|---|---|---|---|---|---|", *(w_row(lane) for lane in lanes), ""]) + files = {LANES_MD: table, "lanes/aliases.tsv": "# old\tnew\tutc\n", + "lanes/log/README.md": "# the object logs\n"} + for lane in lanes: + files[f"lanes/log/{lane}.md"] = (f"# lane {lane} {EM} object log (Amendment 7)\n" + + (bound_line(lane) if lane in bound else "")) + return files + + +def import_files(c: "Clone", files: Dict[str, str]) -> int: + """AT-LR006's first step on a clone of `tmpl` (T222f's `Clone.imported`): + one `import` entry creating `files`, each with its last LF.""" + sw = c.switch() + assert (sw["mode"], sw["marker_state"], c.head()) == ("files", "absent", -1), ( + f"a clone of {W_TEMPLATE} is not at files, marker absent, with an empty journal: {sw}, head {c.head()}") + edits, rows = [], [] + for path in sorted(p for p in files if p != "lanes/log/README.md"): + lines = files[path].rstrip("\n").split("\n") + edits.append({"ord": len(edits) + 1, "op": "create", "path": path}) + edits += [{"ord": len(edits) + 1 + k, "op": "append", "path": path, "new_text": text} + for k, text in enumerate(lines)] + rows.append({"path": path, "present": True, "final_newline": True, + "sha256": sha256(rendered(lines, True))}) + return c.committed({"journal": c.journal("import", "import", OPERATOR, git_commit=GIT_COMMIT, + git_tree=GIT_TREE), + "not_after": c.not_after(), "edits": edits, "files": rows}) + + +def run_git(cwd, *args: str, env: dict) -> str: + proc = subprocess.run(["git", *args], cwd=str(cwd), capture_output=True, text=True, env=env, + check=False) + assert proc.returncode == 0, f"git {' '.join(args)} in {cwd}: {proc.stderr}{proc.stdout}" + return proc.stdout + + +#: `_masked_path`'s names: those the filters dropped a directory for holding. +#: Every name that starts `pg_` is masked as well. +PATH_MASKED = ("psql", "lanes-index") +_MIRRORS: Dict[Tuple[str, Tuple[str, ...]], str] = {} +_MIRROR_ROOT: List[str] = [] +_MIRRORS_LOCK = threading.Lock() + + +def _masked_path(path: str, spared: Optional[Dict[str, Sequence[str]]] = None) -> str: + """`path` with every directory kept, in its place, and each one that + holds `psql`, `lanes-index` or a `pg_*` name swapped for a mirror of it + without them: a directory of links, one per other entry, each to the + entry's absolute path. A directory that holds none of them is kept as it + is, and an empty entry is left out, as before. + + Why not drop the directory: on a GitHub runner, `postgresql-16`'s client + package puts `psql` and its `pg_*` wrappers in `/usr/bin`, beside `git` + and `bash`, and `/bin` is a link to `/usr/bin`. Dropping each directory + that held a `psql` dropped `git` and `bash` with it, and the case's own + `subprocess.run` failed (lane 1's census of round 4, #165 6807454856). + + `spared` maps a directory, as `path` names it, to the masked names it + keeps: a call site's own exception. Each mirror is built once per + process, keyed by the directory's real path and the names it masks, + under one `l2-pathmask-` temporary directory that an `atexit` handler + removes.""" + kept = [] + for where in path.split(os.pathsep): + if not where: + continue + masked = tuple(sorted(set(PATH_MASKED) - set((spared or {}).get(where, ())))) + try: + names = os.listdir(where) + except OSError: + names = [] + if any(name in masked or name.startswith("pg_") for name in names): + where = _path_mirror(os.path.realpath(where), masked) + kept.append(where) + return os.pathsep.join(kept) + + +def _path_mirror(real: str, masked: Tuple[str, ...]) -> str: + """The mirror of the directory `real` less `masked` and every `pg_*` + name, made on the first call for that pair.""" + with _MIRRORS_LOCK: + if (real, masked) not in _MIRRORS: + if not _MIRROR_ROOT: + _MIRROR_ROOT.append(tempfile.mkdtemp(prefix="l2-pathmask-")) + atexit.register(shutil.rmtree, _MIRROR_ROOT[0], ignore_errors=True) + mirror = os.path.join(_MIRROR_ROOT[0], str(len(_MIRRORS) + 1)) + os.mkdir(mirror) + for name in sorted(os.listdir(real)): + if name not in masked and not name.startswith("pg_"): + os.symlink(os.path.join(real, name), os.path.join(mirror, name)) + _MIRRORS[(real, masked)] = mirror + return _MIRRORS[(real, masked)] + + +_STAMP = r'''#!/usr/bin/env python3 +# T243's stamp: each psql call's start time and argv, then T203's shim. +# Where `tee` is in this directory (a burst's writer, T243m), a forked child +# also passes the call's stderr through, and at its end logs to `tee.log`, +# keyed by the start time, the SQLSTATE of each `ERROR:` line (the register's +# VERBOSITY=verbose) and the time the call's stderr closed. The shim is still +# this process, so its pid and its exit are the call's, as before. +import json, os, re, sys, time +HERE = os.path.dirname(os.path.abspath(__file__)) +start = time.time() +with open(os.path.join(HERE, "stamps.log"), "a", encoding="utf-8") as log: + log.write(json.dumps({"t": start, "argv": sys.argv[1:]}) + "\n") +if sys.argv[1:] != ["--version"] and os.path.exists(os.path.join(HERE, "tee")): + rd, wr = os.pipe() + if os.fork() == 0: + os.close(wr) + os.close(0) + os.close(1) + seen, out = b"", True + while True: + chunk = os.read(rd, 65536) + if not chunk: + break + seen += chunk + view = memoryview(chunk) + while out and view: + try: + view = view[os.write(2, view):] + except OSError: + out = False + states = re.findall(r"^ERROR:\s+([0-9A-Z]{5}):", seen.decode("utf-8", "replace"), re.M) + with open(os.path.join(HERE, "tee.log"), "a", encoding="utf-8") as log: + log.write(json.dumps({"t": start, "end": time.time(), "states": states}) + "\n") + os._exit(0) + os.close(rd) + os.dup2(wr, 2) + os.close(wr) +shim = os.path.join(HERE, "shim", "psql") +os.execv(shim, [shim] + sys.argv[1:]) +''' + +_LAUNCHER = r'''#!/usr/bin/env python3 +# T243's start barrier: wait on the FIFO, then become the writer. +import os, sys +ready, fifo, argv = sys.argv[1], sys.argv[2], sys.argv[3:] +open(ready, "w").close() +fd = os.open(fifo, os.O_RDONLY) +open(ready + ".go", "w").close() +os.close(fd) +os.execvp(argv[0], argv) +''' + +_GH = r'''#!/usr/bin/env python3 +# AT-D8's fake gh (T243): each call logged with its body; nothing answered. +import json, os, sys +HERE = os.path.dirname(os.path.abspath(__file__)) +body = sys.stdin.read() if not sys.stdin.isatty() else "" +with open(os.path.join(HERE, "gh.log"), "a", encoding="utf-8") as log: + log.write(json.dumps({"argv": sys.argv[1:], "stdin": body}) + "\n") +''' + + +class Barrier: + """TP:886-888: N launchers wait on one FIFO, released together.""" + + def __init__(self, root: Path, count: int) -> None: + self.fifo, self.ready, self.count = root / "start.fifo", root / "ready", count + os.mkfifo(str(self.fifo)) + self.ready.mkdir() + + def _seen(self, suffix: str) -> int: + return sum(1 for p in self.ready.iterdir() if p.name.endswith(".go") == (suffix == ".go")) + + def release(self) -> float: + deadline = time.monotonic() + 60 + while self._seen("") < self.count: + assert time.monotonic() < deadline, f"only {self._seen('')} of {self.count} launchers wait" + time.sleep(0.05) + fd = os.open(str(self.fifo), os.O_WRONLY) + released = time.monotonic() + while self._seen(".go") < self.count: + assert time.monotonic() < deadline + 30, f"only {self._seen('.go')} launchers passed" + time.sleep(0.01) + os.close(fd) + return released + + +class Writer: + """One `lanes-edit.sh` run: its psql directory, and its outcome.""" + + def __init__(self, name: str, lane: str, proc: subprocess.Popen, psql_dir: Path) -> None: + self.name, self.lane, self.proc, self.psql_dir = name, lane, proc, psql_dir + self.started, self.ended = time.monotonic(), None + self.out = self.err = "" + self._thread = threading.Thread(target=self._wait, daemon=True) + self._thread.start() + + def _wait(self) -> None: + self.out, self.err = self.proc.communicate() + self.ended = time.monotonic() + + def wait(self, timeout: float = 150) -> int: + self._thread.join(timeout) + assert not self._thread.is_alive(), f"{self.name} ({self.lane}) did not end in {timeout} s" + return self.proc.returncode + + @property + def code(self) -> int: + return self.proc.returncode + + def _jsonl(self, name: str) -> List[dict]: + """`name`'s lines, each a JSON object. A last line with no newline yet + is one still being written, as by a detached render's call, which + logs under a lock this read does not take: it is left out, never half + read (lane 2's RV20, NIT 19; T243s).""" + path = self.psql_dir / name + if not path.exists(): + return [] + data = path.read_bytes() + data = data[:data.rfind(b"\n") + 1] + return [json.loads(line) for line in data.decode("utf-8").splitlines() if line] + + def calls(self) -> List[dict]: + """The shim's calls that carry a request, in order.""" + return [e for e in self._jsonl("shim/" + PSQL_SHIM_LOG) if e.get("kind") != "version"] + + def commit_calls(self) -> List[dict]: + return [e for e in self.calls() if "commit_write" in (e.get("stdin") or "")] + + def snapshot_calls(self) -> List[dict]: + return [e for e in self.calls() if "commit_write" not in (e.get("stdin") or "") + and "find_write" not in (e.get("stdin") or "")] + + def ends(self) -> Dict[int, dict]: + return {e["call"]: e for e in self._jsonl("shim/" + PSQL_SHIM_ENDS) if "call" in e} + + def stamps(self) -> List[dict]: + """Each request's start time, paired in order with the shim's log.""" + times = [s["t"] for s in self._jsonl("stamps.log") if s["argv"] != ["--version"]] + return [dict(call, t=t) for call, t in zip(self.calls(), times)] + + def write_ids(self) -> List[str]: + """Each commit call's write id, `commit_write`'s second argument.""" + found = [] + for call in self.commit_calls(): + m = re.search(r"commit_write\(\s*-?[0-9]+\s*,\s*'(" + UUID_RE + ")'", call.get("stdin") or "") + assert m, f"{self.name}'s commit call carries no write id: {(call.get('stdin') or '')[:300]}" + found.append(m.group(1)) + return found + + def seq_said(self) -> Optional[int]: + """The seq `claim` names in its line, `lane now holds + (register seq )`, which it prints by `note`, on stderr, where files + mode printed the log commit's sha (T421; Y@a6497d00:266; T243h).""" + m = SEQ_SAID.search(self.err) + return int(m.group(1)) if m else None + + +# EACH WRITE LANE'S OWN REGISTER CHECKOUT, `` (lane 3, #165 6813027223, +# batch g Q119; T241e's `WRITE_LANES` in `test_sql_register_writes.py` is the +# model). On primary a write reads its author, its pending hand edits and its +# `repos.tsv` from the checkout of the lane it is made for, `-lanes/` +# (`lanes-edit.sh`'s `lane_checkout_path`, `R-merge20:lanes-edit.sh:18342`), +# W being the real path of `workspace.yaml`'s `path:`. Where that is not there +# the write refuses with 2, or 64 for `request-handoff`, sending nothing, since +# a clone there would be D12's fetch (B15 tasks:1154; T415's `pw_checkout`). +# So the estates make it themselves, because `lane-start` makes it only under +# `LANES_ROUTE_WRITES=1` with neither `LANES_REPO` nor `LANES_FILE` set +# (lane 3, #165 6841265575; `dfc4ce4c:lane-start:1209-1210`): beside each +# writer's W, a clone of W's own origin, for each lane `write_lanes_of` names. +# A W kept behind a push (`mirror_at_t0`: AT-C10, AT-D19) keeps `` behind +# it too, at the same commit, and a W cloned before a push has `` cloned +# with it. Until T415 nothing reads ``: these estates set no +# `LANES_ROUTE_WRITES`, so each write goes through `LANES_REPO` as before. +def write_lanes_of(argv: Sequence[str], lane: Optional[str]) -> List[str]: + """The lanes a run of `lanes-edit.sh ` writes for, `lane` being its + `LANES_LANE`: `write_lane_of`'s answer (`R-merge20:lanes-edit.sh:18553`), + as T415's `pw_checkout` takes it, so a dry run's lane is included. Empty + for a run that is no write (a read, `act-begin`), and for an + `append-line` with no `LANES_LANE`, whose lane is in its text (every run + here sets `LANES_LANE`). `rename-lane` gives both names, the old one + first: `canon_lane` reads the old as the new once the rename is known. + `start-record ...` names its lane first, as `write-handoff` does + (T427's arm, `f9e1449c:lanes-edit.sh:27941-27945`); the arm's own + refusals, a `LANES_LANE` that is not ``, a `LANES_SESSION` that is no + transcript uuid and an `--add-row` with no row, come before any clone, + so a checkout made for them changes nothing (lane 3, #165 6859307928; + T243t).""" + verb, args = (argv[0], [str(a) for a in argv[1:]]) if argv else ("", []) + first = args[0] if args else "" + if verb in ("set-row-state", "replace-in-row", "append-session-id", "request-handoff"): + return [first] if first else [] + if verb in ("write-handoff", "start-record"): + return [first] if first and not first.startswith("-") else [] + if verb == "rename-lane": + names, words = [], iter(args) + for word in words: + if word == "--why": + next(words, None) + elif not word.startswith("-"): + names.append(word) + return names[:2] + if verb == "add-row": + m = re.match(r"[^`]*`([^`]*)`", first) + return [m.group(1)] if m and m.group(1) else [] + if verb == "retire-rows": + for k, word in enumerate(args): + if word == "--writer" and k + 1 < len(args): + return [args[k + 1]] + if word.startswith("--writer="): + return [word.split("=", 1)[1]] + if verb in ("append-line", "commit", "log", "claim", "release", "retire-rows", "archive-rows", + "migrate-state-cells"): + return [lane] if lane else [] + return [] + + +class Estate: + """Test 4's workstation over one clone at `primary` (see the section's + head).""" + + def __init__(self, root: Path, c: Clone, lanes: Sequence[str] = W_LANES, + repos: str = REPOS_T0, aliases: str = "", confirmed: bool = True, + bound: Sequence[str] = (LC,)) -> None: + self.root, self.c = root, c + # LANE C IS BOUND (T243g; lane 4's triage, #165 6833813092): every + # `request-handoff` here asks it, and a FREE lane is refused with 8 + # before any write. The other lanes keep logs with no lane-kind line. + self.files = estate_files(lanes, bound) + self.files["lanes/aliases.tsv"] += aliases + self.lanes = list(lanes) + import_files(c, self.files) + # `confirmed` False leaves the cutover unconfirmed, the marker `adding` (T233's [switch-at-files]). + assert c.to_primary(confirmed) == 1, "test 4's cutover is not at seq 1 (AT-4's fixture)" + home = root / "home" + (home / ".agents").mkdir(parents=True) + env = {k: v for k, v in os.environ.items() + if not k.startswith(("GIT_", "PG", "LANES_", "TMUX", "RULE6_", "FAKE_PSQL", "CLAUDE_", + "PROJECTS_ROOT", "XDG_"))} + env.update(HOME=str(home), GIT_CONFIG_NOSYSTEM="1", GIT_CONFIG_GLOBAL=os.devnull, + GIT_AUTHOR_NAME="T243 estate", GIT_AUTHOR_EMAIL="t243@example.invalid", + GIT_COMMITTER_NAME="T243 estate", GIT_COMMITTER_EMAIL="t243@example.invalid", + LC_ALL="C") + self.env = env + self.origin = root / "origin.git" + run_git(root, "init", "-q", "--bare", "-b", "main", str(self.origin), env=env) + seed = root / "seed" + run_git(root, "clone", "-q", str(self.origin), str(seed), env=env) + for path, text in {**self.files, REPOS_PATH: repos}.items(): + (seed / path).parent.mkdir(parents=True, exist_ok=True) + (seed / path).write_bytes(text.encode("utf-8")) + run_git(seed, "add", "-A", env=env) + run_git(seed, "commit", "-q", "-m", "T0: test 4's register", env=env) + run_git(seed, "push", "-q", "origin", "main", env=env) + self.seed = seed + self.bindir = root / "bin" + flipped_copy(LANES_INDEX, self.bindir, True) + for name in ("lanes-edit.sh", "repos.tsv", "lane-start"): + shutil.copy2(str(REPO / name), str(self.bindir / name)) + self.lanes_edit = self.bindir / "lanes-edit.sh" + conf = root / "lanes-index.conf" + conf.write_text(f"mode=primary\nregister_url={c.db.url(WRITER_LOGIN)}\n" + f"reader_url={c.db.url()}\n", encoding="utf-8") + conf.chmod(0o600) + self.gh_dir = root / "gh" + self.gh_dir.mkdir() + (self.gh_dir / "gh").write_text(_GH, encoding="utf-8") + (self.gh_dir / "gh").chmod(0o755) + (self.gh_dir / "tmux").write_text("#!/bin/sh\nexit 1\n", encoding="utf-8") + (self.gh_dir / "tmux").chmod(0o755) + self.launcher = root / "launch.py" + self.launcher.write_text(_LAUNCHER, encoding="utf-8") + self.state = root / "state" + for d in ("state", "config", "tmp"): + (root / d).mkdir(exist_ok=True) + path = _masked_path(env.get("PATH", "")) + env.update(TMPDIR=str(root / "tmp"), XDG_STATE_HOME=str(self.state), + XDG_CONFIG_HOME=str(root / "config"), CLAUDE_CONFIG_DIR=str(home / ".claude"), + LANES_WORKSTATION=W_WS, LANES_HOST="eagle", LANES_OS="linux", + LANES_CONTAINER="none", LANES_IN_CONTAINER="0", LANES_INDEX_CONFIG=str(conf), + PATH=str(self.gh_dir) + os.pathsep + path) + self._n = itertools.count(1) + self.writers: List[Writer] = [] + + # -------------------------------------------------- the workstation's parts + def checkout(self, name: str, origin: Optional[Path] = None, + lanes: Sequence[str] = ()) -> Tuple[Path, Path]: + """A clone of the origin, and the protocol root naming it; and, made + with it, each of `lanes`' own register checkouts beside it + (`lane_checkout`).""" + where = self.root / f"co-{name}" + run_git(self.root, "clone", "-q", str(origin or self.origin), str(where), env=self.env) + for lane in dict.fromkeys(lanes): + self.lane_checkout(where, lane, origin) + agents = self.root / f"agents-{name}" + agents.mkdir() + (agents / "workspace.yaml").write_text(f"repository: {origin or self.origin}\npath: {where}\n", + encoding="utf-8") + return where, agents + + def canon(self, lane: str) -> str: + """`lane` as `canon_lane` answers it over this estate's rows and alias + lines: a row wins at every hop, the last alias line for a name wins, + names compare in lower case, a cycle answers the typed name, and a + walk that meets no row answers its last hop. The rows are those the + estate was made with.""" + rows = {name.lower() for name in self.lanes} + hops: Dict[str, str] = {} + for line in self.files["lanes/aliases.tsv"].splitlines(): + parts = line.split("\t") + if line.startswith("#") or len(parts) < 2: + continue + hops[parts[0].lower()] = parts[1] + here, seen = lane, {lane.lower()} + while here.lower() not in rows and here.lower() in hops: + here = hops[here.lower()] + if here.lower() in seen: + return lane + seen.add(here.lower()) + return here + + def write_lanes(self, argv: Sequence[str], lane: Optional[str]) -> List[str]: + """`write_lanes_of`'s lanes, each followed by its `canon` name: T415's + `pw_checkout` runs `canon_lane` first and takes `-lanes/`, + so a write for a name with no row of its own, such as `add-row` for a + name on an alias walk, needs its canonical lane's `` (lane 4's + triage, #165 6833813092, TRIAGE-W13b o9; T243j). The typed name's + `` is kept too.""" + return list(dict.fromkeys(name for typed in write_lanes_of(argv, lane) + for name in (typed, self.canon(typed)))) + + def lane_checkout(self, where: Path, lane: str, origin: Optional[Path] = None) -> Path: + """``, lane `lane`'s own register checkout beside the checkout + `where`, which `workspace.yaml` names as W: `-lanes/`, W + being `where`'s real path, as `lane_checkout_path` puts it + (Amendment 23 S1). A clone of W's own origin, as `lane-checkout + --create` makes one, so it is at W's commit (lane 3's (119); + see `write_lanes_of`).""" + real = Path(os.path.realpath(where)) + target = real.parent / f"{real.name}-lanes" / lane + target.parent.mkdir(parents=True, exist_ok=True) + run_git(target.parent, "clone", "-q", str(origin or self.origin), str(target), env=self.env) + return target + + def psql_dir(self, name: str, arms: Sequence[dict] = ()) -> Path: + where = self.root / f"psql-{name}" + (where / "shim").mkdir(parents=True) + (where / "psql").write_text(_STAMP, encoding="utf-8") + (where / "psql").chmod(0o755) + (where / "shim" / "psql").write_text(PSQL_SHIM, encoding="utf-8") + (where / "shim" / "psql").chmod(0o755) + (where / "shim" / PSQL_SHIM_CONTROL).write_text( + json.dumps({"psql": self.c.db.psql_bin, "arms": list(arms)}), encoding="utf-8") + return where + + def push_alias(self) -> None: + """T1: a person's commit adding X's repository as an alias of O's, + pushed by a third clone (AT-C10's fixture).""" + (self.seed / REPOS_PATH).write_text(REPOS_T1, encoding="utf-8") + run_git(self.seed, "commit", "-q", "-am", "T1: alias oldname/r9 for opensoft/r9", env=self.env) + run_git(self.seed, "push", "-q", "origin", "main", env=self.env) + + def mirror_at_t0(self) -> Path: + """A second origin frozen at T0, for a clone kept behind the alias.""" + frozen = self.root / "origin-t0.git" + run_git(self.root, "clone", "-q", "--bare", str(self.origin), str(frozen), env=self.env) + return frozen + + # -------------------------------------------------- running writers + def launch(self, lane: str, argv: Sequence[str], arms: Sequence[dict] = (), extra: dict = None, + barrier: Optional[Barrier] = None, origin: Optional[Path] = None, + first: Sequence[Path] = (), script: Optional[Path] = None) -> Writer: + name = f"{next(self._n):02d}-{lane}" + given = (extra or {}).get("LANES_LANE", lane) + where, agents = self.checkout(name, origin, self.write_lanes(argv, given)) + psql = self.psql_dir(name, arms) + env = dict(self.env) + env.update(LANES_LANE=lane, LANES_SESSION=session_of(lane), LANES_REPO=str(where), + AGENT_PROTOCOL_ROOT=str(agents), + PATH=os.pathsep.join([*map(str, first), str(psql), self.env["PATH"]])) + env.update(extra or {}) + command = ["bash", str(script or self.lanes_edit), *argv] + if barrier is not None: + command = [sys.executable, "-I", str(self.launcher), str(barrier.ready / name), + str(barrier.fifo), *command] + proc = subprocess.Popen(command, cwd=str(where), env=env, stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8", + errors="replace", start_new_session=True) + writer = Writer(name, lane, proc, psql) + self.writers.append(writer) + return writer + + def run(self, lane: str, argv: Sequence[str], **options) -> Writer: + writer = self.launch(lane, argv, **options) + writer.wait() + return writer + + def gh_calls(self) -> List[dict]: + log = self.gh_dir / "gh.log" + return [json.loads(l) for l in log.read_text(encoding="utf-8").splitlines() if l] if log.exists() else [] + + # -------------------------------------------------- the register's reads + def rows(self, query: str) -> list: + return self.c.json(f"SELECT coalesce(json_agg(t), '[]'::json) FROM ({query}) t;") + + def journal_after(self, seq: int = 1) -> list: + return self.rows(f"SELECT seq, write_id::text AS write_id, verb, lane FROM {REGISTER}.journal " + f"WHERE seq > {seq} ORDER BY seq") + + def has_write(self, write_id: str) -> bool: + return self.c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{write_id}';") != "0" + + def claimed(self, *objects: str) -> list: + """The live `CLAIMED` holds on `objects`, (object, lane).""" + names = ", ".join(f"'{o}'" for o in objects) + return [(r["object"], r["lane"]) for r in self.rows( + f"SELECT object, lane FROM {REGISTER}.object_holds WHERE object IN ({names}) " + f"AND verb = 'CLAIMED' AND superseded_by IS NULL ORDER BY object, lane")] + + def log_lines(self, verb: str, obj: str) -> int: + return int(self.c.value(f"SELECT count(*) FROM {REGISTER}.log_lines WHERE verb = '{verb}' " + f"AND object = '{obj}';")) + + def utc(self, minutes_ago: float = 0) -> str: + return self.c.value("SELECT to_char((clock_timestamp() - make_interval(secs => " + f"{minutes_ago * 60})) AT TIME ZONE 'UTC', " + "'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"');") + + def open_slots(self, lane: str) -> List[List]: + return [[r["slot_kind"], r["repo_key"], r["pr"]] for r in self.rows( + f"SELECT slot_kind, repo_key, pr FROM {REGISTER}.main_slots WHERE lane = '{lane}' " + "ORDER BY line_id")] + + def hold_row_locked(self) -> subprocess.Popen: + """The test's admin session holding the head row `FOR UPDATE` in an + open transaction, as AT-D9's F3 row does.""" + admin = self.c.db.session(appname="t243-admin") + admin.stdin.write(f"BEGIN;\nSELECT seq FROM {REGISTER}.register_head FOR UPDATE;\n") + admin.stdin.flush() + said = admin.stdout.readline().strip() + assert said.isdigit(), f"the admin session did not lock the head row: {said!r}" + return admin + + @staticmethod + def unlock(admin: subprocess.Popen) -> None: + admin.stdin.write("ROLLBACK;\n") + admin.stdin.close() + admin.wait(timeout=30) + + def waits_on_lock(self, write_id: str) -> bool: + """D19's barrier: the backend whose query carries `write_id` waits on + a lock, polled at 0.1 s steps under one 1.5 s ceiling.""" + deadline = time.monotonic() + 1.5 + while time.monotonic() < deadline: + if self.c.value("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' " + f"AND query LIKE '%{write_id}%';") != "0": + return True + time.sleep(0.1) + return False + + +@pytest.fixture() +def estates(pg_cluster, tmp_path): + """Test 4's estates, each on a clone of `tmpl` of its own.""" + made: List[str] = [] + alive: List[Estate] = [] + + def make(**options) -> Estate: + name = f"t243_{next(_NAMES)}" + pg_cluster.psql(f"CREATE DATABASE {name} TEMPLATE {W_TEMPLATE};") + made.append(name) + found = pg_cluster.psql(f"SELECT to_regclass('{REGISTER}.register_head') IS NOT NULL;", dbname=name) + assert found.stdout.strip() == "t", ( + f"{W_TEMPLATE} holds no {REGISTER} schema: lane 2's T227 builds it by the bootstrap") + pg_cluster.psql(f"REVOKE ALL ON DATABASE {name} FROM PUBLIC;\n" + f"GRANT CONNECT ON DATABASE {name} TO {CONNECT_TO};\n") + root = tmp_path / f"estate-{len(made)}" + root.mkdir() + alive.append(Estate(root, Clone(pg_cluster, name), **options)) + return alive[-1] + + yield make + for e in alive: + for w in e.writers: + if w.ended is None and w.proc.poll() is None: + os.killpg(w.proc.pid, 9) + for name in made: + pg_cluster.drop_database(name) + + +def hold_point(fifo: Path, writer: Writer, timeout: float = 90): + """`fifo` opened for writing once `writer`'s call is held on it (the + shim's `hold`), bounded: a writer that never reaches the call fails the + case and never hangs it. Closing the handle lets the call go.""" + deadline = time.monotonic() + timeout + while True: + try: + return os.fdopen(os.open(str(fifo), os.O_WRONLY | os.O_NONBLOCK), "wb") + except OSError as error: + if error.errno != errno.ENXIO: + raise + assert writer.ended is None, ( + f"{writer.name} exited {writer.code} before its commit call was held: {writer.err[-400:]}") + assert time.monotonic() < deadline, f"{writer.name}'s commit call was not held in {timeout} s" + time.sleep(0.05) + + +CLIENT = pytest.mark.skipif(shutil.which("bash") is None or shutil.which("git") is None, + reason="the lane tooling is bash over git") +WINDOW = {"mode": "window", "match": "commit_write", "ms": 200} + + +def claim(obj: str, *more: str) -> List[str]: + return ["claim", obj, "--home", KEY, *more] + + +def burst(e: Estate, objects: Sequence[str], window_ms: int, extra: dict = None, + node: Optional[pytest.Item] = None, name: str = "burst", + wait_s: float = 150) -> Tuple[List[Writer], float]: + """Writers on `objects`, one lane each, released together; returns them, + and the drain from the release to the last exit. Before its caller + asserts anything, it records the drain and each writer's attempts, + counted as snapshot calls in the shim's log, and its seconds from the + release to its exit, never asserted (TP:3783-3787 at `0c3750a5`; lane 3, + #165 6838735040; T243l): `record(name, node, ...)`. Beside them, for + lane 1's two points (#165 6840891258; T243m), also never asserted: each + writer's calls in order with their exits, its commits' losses by + SQLSTATE, and its commits' seconds, as `burst_calls` reads them, and the + longest commit of the burst. + + `attempts` counts the snapshots alone, `burst_calls`' `s` calls, one per + attempt, as the row counts them, and `reads` every other call that is + not a commit or a lookup, its `r` and `m` calls, the detached render's + reads among them, which a slower runner may not have made yet when the + logs are read. UNTIL T243q `attempts` counted both, so an older figure, + py-bench's 73 a burst (55 + 18) and CI round 6g's, is `attempts + + reads` here (lane 2's RV20, finding 9; T243q). + + `name` is the record's, `burst`, but `burst-measure` for AT-4 (c)'s + measuring burst, so a census tells the two apart; `wait_s` bounds each + writer's wait, 150 s, or longer where AT-4 (c)'s deadline asks for it, + so the case's own drain assertion decides a slow burst (T243u). The + record ends with each writer's exit code, `codes`, so it shows how the + burst ended, the measuring burst's among them (lane 2's RV26, NIT 7; + T243w).""" + barrier = Barrier(e.root, len(objects)) + arms = [dict(WINDOW, ms=window_ms)] + writers = [e.launch(lane, claim(obj, "--no-github"), arms=arms, barrier=barrier, extra=extra) + for lane, obj in zip(BURST, objects)] + for w in writers: + (w.psql_dir / "tee").write_text("", encoding="utf-8") # the stamp's tee (T243m) + released = barrier.release() + for w in writers: + w.wait(wait_s) + drain = max(w.ended for w in writers) - released + read = {w.lane: burst_calls(w) for w in writers} + kinds = {lane: [call[0] for call in r[0].split()] for lane, r in read.items()} + record(name, node, drain_s=round(drain, 2), margin_vs_30s=round(D7_S / max(drain, 0.01), 2), + attempts={lane: k.count("s") for lane, k in kinds.items()}, + reads={lane: k.count("r") + k.count("m") for lane, k in kinds.items()}, + seconds={w.lane: round(w.ended - released, 2) for w in writers}, + calls={lane: r[0] for lane, r in read.items()}, losses={lane: r[1] for lane, r in read.items()}, + commit_s={lane: r[2] for lane, r in read.items()}, + longest_commit_s=max((x for r in read.values() for x in r[2]), default=None), + codes={w.lane: w.code for w in writers}) + return writers, drain + + +def burst_calls(w: Writer) -> Tuple[str, Dict[str, int], List[float]]: + """Lane 1's two points (#165 6840891258; T243m), read from `w`'s logs: + + - its calls in order, each ``, the code `psql`'s own exit + in the shim's ends (`?` where it has none): `s` the snapshot, the + request just before a commit; `c` a commit (`commit_write`); `l` a + lookup (`find_write`); `m` the render's `record_mirror_state`; `r` + any other request, the render's head read, a snapshot's second + request or LR004's re-read among them; + - its commits' losses, each failed commit counted under the SQLSTATE of + each `ERROR:` line its stderr carried (LR001, 40001, 55P03, 40P01, + LR008 or another), as the stamp's tee logs them, or `unread` where + the tee logged none; + - each commit's seconds, from the stamp's start to its stderr's close, + the shim's window included, where the tee logged its end. + + A burst's figures, never asserted.""" + calls, ends = w.stamps(), w.ends() + tees = {x["t"]: x for x in w._jsonl("tee.log")} + letters: List[str] = [] + losses: Dict[str, int] = {} + seconds: List[float] = [] + for k, call in enumerate(calls): + text = call.get("stdin") or "" + after = (calls[k + 1].get("stdin") or "") if k + 1 < len(calls) else "" + if "commit_write" in text: + kind = "c" + elif "find_write" in text: + kind = "l" + elif "record_mirror_state" in text: + kind = "m" + else: + kind = "s" if "commit_write" in after else "r" + end = ends.get(call["call"], {}) + code = end.get("psql", end.get("exit", "?")) + letters.append(f"{kind}{code}") + tee = tees.get(call["t"]) + if kind == "c" and tee: + seconds.append(round(tee["end"] - call["t"], 2)) + if kind == "c" and code != 0: + for state in (tee or {}).get("states") or ["unread"]: + losses[state] = losses.get(state, 0) + 1 + return " ".join(letters), losses, seconds + + +def attempt_starts(w: Writer) -> Tuple[List[dict], List[int]]: + """`w`'s requests, each with its start on the stamp's wall clock + (`Writer.stamps`), and the index among them of each attempt's start: + its snapshot request, `burst_calls`' `s`, the request just before a + commit call that is not itself a commit, a lookup or the render's + `record_mirror_state`. Read by `attempt_seconds` (T243u) and `room` + (T243v).""" + calls = w.stamps() + + def asks(k: int, *names: str) -> bool: + return any(name in (calls[k].get("stdin") or "") for name in names) + + return calls, [k for k in range(len(calls) - 1) if asks(k + 1, "commit_write") + and not asks(k, "commit_write", "find_write", "record_mirror_state")] + + +def attempt_seconds(w: Writer) -> List[float]: + """Each attempt of `w`'s run, in seconds, for AT-4 (c)'s runner's own D7 + (T243u). An attempt that lost its turn runs from its snapshot request's + start, `burst_calls`' `s`, the request just before a commit call, to the + next attempt's: one turn of D7's loop, its decision and the next + attempt's directory included (`44f487d1:lanes-edit.sh:11176-11190`). The + last attempt runs from its snapshot request's start to the end of its + commit call, since what follows a commit (the tail, the replay, the + render's fork) is not D7's loop; where the tee has not logged that end, + it is left out. Every time is the stamp's wall clock, `stamps.log` and + the tee's `tee.log`, never the writer's monotonic `started` and + `ended`. D7's clock starts at the first attempt's start + (`lanes-edit.sh:11176`), so the first attempt, a cold image cache's, + is one of them. The starts are `attempt_starts`' (T243v).""" + calls, starts = attempt_starts(w) + tees = {x["t"]: x for x in w._jsonl("tee.log")} + seconds = [calls[b]["t"] - calls[a]["t"] for a, b in zip(starts, starts[1:])] + if starts: + tee = tees.get(calls[starts[-1] + 1]["t"]) + if tee: + seconds.append(tee["end"] - calls[starts[-1]]["t"]) + return seconds + + +def p95(values: Sequence[float]) -> float: + """The 95th percentile of `values` by nearest rank: the smallest value at + or above 95% of them (T243u).""" + ordered = sorted(values) + return ordered[math.ceil(0.95 * len(ordered)) - 1] + + +def room(writers: Sequence[Writer], deadline: int) -> Tuple[Optional[float], Optional[float]]: + """AT-4 (c)'s room under its deadline, `room_s`, and the figure it is + taken from, `last_start_s`, in seconds (T243v; lane 2's RV26, SHOULD 3; + lane 3, #165 6870898118: "record `room_s` beside the drain"). + + D7's loop takes `PW_T0` from `SECONDS` just before a write's first + attempt and, at the top of each later attempt, ends the run 75, + "register busy", once `SECONDS - PW_T0` has reached D + (`142afd90:lanes-edit.sh:11198-11201`). So D bounds, for each writer, + the start of its last attempt, counted from the start of its first. + `last_start_s` is the latest such start over `writers`, and `room_s` is + `deadline - last_start_s`: the room the writer nearest its deadline still + had when its last attempt started. A writer whose next attempt would + start after that room is spent exits 75, and the case's oracle fails. + + Each start is an attempt's snapshot request, `attempt_starts`', on the + stamp's wall clock. It is the attempt's first request (`pw_attempt_dir` + makes none, `:11203-11204`), so the same short lead, the attempt's + directory and the client up to its request, comes before the first + attempt's snapshot as before the last's, and the difference leaves it + out. `SECONDS` is whole seconds of the wall clock, so the deadline acts + at the D-th whole second after `PW_T0`, between D - 1 and D seconds + after it: a `room_s` of 1 s or less may already be past it. + + Recorded, never asserted. Both are None where no writer's logs hold an + attempt.""" + spans = [] + for w in writers: + calls, starts = attempt_starts(w) + if starts: + spans.append(calls[starts[-1]]["t"] - calls[starts[0]]["t"]) + if not spans: + return None, None + return round(deadline - max(spans), 2), round(max(spans), 2) + + +def burst_ends(e: Estate, writers: Sequence[Writer]) -> List[Writer]: + """The ends a burst may have where its case's own row does not ask every + writer to commit (AT-4 (g), AT-F14): each writer committed once, its + write in the journal, or exited 75 with nothing written, its write ids + absent from the journal, and none ended any other way; the journal grew + by one row per committed writer, its seqs from 2 with no gap and no + repeat. That is AT-D9c's form for its own burst (Y TP:3755-3759; + TP:3781-3785 at `0c3750a5`). Lane 3 pointed the bursts to it (#165 + 6840952649), then withdrew that (6841239274): each case's own row + decides, (g)'s the cache's (Y TP:936-950) and AT-F14's each committed + writer's (Y TP:6852-6854), and neither asks every writer to commit + (T243r). Returns the writers that committed (T243n). + + At least one writer commits: a burst in which every writer ends 75 has + lost every writer, against "A burst loses no writer" (Y TP:916-919), and + would leave each check over the writers that committed, and (g)'s head + order, true of nothing (lane 2's RV20, finding 8; T243p).""" + others = [(w.lane, w.code, w.err[-300:]) for w in writers if w.code not in (0, 75)] + assert not others, f"a burst writer ended neither 0 nor 75: {others}" + committed = [w for w in writers if w.code == 0] + assert committed, f"no burst writer committed: all {len(writers)} ended 75 with nothing written" + for w in writers: + found = [x for x in set(w.write_ids()) if e.has_write(x)] + if w.code == 0: + assert len(found) == 1, f"{w.lane} exited 0, and {len(found)} of its write ids are in the journal" + else: + assert not found, f"{w.lane} exited 75, yet its write {found} is in the journal: {w.err[-300:]}" + grew = e.journal_after() + assert [r["seq"] for r in grew] == list(range(2, 2 + len(committed))), ( + f"{len(committed)} writers committed, and the journal grew by {grew}") + return committed + + +def record(name: str, node: Optional[pytest.Item] = None, **figures) -> str: + """A figure recorded beside the result, never asserted (TP:7556-7559), + and returned. With `node`, the case's item, the line is also the case's + junit property `T243 `, which a failed case's junit carries as a + passed one's does, though it leaves out captured output (T243l).""" + line = f"[T243 {name}] " + ", ".join(f"{k}={v}" for k, v in figures.items()) + print(line) + if node is not None: + node.user_properties.append((f"T243 {name}", line)) + return line + + +@CLIENT +def test_two_claimants_one_claimed_and_the_other_exits_2(estates): + """AT-4 (a): two lanes claim O through the barrier, W = 200 ms. One + `CLAIMED`, in the journal and the line store; exits {0, 2}; the journal + grew by one; the loser's write id is absent; no `CLAIM-LOST`; the winner + prints `register seq `, its entry's seq, and the claim comment its + tail posts through the fake `gh` cites the same ``.""" + e = estates() + barrier = Barrier(e.root, 2) + writers = [e.launch(lane, claim(OBJ), arms=[WINDOW], barrier=barrier) for lane in (LA, LB)] + barrier.release() + codes = sorted(w.wait() for w in writers) + assert codes == [0, 2], [(w.lane, w.code, w.err[-400:]) for w in writers] + win, lose = sorted(writers, key=lambda w: w.code) + grew = e.journal_after() + assert [(r["verb"], r["lane"]) for r in grew] == [("claim", win.lane)], grew + assert e.claimed(OBJ) == [(OBJ, win.lane)] + assert e.log_lines("CLAIMED", OBJ) == 1 + assert all(not e.has_write(w) for w in lose.write_ids()), "the loser's write id is in the journal" + assert e.c.value(f"SELECT count(*) FROM {REGISTER}.doc_lines WHERE text LIKE '%CLAIM-LOST%';") == "0" + seq = grew[0]["seq"] + assert win.seq_said() == seq, f"the winner printed {win.seq_said()}, its entry is seq {seq}: {win.err!r}" + cites = [g for g in e.gh_calls() if f"register seq {seq}" in json.dumps(g)] + assert cites, f"no claim comment through gh cites register seq {seq}: {e.gh_calls()}" + + +@CLIENT +def test_the_later_hold_exits_2_and_prints_the_earlier_line(estates): + """AT-4 (b): two `HOLD`s on one repository's `main`, through the barrier. + The journal grew by one, the first writer's `HOLD`; it exits 0; the + second exits 2, writes nothing, and its stderr carries the earlier line + verbatim with its register seq (D18).""" + e = estates() + texts = {lane: f"HOLD {EM} lane {lane} ({W_WS}), {e.utc()}: {KEY} main for T243's case (b)" + for lane in (LA, LB)} + barrier = Barrier(e.root, 2) + writers = [e.launch(lane, ["append-line", texts[lane]], arms=[WINDOW], barrier=barrier) + for lane in (LA, LB)] + barrier.release() + for w in writers: + w.wait() + grew = e.journal_after() + assert len(grew) == 1, grew + first = next(w for w in writers if w.lane == grew[0]["lane"]) + later = next(w for w in writers if w is not first) + assert (first.code, later.code) == (0, 2), [(w.lane, w.code, w.err[-400:]) for w in writers] + assert texts[first.lane] in later.err, f"the later HOLD's stderr lacks the earlier line: {later.err!r}" + # The relay names the earlier line's seq as `held-by=`, then the path + # and the line, tab-separated (register-cli.md:519; T243i). + assert f"held-by={grew[0]['seq']}\t" in later.err, f"no held-by= in {later.err!r}" + assert all(not e.has_write(w) for w in later.write_ids()) + + +@CLIENT +def test_ten_writers_released_together_all_commit_in_order(estates, request): + """AT-4 (c), section 4.1's oracle: ten writers on ten free objects, W = + 200 ms, under the runner's own D7 (T243u; tasks:2337 at `8e8d1b49`; Brett's + "(d) Both (a) and (c) (Recommended)", 2026-11-13): the per-workstation + formula of the design's "D7 stays 30 s" (Y:5906), `max(30 s, 10 x the + runner's attempt p95)`, set as `LANES_REGISTER_DEADLINE_S` in whole + seconds rounded up. The writer honours whole seconds only, and ignores + any other value, keeping 30 (`pw_deadline_s`, + `44f487d1:lanes-edit.sh:8645-8658`), so the case rounds up itself. + + Each writer reads its deadline once, before its first attempt + (`:11157`), so the p95 comes from a measuring burst of the same shape, + run just before in an estate of its own: ten writers on `#500` to `#509` + released together, W = 200 ms, a cold image cache, and + `MEASURE_DEADLINE_S`, which none is meant to reach, so all ten stay in + the load until each commits. Every attempt of the ten, as + `attempt_seconds` reads it, is pooled, and the p95 taken by nearest rank. + Before that, all ten measuring writers must have exited 0: one that + ended early lightens the load and lowers the p95, and one that ran out + of `MEASURE_DEADLINE_S` exits 75 and cuts the pooled sample short, so + either fails the case before its burst runs; `burst-measure` records + each one's exit code (lane 2's RV26, NIT 7; T243w). + + The burst then holds section 4.1's oracle: all exit 0, none 75 or 69; + seqs 2 to 11, no gap and no repeat; ten distinct write ids; one + `CLAIMED` per object; the head reads 11. Its drain is asserted under + twice its deadline (timing rule 2), 60 s at the default. Before any + assertion it records, never asserted and each as the case's junit + property (T243l): the measuring burst's own record, `burst-measure`; + the burst's, `burst`, with each writer's attempts and seconds; and + `burst-deadline`, the p95, the deadline and the drain side by side, with + the burst's own attempt p95 by the same rule, so a reader sees whether + the measure predicted it, and `room_s`, the room the deadline left + above the latest start of a writer's last attempt, with that start, + `last_start_s` (`room`; T243v). A 75 under this deadline is a writer + that needed more room than it had, so `room_s` shows how near a + passing run came to one (lane 3, #165 6870898118). Its row asks every + writer to commit, "All ten exit 0, and none exits 75 or 69" (Y + TP:7509; TP:7583 at `0c3750a5`), so it keeps that oracle where AT-4 (g) + and AT-F14 take `burst_ends` (T243n). Case (h) still asserts D7's 30 s + default once, with the variable unset.""" + measure = estates() + measured, measure_drain = burst(measure, [f"{KEY}#{500 + k}" for k in range(10)], 200, + extra={"LANES_REGISTER_DEADLINE_S": str(MEASURE_DEADLINE_S)}, + node=request.node, name="burst-measure") + assert [w.code for w in measured] == [0] * 10, ( + f"the measuring burst did not end as meant, all ten committing inside its {MEASURE_DEADLINE_S} s, " + f"so its attempts do not measure the burst's load: {[(w.lane, w.code, w.err[-300:]) for w in measured]}") + seconds = [s for w in measured for s in attempt_seconds(w)] + assert seconds, f"the measuring burst's logs hold no attempt: {[(w.lane, w.code) for w in measured]}" + runner_p95 = p95(seconds) + deadline = max(D7_S, math.ceil(D7_PER_P95 * runner_p95)) + e = estates() + objects = [f"{KEY}#{100 + k}" for k in range(10)] + writers, drain = burst(e, objects, 200, extra={"LANES_REGISTER_DEADLINE_S": str(deadline)}, + node=request.node, wait_s=max(150, 2 * deadline + 30)) + own = [s for w in writers for s in attempt_seconds(w)] + room_s, last_start_s = room(writers, deadline) + record("burst-deadline", request.node, p95_s=round(runner_p95, 3), attempts_measured=len(seconds), + measure_drain_s=round(measure_drain, 2), deadline_s=deadline, twice_deadline_s=2 * deadline, + drain_s=round(drain, 2), margin_vs_deadline=round(deadline / max(drain, 0.01), 2), + room_s=room_s, last_start_s=last_start_s, + burst_p95_s=round(p95(own), 3) if own else None, burst_attempts=len(own)) + assert [w.code for w in writers] == [0] * 10, [(w.lane, w.code, w.err[-300:]) for w in writers] + grew = e.journal_after() + assert [r["seq"] for r in grew] == list(range(2, 12)), grew + assert len({r["write_id"] for r in grew}) == 10 + assert e.claimed(*objects) == sorted(zip(objects, BURST)) + assert e.c.head() == 11 + assert drain < 2 * deadline, ( + f"the burst drained in {drain:.1f} s, past twice its deadline of {deadline} s, " + f"10 x the measured attempt p95 of {runner_p95:.2f} s, or D7's {D7_S} s") + + +@CLIENT +def test_a_write_that_cannot_get_its_turn_exits_75_register_busy(estates): + """AT-4 (d): two writers on distinct objects, W = 2 s, deadline 1 s. One + exits 0, the other 75; the busy writer's write id is absent; its stderr + says nothing was written and a retry in seconds is safe (P231-233).""" + e = estates() + barrier = Barrier(e.root, 2) + writers = [e.launch(lane, claim(obj, "--no-github"), arms=[dict(WINDOW, ms=2000)], barrier=barrier, + extra={"LANES_REGISTER_DEADLINE_S": "1"}) + for lane, obj in ((LA, f"{KEY}#51"), (LB, f"{KEY}#52"))] + barrier.release() + assert sorted(w.wait() for w in writers) == [0, 75], [(w.lane, w.code, w.err[-400:]) for w in writers] + busy = next(w for w in writers if w.code == 75) + assert all(not e.has_write(w) for w in busy.write_ids()) + said = busy.err.lower() + assert all(word in said for word in BUSY_WORDS), f"the busy text lacks {BUSY_WORDS}: {busy.err!r}" + assert len(e.journal_after()) == 1 + + +@CLIENT +def test_the_default_deadline_is_30_s_for_a_write_that_never_gets_its_turn(estates): + """AT-4 (h): `LANES_REGISTER_DEADLINE_S` unset; the admin session holds + the head row `FOR UPDATE` for the whole case, so each commit call gets + 55P03 at its `lock_timeout`, a lost turn. The writer exits 75, its write + ids absent, its stderr carrying the busy text with "attempts in 30 s"; + it exits no earlier than 29 s after its launch (timing rule 3) and under + 2 x (30 + 8 + 8) = 92 s, and no snapshot call starts more than 60 s after + the first (timing rule 2). Both times are recorded against 46 s and + 30 s.""" + e = estates() + admin = e.hold_row_locked() + try: + w = e.run(LA, claim(f"{KEY}#61", "--no-github")) + finally: + e.unlock(admin) + took = w.ended - w.started + assert w.code == 75, f"exit {w.code}, not 75: {w.err[-600:]}" + assert w.write_ids() and all(not e.has_write(x) for x in w.write_ids()) + assert "register busy" in w.err.lower() and f"attempts in {D7_S} s" in w.err, w.err + assert took >= D7_S - 1, f"it exited {took:.1f} s after its launch, before D7's {D7_S} s less 1 s" + assert took < 2 * (D7_S + 8 + 8), f"it exited {took:.1f} s after its launch, past 92 s" + starts = [s["t"] for s in w.stamps() if "commit_write" not in (s.get("stdin") or "")] + spread = max(starts) - min(starts) if starts else 0.0 + assert spread <= TWICE_D7_S, f"a snapshot call started {spread:.1f} s after the first" + record("default-deadline", exit_s=round(took, 2), vs_46s=round(46 - took, 2), + snapshot_spread_s=round(spread, 2), vs_30s=round(D7_S - spread, 2), attempts=len(starts)) + + +#: AT-4 (f), with lane 3's text for T243 (#165 6772120103, "T243 (:2232)"): +#: each case's lines before, the line `append-line` sends, and what it must +#: do: refused with 2 and nothing written, or committed with 0, taking or +#: freeing what Q17 (B) says (`main_slots` for the writer's lane). +NO_REPO = { + "hold-no-repo": ([], "HOLD {em} lane {lane} ({ws}), {utc}: for a frozen window", 2, None), + "landing-no-repo": ([], "LANDING {em} lane {lane}, session {sid}@{ws}, {utc}, PR #{pr}", 2, None), + "take-no-utc": ([], "HOLD {em} lane {lane} ({ws}): {key} main for a frozen window", 2, None), + "free-text": ([], "NOTE {em} lane {lane}: the register's free text (T243)", 0, []), +} + +#: Test 4 (f)'s slot lines of another shape, which commit 0 (lane 3, +#: 6771569269: "`append-line` writes these lines on primary ... T424 does not +#: change"), in a function of their own, outside T424's word +#: `hold_line_that_names_no_repository` (lane 3's batch 7, ruling 4). The same +#: columns as `NO_REPO`'s. +OTHER_SHAPE = { + "landed-no-repo": (["LANDING {em} lane {lane}, session {sid}@{ws}, {utc}, PR #{pr} into {key} main"], + "LANDED {em} lane {lane}, session {sid}@{ws}, {utc}, PR #{pr} {arrow} abc1234", 0, []), + "hold-released-no-repo": (["HOLD {em} lane {lane} ({ws}), {utc}: {key} main for a frozen window"], + "HOLD RELEASED {em} lane {lane} ({ws}), {utc}", 0, [["hold", KEY, None]]), + "landing-no-pr": ([], "LANDING {em} lane {lane}, session {sid}@{ws}, {utc}, into {key} main", 0, []), +} + + +@CLIENT +@pytest.mark.parametrize("case", list(NO_REPO)) +def test_a_hold_line_that_names_no_repository_is_refused_with_2(estates, case): + """AT-4 (f) (`design.md` S6): on primary, `append-line` with a line + matching `HOLD ` or `LANDING ` from which no repository can be read exits + 2 and writes nothing, and so does a take whose UTC cannot be read (U3; + its client half is AT-F13 (b)). A line of another shape is written as free + text: [free-text] commits 0, and as a line of no kind it gets no + `register_lines` row and takes or frees nothing (T243k).""" + append_line_case(estates(), case, NO_REPO[case]) + + +@CLIENT +@pytest.mark.parametrize("case", list(OTHER_SHAPE)) +def test_a_slot_line_of_another_shape_commits_0(estates, case): + """Test 4 (f)'s lines of another shape (lane 3, 6771569269): on primary a + `LANDED` naming no repository commits 0 and frees its pair in + `main_slots`; a `HOLD RELEASED` naming none and a `LANDING` naming no PR + commit 0 and take or free nothing. T424's classifier must let them + through (lane 3's batch 7, ruling 4).""" + append_line_case(estates(), case, OTHER_SHAPE[case]) + + +def append_line_case(e: Estate, case: str, row: tuple) -> None: + """One case of test 4 (f): `row`'s lines before, then its line, and its + exit, and what the register then holds.""" + before_lines, text, code, slots = row + + def spell(form: str) -> str: + return form.format(em=EM, arrow=ARROW, lane=LA, ws=W_WS, sid=session_of(LA), utc=e.utc(), + pr=PR, key=KEY) + + for line_form in before_lines: + assert e.run(LA, ["append-line", spell(line_form)]).code == 0 + if before_lines: + assert e.open_slots(LA) == [[("landing" if "LANDING" in before_lines[0] else "hold"), KEY, + PR if "LANDING" in before_lines[0] else None]], e.open_slots(LA) + head, state = e.c.head(), e.c.state() + slots_before = e.rows(f"SELECT * FROM {REGISTER}.main_slots ORDER BY line_id") + line = spell(text) + w = e.run(LA, ["append-line", line]) + assert w.code == code, f"[{case}] exit {w.code}, not {code}: {w.err[-600:]}" + if code == 2: + assert e.c.state() == state, f"[{case}] the refusal changed the register" + assert w.commit_calls() == [], f"[{case}] a commit call was sent" + return + assert e.c.head() == head + 1 + said = e.rows(f"SELECT kind, slot_role FROM {REGISTER}.register_lines WHERE seq = {head + 1}") + if case == "free-text": + # A LINE OF NO KIND GETS NO `register_lines` ROW (lane 1, #165 + # 6834413025, which lanes 3 and 4 agree; ERR-49, B19 tasks:515, + # "`register_lines` for kinds only"): `parse_register_line` gives None + # for it, and the commit sends no row. So the oracle is the line + # written, no row, `main_slots` as it was, and another lane's `HOLD` + # still committing (T243k). + assert said == [], f"[{case}] a line of no kind got register_lines rows: {said}" + sent = e.rows(f"SELECT path, new_text FROM {REGISTER}.journal_edits WHERE seq = {head + 1} " + "AND op = 'append'") + assert {"path": LANES_MD, "new_text": line} in sent, f"[{case}] the line was not written: {sent}" + after = e.rows(f"SELECT * FROM {REGISTER}.main_slots ORDER BY line_id") + assert after == slots_before, f"[{case}] main_slots moved: {slots_before} -> {after}" + other = e.run(LB, ["append-line", f"HOLD {EM} lane {LB} ({W_WS}), {e.utc()}: {KEY} main for T243's " + "free-text case"]) + assert other.code == 0, f"[{case}] another lane's HOLD exited {other.code}: {other.err[-600:]}" + return + role = "close" if case == "landed-no-repo" else None + assert len(said) == 1 and said[0]["slot_role"] == role, f"[{case}] {said}, its slot_role not {role}" + assert e.open_slots(LA) == slots, f"[{case}] main_slots for {LA}: {e.open_slots(LA)}, not {slots}" + + +#: Q17 (B)'s table (lane 1's `REGISTER_SLOTS`, T118): each kind's line, the +#: line before it that it pairs with, and whether `main` is taken after it. +KINDS = { + "hold": ([], "HOLD", True), + "hold-refresh": ([], "HOLD (refresh)", False), + "hold-run-id": ([], "HOLD (run id)", False), + "hold-released": (["HOLD"], "HOLD RELEASED", False), + "hold-withdrawn": (["HOLD"], "HOLD WITHDRAWN", False), + "hold-expired": (["HOLD"], "HOLD EXPIRED", False), + "queued-hold": ([], "QUEUED-HOLD", False), + "landing": ([], "LANDING", True), + "landed": (["LANDING"], "LANDED", False), + "landing-expired": (["LANDING"], "LANDING EXPIRED", False), + "landing-withdrawn": ([], "LANDING WITHDRAWN", False), +} + + +def kind_line(e: Estate, kind: str, lane: str, minutes_ago: float = 0) -> str: + utc = e.utc(minutes_ago) + if kind.startswith("LANDING") or kind == "LANDED": + tail = f" {ARROW} abc1234" if kind == "LANDED" else "" + return f"{kind} {EM} lane {lane}, session {session_of(lane)}@{W_WS}, {utc}, PR #{PR} into {KEY} main{tail}" + return f"{kind} {EM} lane {lane} ({W_WS}), {utc}: {KEY} main for T243's table" + + +@CLIENT +@pytest.mark.parametrize("kind", list(KINDS)) +def test_each_line_kind_takes_or_releases_main_as_q17_b_says(estates, kind): + """After each kind's line, `main_slots` shows `main` taken or released as + Q17 (B)'s table says, and another lane's `HOLD` then exits 0 or 2 to + match. `LANDING EXPIRED` is written by AT-F13's form over a `LANDING` + composed with a UTC 31 minutes back (TP:975-980).""" + e = estates() + before, name, taken = KINDS[kind] + for pair in before: + ago = 31 if name == "LANDING EXPIRED" else 0 + assert e.run(LA, ["append-line", kind_line(e, pair, LA, ago)]).code == 0 + if name == "LANDING EXPIRED": + argv = ["append-line", "--landing-expired", LA, "--pr", str(PR), "--repo", KEY, + "--word", "Release the stale LANDING (Recommended)", "--word-where", + "lane openRepoTools-2's session", "--word-utc", e.utc(1)] + w = e.run(LB, argv) + else: + w = e.run(LA, ["append-line", kind_line(e, name, LA)]) + assert w.code == 0, f"[{kind}] exit {w.code}: {w.err[-600:]}" + slots = e.open_slots(LA) + assert bool(slots) == taken, f"[{kind}] main_slots for {LA}: {slots}" + other = e.run(LC, ["append-line", kind_line(e, "HOLD", LC)]) + assert other.code == (2 if taken else 0), f"[{kind}] another lane's HOLD exited {other.code}: {other.err[-400:]}" + + +@CLIENT +def test_claim_prints_its_register_seq(estates): + """Q19 (A), GB section 4.3 (R29): `claim` prints `register seq `, its + entry's seq, the first write after the cutover, 2.""" + e = estates() + w = e.run(LA, claim(OBJ, "--no-github")) + assert w.code == 0, w.err[-600:] + grew = e.journal_after() + assert [(r["seq"], r["verb"]) for r in grew] == [(2, "claim")], grew + assert w.seq_said() == 2, f"claim printed {w.err!r}" + + +@CLIENT +def test_a_serialization_failure_is_retried_never_reported_as_69(estates): + """AT-4 (e): in case (a)'s race the loser's first commit call fails (its + snapshot predates the winner's commit), and the loser re-runs from the + pre-check and exits 2, never 69 or 75; its write ids are absent.""" + e = estates() + barrier = Barrier(e.root, 2) + writers = [e.launch(lane, claim(OBJ, "--no-github"), arms=[WINDOW], barrier=barrier) for lane in (LA, LB)] + barrier.release() + for w in writers: + w.wait() + lose = next((w for w in writers if w.code != 0), None) + assert lose is not None and lose.code == 2, [(w.lane, w.code, w.err[-400:]) for w in writers] + assert not {69, 75} & {w.code for w in writers}, [w.code for w in writers] + calls = lose.commit_calls() + assert calls, "the loser made no commit call: its snapshot did not predate the winner's commit" + first = lose.ends().get(calls[0]["call"], {}) + assert first.get("psql") not in (None, 0), f"the loser's first commit call did not fail: {first}" + assert len(lose.snapshot_calls()) >= 2, "the loser did not re-read after its failed commit" + assert all(not e.has_write(x) for x in lose.write_ids()) + + +def _objects_named(e: Estate) -> Dict[str, Path]: + objects = e.state / "openRepoTools" / "register-images" / "objects" + return {p.name: p for p in objects.iterdir()} if objects.exists() else {} + + +@CLIENT +def test_the_image_cache_stays_content_addressed_under_the_burst_and_a_corrupt_object_is_refetched( + estates, request): + """AT-4 (g): in the state directory the burst's writers shared, every + object is named by the sha256 of its bytes and no temporary file is left; + `head` never decreases in the samples taken during the burst and ends no + higher than the journal's head; its manifest lists only present objects. + An object corrupted under its name is not used: the next write exits 0, + and its render is byte-identical to a full render.""" + e = estates() + cache = e.state / "openRepoTools" / "register-images" + samples: List[int] = [] + stop = threading.Event() + + def sample() -> None: + while not stop.is_set(): + try: + samples.append(int((cache / "head").read_text().strip())) + except (OSError, ValueError): + pass + time.sleep(0.05) + + sampler = threading.Thread(target=sample, daemon=True) + sampler.start() + try: + writers, _ = burst(e, [f"{KEY}#{200 + k}" for k in range(10)], 200, node=request.node) + finally: + stop.set() + sampler.join(5) + # (g)'s own row asks the cache of the state the ten writers shared, not + # that every one commits (Y TP:936-950; TP:941-955 at `0c3750a5`): a + # writer may end 75 with nothing written, as `burst_ends` checks (T243n). + burst_ends(e, writers) + named = _objects_named(e) + assert named, f"the burst left no object under {cache}" + for name, path in named.items(): + assert re.fullmatch(r"[0-9a-f]{64}", name), f"a file that is not an object is left: {name}" + assert sha256(path.read_bytes()) == name, f"{name} is not named by its bytes" + assert samples == sorted(samples), f"register-images/head went back: {samples}" + head = int((cache / "head").read_text().strip()) + assert head <= e.c.head() + listed = [line.split("\t")[1] for line in (cache / "seq" / str(head)).read_text().splitlines() if line] + assert set(listed) <= set(named), f"manifest {head} lists absent objects: {set(listed) - set(named)}" + + victim = named[listed[0]] + victim.write_bytes(victim.read_bytes() + b"corrupt\n") + _, lines = e.c.file(LANES_MD) + text = f"NOTE {EM} lane {LA}: written after the corrupt object (T243)" + w = e.run(LA, ["append-line", text]) + assert w.code == 0, f"the write after the corruption exited {w.code}: {w.err[-600:]}" + nl, after = e.c.file(LANES_MD) + assert after == [*lines, text] + digest = e.c.value(f"SELECT sha256 FROM {REGISTER}.files WHERE path = '{LANES_MD}';") + assert digest == sha256(rendered(after, bool(nl))), "the render after the corruption is not a full render's" + + +@CLIENT +@pytest.mark.parametrize("order", ["alias-holder-first", "behind-the-alias-first"]) +def test_an_alias_edit_racing_a_claim_cannot_let_two_claims_of_one_object_pass(estates, order): + """AT-C10, both orders. [alias-holder-first]: A, at T0, claims O and is + held at its commit call; T1 adds X as O's alias; B claims X and commits; + A, released, loses its turn, and its next snapshot's pre-check refuses it + with 2: one commit call from A. [behind-the-alias-first]: A, at T0, + claims X and commits; after T1, B claims O and is refused LR005 + `claim-spellings` on its commit call, its write id absent.""" + e = estates() + if order == "alias-holder-first": + fifo = e.root / "hold-a.fifo" + os.mkfifo(str(fifo)) + a = e.launch(LA, claim(OBJ, "--no-github"), + arms=[{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(fifo)}]) + held = hold_point(fifo, a) + e.push_alias() + b = e.run(LB, claim(OBJ_X, "--no-github")) + held.close() + a.wait() + assert (a.code, b.code) == (2, 0), [(w.lane, w.code, w.err[-400:]) for w in (a, b)] + assert e.claimed(OBJ, OBJ_X) == [(OBJ, LB)] + assert len(a.commit_calls()) == 1, "A made a second commit call" + assert a.ends()[a.commit_calls()[0]["call"]].get("psql") not in (None, 0) + else: + a = e.run(LA, claim(OBJ_X, "--no-github")) + assert a.code == 0, a.err[-600:] + e.push_alias() + b = e.run(LB, claim(OBJ, "--no-github")) + assert b.code == 2, f"B exited {b.code}: {b.err[-600:]}" + assert "LR005" in b.err and "claim-spellings" in b.err, b.err + assert b.commit_calls(), "B's refusal came before its commit call, not from the spelling check" + assert all(not e.has_write(x) for x in b.write_ids()) + assert e.claimed(OBJ, OBJ_X) == [(OBJ_X, LA)] + assert e.c.value(f"SELECT count(*) FROM {REGISTER}.object_holds WHERE object = '{OBJ_X}' " + f"AND lane = '{LB}';") == "0" + + +@CLIENT +@pytest.mark.parametrize("tip", ["same-tip", "behind-the-alias"]) +def test_two_spellings_of_one_object_claimed_at_once_leave_one_claimed_and_exit_2(estates, tip): + """AT-D19: L1 claims O and L2 claims X, each held at its first commit + call after both snapshots read `n`; the admin session holds the head row; + L1's call is released and waits on the lock, then L2's; the admin rolls + back, so L1 takes the head row first. L1 exits 0 and prints `register + seq `; L2's first commit fails, it re-reads and exits 2, never 69 + or 75, its stderr naming L1's claim; the journal grew by one; L1's + `object_holds` row carries O as typed, the key O and the alias X; one + live `CLAIMED` holds O and no line holds X.""" + e = estates() + origin_l2 = e.mirror_at_t0() if tip == "behind-the-alias" else None + e.push_alias() + n = e.c.head() + fifos = {lane: e.root / f"hold-{lane}.fifo" for lane in (LA, LB)} + for f in fifos.values(): + os.mkfifo(str(f)) + arms = {lane: [{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(fifos[lane])}] + for lane in (LA, LB)} + l1 = e.launch(LA, claim(OBJ, "--no-github"), arms=arms[LA]) + l2 = e.launch(LB, claim(OBJ_X, "--no-github"), arms=arms[LB], origin=origin_l2) + holds = {LA: hold_point(fifos[LA], l1), LB: hold_point(fifos[LB], l2)} # both snapshots read n + admin = e.hold_row_locked() + try: + holds[LA].close() + assert e.waits_on_lock(l1.write_ids()[0]), "L1's commit never waited on the head row" + holds[LB].close() + assert e.waits_on_lock(l2.write_ids()[0]), "L2's commit never waited on the head row" + finally: + e.unlock(admin) + l1.wait() + l2.wait() + assert (l1.code, l2.code) == (0, 2), [(w.lane, w.code, w.err[-400:]) for w in (l1, l2)] + assert l1.seq_said() == n + 1 + assert l2.ends()[l2.commit_calls()[0]["call"]].get("psql") not in (None, 0) + assert len(l2.snapshot_calls()) >= 2, "L2 did not re-read at n+1" + assert LA in l2.err, f"L2's refusal does not name L1's claim: {l2.err!r}" + assert [r["lane"] for r in e.journal_after(n)] == [LA] + spellings = e.c.value(f"SELECT spellings::text FROM {REGISTER}.object_holds WHERE object = '{OBJ}' " + f"AND lane = '{LA}';") + assert all(s in spellings for s in (OBJ, OBJ_X)), spellings + assert all(not e.has_write(x) for x in l2.write_ids()) + assert e.claimed(OBJ, OBJ_X) == [(OBJ, LA)] + assert e.log_lines("CLAIMED", OBJ_X) == 0 + if tip == "behind-the-alias": + assert "LR005" in l2.err and "claim-spellings" in l2.err, l2.err + + +@CLIENT +def test_the_claim_behind_the_alias_is_refused_by_lr005_on_its_spellings(estates): + """AT-D19 [behind-the-alias], by itself: L1 at the tip with the alias + claims O and commits; L2, at the tip before the alias, claims X. Neither + the verb's check nor `one_live_claim` sees L1's claim, so the refusal is + LR005 `claim-spellings` on L2's last commit call: exit 2, its write id + absent, one live `CLAIMED` on O.""" + e = estates() + frozen = e.mirror_at_t0() + e.push_alias() + l1 = e.run(LA, claim(OBJ, "--no-github")) + assert l1.code == 0, l1.err[-600:] + l2 = e.run(LB, claim(OBJ_X, "--no-github"), origin=frozen) + assert l2.code == 2, f"L2 exited {l2.code}: {l2.err[-600:]}" + assert "LR005" in l2.err and "claim-spellings" in l2.err, l2.err + calls = l2.commit_calls() + assert calls and l2.ends()[calls[-1]["call"]].get("psql") not in (None, 0), "no refused commit call" + assert all(not e.has_write(x) for x in l2.write_ids()) + assert e.claimed(OBJ, OBJ_X) == [(OBJ, LA)] + + +@CLIENT +def test_the_burst_window_is_effective_a_short_deadline_sends_writers_busy(estates): + """Section 4.1's companion: the same burst with a 1 s deadline. At least + one writer exits 75, none exits 69, and each busy writer's write ids are + absent from the journal.""" + e = estates() + writers, drain = burst(e, [f"{KEY}#{300 + k}" for k in range(10)], 200, + extra={"LANES_REGISTER_DEADLINE_S": "1"}) + codes = [w.code for w in writers] + assert 75 in codes and 69 not in codes, codes + assert set(codes) <= {0, 75}, codes + for w in writers: + if w.code == 75: + assert all(not e.has_write(x) for x in w.write_ids()), f"{w.lane} busy, yet written" + record("window-effective", busy=codes.count(75), drain_s=round(drain, 2)) + + +# ============================================================ PHASE W: AT-F14, output once (T248) +# +# F14 (S1, "Output and side effects, once"; lane 4's section): the verb runs +# as a child that stops at the seam; only the deciding attempt's output is +# replayed, stdout first; every side effect outside the register runs once, +# after COMMIT; one run makes one journal entry. On test 4's estate (T243's +# `Estate`), with logging fakes for `gh`, `tmux` and a `git` wrapper that log +# their environment (TP:6847-6851). The child's state directory is read as +# `/openRepoTools/register-writes/` (a reading: lane 4 names +# `/register-writes/`). They FAIL UNTIL L4-B3 and L4-B7 land (T413, +# T414, T415), with T243's own; without PostgreSQL 16 each skips. +# +# THE TAIL'S `gh` CALL IS THE COMMENT (T248a; lane 4's triage, #165 +# 6827643168, item 3). `claim` also calls `gh` for Rule 1's three reads, +# `issue view --comments` and `pr list` (`gh_reads_live`), at its step 2, +# before `write_event` reaches the seam. Under the flag they are made once a +# run, by its first attempt, and cached for the rest (T413; Y:5043), and they +# decide nothing. So the cases count only `gh`'s comment calls +# (`comment_calls`), `issue comment` or `pr comment` (`gh_comment`, T421). + +_ENV_LOGGER = r'''#!/usr/bin/env python3 +# T248's logging stand-in: its name, argv, whether the flag reached it, and +# each seam.out it can see; then the real tool, where one is named. For `gh` +# with `--body-file -` (`gh_comment`), the body it reads from stdin too, and +# no other call's stdin is read (T248a). A `tmux` given answers prints the +# one its `display-message` format asks for, keyed `t:` where the +# call names a target with `-t` (T248b). +import glob, json, os, sys +HERE = os.path.dirname(os.path.abspath(__file__)) +NAME = os.path.basename(sys.argv[0]) +REAL = REAL_TOOL +STATE = STATE_DIR +ANSWERS = TMUX_ANSWERS +ARGV = sys.argv[1:] +seams = {p: open(p, encoding="utf-8", errors="replace").read() + for p in glob.glob(os.path.join(STATE, "**", "seam.out"), recursive=True)} +body = None +if NAME == "gh" and "--body-file" in ARGV and ARGV[ARGV.index("--body-file") + 1:][:1] == ["-"]: + body = sys.stdin.read() +with open(os.path.join(HERE, "tools.log"), "a", encoding="utf-8") as log: + log.write(json.dumps({"tool": NAME, "argv": ARGV, "scratch": os.environ.get( + "LANES_REGISTER_SCRATCH"), "seams": seams, "body": body}) + "\n") +if ANSWERS and NAME == "tmux" and ARGV[:1] == ["display-message"]: + said = ANSWERS.get(("t:" if "-t" in ARGV else "") + ARGV[-1]) + if said is not None: + print(said) +if REAL: + os.execv(REAL, [REAL] + ARGV) +''' + +#: The answers of a `tmux` whose server holds the bound window live (T248b): +#: any window asked for by `-t` is in session `t243`, `bound_line`'s, and runs +#: `claude`, so `binding_window_state` reads it `live` and `guard_type` types +#: into it; the asking pane is `asker:1`, window `@90`, never a bound one. +TMUX_LIVE = {"t:#{session_name}": "t243", "t:#{pane_current_command}": "claude", + "#{session_name}:#{window_index}": "asker:1", "#{window_id}": "@90", "#{pane_id}": "%90"} + + +def loggers(e: Estate, tmux_live: bool = False) -> Path: + """`git` (a wrapper), `gh` and `tmux` (fakes), each logging to + `tools.log` beside them. With `tmux_live`, the fake `tmux` answers its + `display-message` reads from `TMUX_LIVE` (T248b); otherwise it prints + nothing, as before.""" + where = e.root / "loggers" + where.mkdir(exist_ok=True) + for tool, real in (("git", shutil.which("git")), ("gh", None), ("tmux", None)): + answers = repr(TMUX_LIVE) if tool == "tmux" and tmux_live else "None" + (where / tool).write_text(_ENV_LOGGER.replace("REAL_TOOL", repr(real)) + .replace("STATE_DIR", repr(str(e.state))) + .replace("TMUX_ANSWERS", answers), encoding="utf-8") + (where / tool).chmod(0o755) + return where + + +def tool_calls(where: Path, tool: Optional[str] = None) -> List[dict]: + log = where / "tools.log" + calls = [json.loads(l) for l in log.read_text(encoding="utf-8").splitlines() if l] if log.exists() else [] + return [c for c in calls if tool is None or c["tool"] == tool] + + +def comment_calls(where: Path) -> List[dict]: + """The fake `gh`'s comment calls, `issue comment` or `pr comment`: the + tail's `gh` (`gh_comment`), never Rule 1's reads (see the section's + head; T248a).""" + return [c for c in tool_calls(where, "gh") if c["argv"][:1] in (["issue"], ["pr"]) + and c["argv"][1:2] == ["comment"]] + + +def typed_calls(where: Path) -> List[dict]: + """The fake `tmux`'s typing, `send-keys` (`guard_type`): the tail's + `tmux`, never its reads before the seam, `display-message` for the + asking window and the bound window's session and command (T248b).""" + return [c for c in tool_calls(where, "tmux") if c["argv"][:1] == ["send-keys"]] + + +def register_writes(e: Estate) -> Path: + return e.state / "openRepoTools" / "register-writes" + + +def seam_outs(e: Estate) -> Dict[str, str]: + return {str(p): p.read_text(encoding="utf-8", errors="replace") + for p in register_writes(e).rglob("seam.out")} if register_writes(e).exists() else {} + + +@CLIENT +def test_a_writer_prints_its_output_once_however_many_attempts_it_made(estates, request): + """Test 4's burst of ten: each committed writer's success text, claim's + `register seq ` line, is printed exactly once, however many attempts + it made, and its stderr no line twice (only the deciding attempt's). The + line is a `note`, on stderr (T421; T243h). The row's oracle is each + committed writer's (Y TP:6852-6854; TP:6925-6927 at `0c3750a5`), so a + writer may end 75 with nothing written, as `burst_ends` checks (T248d). + + Its record counts each writer's attempts by its snapshots alone, + `burst_calls`' `s` calls, and `reads` its `r` and `m` calls, the detached + render's reads among them, as the burst record does (T243q). Read after + the burst's own record, it can hold more render reads than that one, + never more attempts. UNTIL T248e `attempts` here counted both, so an + older figure is `attempts + reads` (lane 2's RV20, finding 9; T248e).""" + e = estates() + writers, _ = burst(e, [f"{KEY}#{400 + k}" for k in range(10)], 200, node=request.node) + for w in burst_ends(e, writers): + assert len(SEQ_SAID.findall(w.err)) == 1, f"{w.lane} printed its success text not once: {w.err!r}" + lines = [l for l in w.err.splitlines() if l.strip()] + assert len(lines) == len(set(lines)), f"{w.lane}'s stderr repeats an attempt's lines: {w.err!r}" + kinds = {w.lane: [call[0] for call in burst_calls(w)[0].split()] for w in writers} + record("output-once", attempts={lane: k.count("s") for lane, k in kinds.items()}, + reads={lane: k.count("r") + k.count("m") for lane, k in kinds.items()}) + + +@CLIENT +def test_a_refused_writer_replays_its_output_then_the_refusal(estates): + """Test 4's case (a): the writer refused at its commit prints its + buffered output, then the refusal, last, naming the holder, and no + success text. The success text, claim's `register seq ` line, is a + `note`, on stderr (T421), so stderr is where its absence is read + (T248c).""" + e = estates() + barrier = Barrier(e.root, 2) + writers = [e.launch(lane, claim(OBJ, "--no-github"), arms=[WINDOW], barrier=barrier) for lane in (LA, LB)] + barrier.release() + assert sorted(w.wait() for w in writers) == [0, 2] + win, lose = sorted(writers, key=lambda w: w.code) + assert not SEQ_SAID.search(lose.err), f"the refused writer printed success text: {lose.err!r}" + lines = [l for l in lose.err.splitlines() if l.strip()] + assert lines and win.lane in lines[-1], f"the refusal naming {win.lane} is not last: {lose.err!r}" + assert len(lines) == len(set(lines)), f"the refused writer's stderr repeats lines: {lose.err!r}" + + +@CLIENT +@pytest.mark.parametrize("effect", ["tmux", "gh"]) +def test_a_side_effect_runs_once_after_commit_and_never_for_a_refusal(estates, effect): + """[gh]: two claimants on O; the fake `gh` comments once, by the + committed claim, its body citing its seq, never by the refused one + (Rule 1's reads, which each claimant makes before the seam, are not + counted; T248a). [tmux]: two `request-handoff`s for lane C, which is + bound to a window this tmux server holds live; the fake `tmux` types + (`send-keys`) once per committed request, naming its requester, after + its COMMIT (the journal holds it when `tmux` runs), and never for an + attempt refused at its commit, whose child runs no tail. + + [tmux] as the engine does it (T248b; N2-17b's reading 3): a request for a + bound or already requested lane is taken, so both requests may commit, + and the journal, not the exit, says which did. Each runs `--no-wait`, so + its wait ends at once, 2, "THE WAIT ENDED WITH NOTHING", after its + commit. The reads `tmux` answers before the seam (`display-message` for + the asking window, the bound window's session and its command) are not + counted: only `send-keys`, and the fake answers them from `TMUX_LIVE`. + + After COMMIT: when the call runs, a `seam.out` reads `tail`, so an + attempt has taken `committed`, and none reads other than `tail` or + `none`: the refused claimant's abandoned attempt may have written its own + `none` by then, and the logger sees every `seam.out` under + `register-writes/` (T248a).""" + e = estates() + where = loggers(e, tmux_live=effect == "tmux") + barrier = Barrier(e.root, 2) + if effect == "gh": + writers = [e.launch(lane, claim(OBJ), arms=[WINDOW], barrier=barrier, first=[where]) + for lane in (LA, LB)] + else: + writers = [e.launch(lane, ["request-handoff", LC, "--session", session_of(lane), "--no-wait"], + arms=[WINDOW], barrier=barrier, first=[where]) for lane in (LA, LB)] + barrier.release() + for w in writers: + w.wait() + grew = e.journal_after() + if effect == "gh": + committed = [w.lane for w in writers if w.code == 0] + else: + assert {w.code for w in writers} <= {0, 2}, [(w.lane, w.code, w.err[-300:]) for w in writers] + committed = [r["lane"] for r in grew if r["verb"] == "request-handoff"] + assert len(committed) == len(grew) >= 1, ([(w.lane, w.code, w.err[-300:]) for w in writers], grew) + calls = comment_calls(where) if effect == "gh" else typed_calls(where) + assert len(calls) == len(committed), f"{effect} ran {len(calls)} times for {len(committed)} commits: {calls}" + if effect == "gh": + assert all(str(grew[0]["seq"]) in SEQ_SAID.findall(c["body"] or "") for c in calls), calls + else: + typed = sorted(lane for lane in (LA, LB) for c in calls if session_of(lane) in " ".join(c["argv"])) + assert typed == sorted(committed), f"tmux typed for {typed}, the journal's requests are {committed}: {calls}" + for call in calls: + said = [text.strip() for text in call["seams"].values()] + assert "tail" in said and set(said) <= {"tail", "none"}, f"{effect} ran before COMMIT: {call}" + + +#: The test copy of `lanes-edit.sh` whose writes reach the seam twice: its +#: `commit_publish` (T414's seam) is renamed and called twice by a wrapper. +TWICE = ('commit_publish() { commit_publish_t248_once "$@" || return; ' + 'commit_publish_t248_once "$@"; }\n') + + +@CLIENT +def test_a_second_reach_of_the_seam_in_one_run_exits_2(estates): + """One run, one journal entry: an arm the test copy makes reach the seam + twice exits 2, "a second register write in one run", naming the first + seq, and the journal holds that one entry.""" + e = estates() + text = e.lanes_edit.read_text(encoding="utf-8") + assert text.count("\ncommit_publish() {") == 1, "lanes-edit.sh has no commit_publish, T414's seam" + twice = e.bindir / "lanes-edit-twice.sh" + twice.write_text(text.replace("\ncommit_publish() {", "\n" + TWICE + "commit_publish_t248_once() {", 1), + encoding="utf-8") + w = e.run(LA, ["append-line", f"NOTE {EM} lane {LA}: one run, two reaches (T248)"], script=twice) + grew = e.journal_after() + assert w.code == 2, f"exit {w.code}, not 2: {w.err[-600:]}" + assert len(grew) == 1, grew + assert "a second register write in one run" in w.err and str(grew[0]["seq"]) in w.err, w.err + + +@CLIENT +@pytest.mark.parametrize("case", ["outside-register-writes", "wrong-mode", "other-parent"]) +def test_the_scratch_flag_is_honoured_only_for_its_own_attempt_directory(estates, case): + """A write run with `LANES_REGISTER_SCRATCH` exported, naming a directory + outside `/register-writes/`, one of mode 0755, or one whose + `parent.pid` is not the run's parent, does not take it as the flag: the + shim's log shows the run's own snapshot and commit calls, and it + commits.""" + e = estates() + base = e.root / "elsewhere" if case == "outside-register-writes" else register_writes(e) + attempt = base / "attempt-t248" + attempt.mkdir(parents=True) + attempt.chmod(0o755 if case == "wrong-mode" else 0o700) + (attempt / "parent.pid").write_text("1\n" if case == "other-parent" else f"{os.getpid()}\n", + encoding="utf-8") + w = e.run(LA, ["append-line", f"NOTE {EM} lane {LA}: the flag refused, {case} (T248)"], + extra={"LANES_REGISTER_SCRATCH": str(attempt)}) + assert w.code == 0, f"[{case}] exit {w.code}: {w.err[-600:]}" + assert w.snapshot_calls() and w.commit_calls(), f"[{case}] the run made no snapshot or commit call" + assert len(e.journal_after()) == 1 + + +@CLIENT +def test_the_scratch_flag_never_reaches_git_gh_or_tmux(estates): + """The child unsets `LANES_REGISTER_SCRATCH` at once, so the logged + environments of the `git` wrapper, the fake `gh` and the fake `tmux` + never hold it, over a committed claim and a refused one.""" + e = estates() + where = loggers(e) + barrier = Barrier(e.root, 2) + writers = [e.launch(lane, claim(OBJ), arms=[WINDOW], barrier=barrier, first=[where]) for lane in (LA, LB)] + barrier.release() + assert sorted(w.wait() for w in writers) == [0, 2] + calls = tool_calls(where) + assert {c["tool"] for c in calls} >= {"git", "gh"}, f"the loggers saw too little: {calls[:3]}" + held = [c for c in calls if c["scratch"]] + assert held == [], f"LANES_REGISTER_SCRATCH reached {sorted({c['tool'] for c in held})}" + + +@CLIENT +@pytest.mark.parametrize("case", ["parent-gone", "cap"]) +def test_a_child_at_the_seam_stops_with_none_and_runs_no_tail(estates, case): + """With its parent killed ([parent-gone]) or stopped by SIGSTOP at the + cap (`LANES_REGISTER_SEAM_CAP_S=3`, [cap]) while it waits at the seam, + the child writes `seam.out` as `none` and runs no tail: the fake `gh` + makes no comment, and nothing is written. The parent's commit call is + held by the shim and never sent. Rule 1's reads, made before the seam, + are not the tail and are not counted (T248a).""" + e = estates() + where = loggers(e) + fifo = e.root / "hold.fifo" + os.mkfifo(str(fifo)) + w = e.launch(LA, claim(OBJ), first=[where], extra={"LANES_REGISTER_SEAM_CAP_S": "3"}, + arms=[{"mode": "hold", "match": "commit_write", "call": 1, "fifo": str(fifo)}]) + held = hold_point(fifo, w) + try: + os.kill(w.proc.pid, 9 if case == "parent-gone" else 19) + deadline = time.monotonic() + 20 + while "none" not in "".join(seam_outs(e).values()): + assert time.monotonic() < deadline, f"[{case}] no seam.out reads none: {seam_outs(e)}" + time.sleep(0.1) + time.sleep(0.5) + assert comment_calls(where) == [], f"[{case}] the tail ran: {comment_calls(where)}" + assert e.journal_after() == [] + finally: + if case == "cap": + os.kill(w.proc.pid, 18) + assert held is not None # never closed: the held commit call is never sent + + +@CLIENT +@pytest.mark.parametrize("decision", ["committed", "abandon"]) +def test_seam_out_is_written_once_before_the_child_acts(estates, decision): + """`seam.out` is written once, with noclobber: `tail` before the child + acts on `committed` (the fake `gh`'s comment, the claim's tail, sees + `tail`), and `none` before it acts on `abandon` (the refused claim's + child runs no tail). No `seam.out` holds anything else. Only `gh`'s + comment calls are read, never Rule 1's reads (T248a).""" + e = estates() + where = loggers(e) + if decision == "committed": + w = e.run(LA, claim(OBJ), first=[where]) + assert w.code == 0, w.err[-600:] + gh = comment_calls(where) + assert gh and all(set(c["seams"].values()) == {"tail"} or set(c["seams"].values()) == {"tail\n"} + for c in gh), f"gh's comment saw no seam.out reading tail: {gh}" + else: + assert e.run(LB, claim(OBJ, "--no-github")).code == 0 + w = e.run(LA, claim(OBJ), first=[where]) + assert w.code == 2, w.err[-600:] + assert comment_calls(where) == [], "the abandoned attempt ran its tail" + for path, said in seam_outs(e).items(): + assert said.strip() in ("tail", "none") and said.count("\n") <= 1, f"{path}: {said!r}" + + +# ============================================================ PHASE N: AT-F5 group A, the act's client half (T233) +# +# AT-F5 (TP:5818-5946; Y:5567-5624): lane 4's L4-A2, group A, which is T410's +# `act-begin` with `--rebase` and the client's checks on `LANES_ACT` and +# `LANES_EXPECT`, and T411's `canon-lane --walk`. They are written to the +# test plan's text and B14's T410 and T411 bullets: L4-A2 is in no bundle +# lane 4 has shared (its share folder holds T409, T413 and T419 only), and +# neither verb is in line R's merge10. +# +# Off primary, and `canon-lane --walk`, they run in files mode with no engine, +# on `test_lane_start_claude_current`'s `Sandbox` (`LANES_INDEX=off`) with a +# `psql` first on `PATH` that only logs its calls, so they are red at the base +# by their own assertions. The others run on test 4's estate at `primary` +# (T243's `Estate`), each writer with its own `psql` log, and skip without +# PostgreSQL 16. Every case FAILS UNTIL L4-A2 (lane 4's T410 and T411) and +# L1-A9 land. + +WALK_A, WALK_B = "repoWA-1", "repoWB-1" +#: The walk's alias lines on test 4's estate: `a` to `b`, `b` to `c` (LC, which has a row). +WALK_ALIASES = f"{WALK_A}\t{WALK_B}\t2026-10-01T00:00:00Z\n{WALK_B}\t{LC}\t2026-10-01T00:00:00Z\n" +_ACT_ID = re.compile(r"^([0-9a-f]{32})@") + + +class FilesBox(Sandbox): + """A files-mode workstation (`Sandbox`) with rows of its own, an alias + table, and a `psql` that logs each call and answers nothing.""" + + def __init__(self, root: Path, rows: Sequence[str] = (), aliases: Sequence[Tuple[str, str]] = ()) -> None: + super().__init__(root) + self.psql_log = root / "psql.log" + _write(self.fakebin / "psql", f'#!/usr/bin/env bash\nprintf "%s\\n" "$*" >> "{self.psql_log}"\nexit 2\n') + lanes = self.wip / "lanes" + with open(lanes / "LANES.md", "a", encoding="utf-8") as handle: + for lane in rows: + handle.write(f"| `{lane}` | harness `{session_of(lane)}` | Eagle / test / brett " + f"| 2026-10-01T00:00Z | none | handoffs/{lane}.md " + f"| LIVE {DOT} 2026-10-01T00:00:00Z {DOT} working |\n") + if aliases: + (lanes / "aliases.tsv").write_text( + "# old\tnew\tutc\n" + "".join(f"{old}\t{new}\t2026-10-01T00:00:00Z\n" for old, new in aliases), + encoding="utf-8") + if rows or aliases: + self.git("-C", str(self.wip), "add", "-A") + self.git("-C", str(self.wip), "commit", "-q", "-m", "T233's register") + self.git("-C", str(self.wip), "push", "-q", "origin", "main") + self.git("-C", str(self.ws), "pull", "-q", "--ff-only") + + def edit(self, *argv: str, env: Optional[dict] = None) -> subprocess.CompletedProcess: + full = dict(self.env, **(env or {})) + return subprocess.run([str(self.bin / "lanes-edit.sh"), *argv], env=full, cwd=str(self.lane_dir), + capture_output=True, text=True, timeout=120, check=False, + stdin=subprocess.DEVNULL) + + def psql_calls(self) -> List[str]: + return self.psql_log.read_text(encoding="utf-8").splitlines() if self.psql_log.exists() else [] + + +def test_act_begin_prints_its_base_and_touches_no_database_off_primary(tmp_path): + """Off primary `act-begin` prints `@files`, the id 32 lower-case + hex digits, new for every act, and touches no database: the `psql` on + `PATH` is never called (TP:5828-5830; Y:5571-5575; T410).""" + box = FilesBox(tmp_path / "box") + first, second = box.edit("act-begin"), box.edit("act-begin") + for r in (first, second): + assert r.returncode == 0, f"act-begin exited {r.returncode}: {r.stderr[-600:]}" + assert re.fullmatch(r"[0-9a-f]{32}@files\n", r.stdout), f"act-begin printed {r.stdout!r}" + assert first.stdout != second.stdout, "two acts were given one id" + assert box.psql_calls() == [], f"act-begin off primary called psql: {box.psql_calls()}" + + +#: `canon-lane --walk`'s cases (TP:5866-5873): the rows, the alias lines, the +#: typed name, and the names the walk prints. +WALKS = { + "two-hops": ((LC,), ((WALK_A, WALK_B), (WALK_B, LC)), WALK_A, [WALK_A, WALK_B, LC]), + "no-alias": ((LC,), (), LC, [LC]), + "refused": (("repoWR-1", "RepoWR-1"), (), "repoWR-1", []), +} + + +@pytest.mark.parametrize("case", list(WALKS)) +def test_canon_lane_walk_prints_each_hop_with_canon_lanes_exit_code(tmp_path, case): + """[two-hops]: alias lines lead `a` to `b` and `b` to `c`, which has a + row; `canon-lane --walk a` prints `a`, `b` and `c`, one per line, the typed + name first, and exits as `canon-lane a` does. [no-alias]: the lane alone. + [refused]: where `canon-lane` refuses (two rows that differ only by case, + its 2), the walk exits with the same code and prints nothing on stdout + (T411).""" + rows, aliases, typed, names = WALKS[case] + box = FilesBox(tmp_path / "box", rows, aliases) + plain = box.edit("canon-lane", typed) + walk = box.edit("canon-lane", "--walk", typed) + assert (plain.returncode != 0) == (case == "refused"), f"[{case}] canon-lane exited {plain.returncode}" + assert walk.returncode == plain.returncode, ( + f"[{case}] the walk exited {walk.returncode}, canon-lane {plain.returncode}: {walk.stderr[-600:]}") + assert walk.stdout.splitlines() == names, f"[{case}] the walk printed {walk.stdout!r}, not {names}" + if case == "refused": + # The refusal is canon-lane's own (its two-rows note), not a refusal of the flag. + assert "differ only by case" in walk.stderr, f"[{case}] not canon-lane's refusal: {walk.stderr[-600:]}" + + +def test_canon_lane_walk_whose_register_read_fails_off_primary_exits_1_and_prints_nothing(tmp_path): + """Off primary, where the register cannot be read, `canon-lane --walk` + exits 1 and prints nothing on stdout: a read that failed is never "no + row" (R22; Amendment 7(d)), so the walk lists no name from a register + nobody read (lane 3, #165 6827722997, Q-A and item 3; lane 4's E5; + T233b). A failed files read is never cached, so `canon_lane`, which + still reads that failure as no row, answers, and the walk's own read of + the register fails again. `canon-lane`'s own answer is not asserted: its + reading is a follow-up item for its own ruling. + + The register is made unreadable as `test_lanes_index`'s `_unreadable` + makes a published object unreadable: the loose object of `lanes/LANES.md` + at `origin/main`, which the checkout wrote itself, is moved aside, so its + `git show` fails while every other object reads, the alias table among + them. First, with the register readable, the same walk prints its three + names, so the 1 is the failed read's. + + It fails until lane 4's T411 lands with its `b637ce0d`, which ends the + walk with a failed register read's own status, the 1 here (#165 6814837872, + Question 126). Without T411, as on line R before merge44 (round 6d's + census), `canon-lane --walk` is the usage 64, "canon-lane takes one + lane" (lane 1, #165 6837462215 and 6850698453); with + only T411's first commit, `2ee320e1`, as at pair-m33a `8ba10b4e`, the + walk still reads that failure as no row and exits 0 with its three names + (lane 4's pair-m33a CI, s2 15, read by lane 2's triage, #165 6838261062; + T233c, T233d, T233e).""" + rows, aliases, typed, names = WALKS["two-hops"] + box = FilesBox(tmp_path / "box", rows, aliases) + walk = box.edit("canon-lane", "--walk", typed) + assert (walk.returncode, walk.stdout.splitlines()) == (0, names), ( + f"with the register readable the walk exited {walk.returncode} and printed {walk.stdout!r}: " + f"{walk.stderr[-600:]}") + sha = box.git("-C", str(box.wip), "rev-parse", f"origin/main:{LANES_MD}").strip() + blob = box.wip / ".git" / "objects" / sha[:2] / sha[2:] + assert blob.is_file(), f"the register's blob {sha} is not a loose object of {box.wip}" + blob.rename(blob.with_name(blob.name + ".aside")) + walk = box.edit("canon-lane", "--walk", typed) + assert walk.returncode == 1, ( + f"the walk over a register that could not be read exited {walk.returncode}, not 1: {walk.stderr[-600:]}") + assert walk.stdout == "", f"the walk over a register that could not be read printed {walk.stdout!r}" + + +@CLIENT +@pytest.mark.parametrize("case", ["present", "switch-at-files", "snapshot-fails"]) +def test_act_begin_on_primary_prints_id_seq_and_generation(estates, case): + """[present]: on primary `act-begin` prints 32 lower-case hex digits, `@`, + the head seq with no leading zero, `:` and the switch's generation as a + lower-case uuid, and the shim's log holds its one snapshot call. + [switch-at-files] (the cutover aborted by `switch_to_files`, which aborts + only a cutover left unconfirmed, the marker `adding`, so this estate's + cutover gets no `record_marker`) and [snapshot-fails] (the shim fails the + call): exit 69, nothing on stdout (TP:5874-5878; T410).""" + e = estates(confirmed=case != "switch-at-files") + arms = [] + if case == "switch-at-files": + aborted = e.c.call(ABORT_SQL, client=e.c.client(), seq=e.c.head(), lane=OPERATOR, ws=WS) + assert aborted.returncode == 0 and e.c.switch()["mode"] == "files", aborted.stderr + elif case == "snapshot-fails": + arms = [{"mode": "fail-call", "call": 1}] + w = e.run(LA, ["act-begin"], arms=arms) + if case != "present": + assert (w.code, w.out) == (69, ""), f"[{case}] exit {w.code}, stdout {w.out!r}: {w.err[-600:]}" + return + gen = e.c.switch()["generation"] + assert w.code == 0, f"exit {w.code}: {w.err[-600:]}" + assert re.fullmatch(r"[0-9a-f]{32}@" + str(e.c.head()) + ":" + re.escape(gen) + r"\n", w.out), ( + f"act-begin printed {w.out!r}, not @{e.c.head()}:{gen}") + assert len(w.calls()) == 1, f"act-begin made {len(w.calls())} calls, not its one snapshot" + + +@CLIENT +def test_act_begin_rebase_keeps_the_id_and_takes_the_new_base(estates): + """`act-begin`; a rival touches one of the act's scopes; `act-begin + --rebase` prints the same id with the head seq then and the switch's + generation; the act's write on that scope commits; a second rival entry + after the new base on the same scope still refuses it with 2 + (TP:5850-5858; Y:5591-5595; T410).""" + e = estates() + act = act_begin(e) + scope = f"objects:{LA.lower()}" + other = {"LANES_SESSION": "dddddddd-0233-4000-8000-00000000d233"} + assert e.run(LA, act_write(e, "objects", tag="rival-1"), extra=other).code == 0 + rebased = e.run(LA, ["act-begin", "--rebase"], extra={"LANES_ACT": act}) + gen = e.c.switch()["generation"] + assert rebased.code == 0, f"--rebase exited {rebased.code}: {rebased.err[-600:]}" + assert rebased.out == f"{_ACT_ID.match(act).group(1)}@{e.c.head()}:{gen}\n", ( + f"--rebase printed {rebased.out!r}; the act was {act}, the head is {e.c.head()}") + new = rebased.out.strip() + w = e.run(LA, act_write(e, "objects", tag="after-rebase"), extra=acted(new, scope)) + assert w.code == 0, f"the act's write after its rebase exited {w.code}: {w.err[-600:]}" + assert e.run(LA, act_write(e, "objects", tag="rival-2"), extra=other).code == 0 + late = e.run(LA, act_write(e, "objects", tag="after-rival-2"), extra=acted(new, scope)) + assert late.code == 2 and f"expectation: {scope} changed" in late.err, (late.code, late.err[-600:]) + + +@CLIENT +@pytest.mark.parametrize("case", ["malformed", "files-base", "generation-changed"]) +def test_act_begin_rebase_refuses_with_2(estates, case): + """`act-begin --rebase` refuses with 2, printing nothing and writing + nothing, where `LANES_ACT` is malformed (the old `@`), where its + base is `files` on primary, and where its generation is not the switch's + (TP:5859-5861; T410).""" + e = estates() + act = {"malformed": f"{'a' * 32}@{e.c.head()}", "files-base": f"{'a' * 32}@files", + "generation-changed": f"{'a' * 32}@{e.c.head()}:{uuid.uuid4()}"}[case] + state = e.c.state() + w = e.run(LA, ["act-begin", "--rebase"], extra={"LANES_ACT": act}) + assert (w.code, w.out) == (2, ""), f"[{case}] exit {w.code}, stdout {w.out!r}: {w.err[-600:]}" + assert e.c.state() == state + + +#: The client's checks before the first snapshot (TP:5879-5887; Y:5597-5605): +#: each case's `LANES_ACT` and `LANES_EXPECT` pairs, `None` for unset, and the +#: variable the refusal names. `{act}` is a well-formed base from `act-begin`. +BAD_ACTS = { + "malformed": ([(f"{'a' * 32}@1", "objects:{lane}"), (f"{'a' * 32}@1:{'A' * 8}-0000-4000-8000-{'0' * 12}", ""), + ("{id}@01:{gen}", "")], "LANES_ACT"), + "expect-without-act": ([(None, "objects:{lane}"), ("", "objects:{lane}")], "LANES_EXPECT"), + "bad-scope": ([("{act}", "*"), ("{act}", "row:*"), ("{act}", "lane:{lane}")], "LANES_EXPECT"), + "files-base": ([("{id}@files", "objects:{lane}")], "LANES_ACT"), +} + + +@CLIENT +@pytest.mark.parametrize("case", list(BAD_ACTS)) +def test_the_client_refuses_a_bad_act_with_2_before_any_call(estates, case): + """On primary a write whose `LANES_ACT` is malformed (the old form, an + upper-case uuid, a seq with a leading zero), whose `LANES_EXPECT` is set + with no `LANES_ACT`, whose scope is outside the six forms (`*` and `row:*` + among them), or whose base is `files` (the text naming `@files`) + exits 2 naming the variable, with the shim's log empty and nothing + written (TP:5879-5887; T410).""" + e = estates() + act = act_begin(e) + ident, gen = _ACT_ID.match(act).group(1), e.c.switch()["generation"] + pairs, variable = BAD_ACTS[case] + for raw_act, raw_expect in pairs: + state = e.c.state() + extra = {"LANES_EXPECT": raw_expect.format(lane=LA.lower())} + if raw_act is not None: + extra["LANES_ACT"] = raw_act.format(act=act, id=ident, gen=gen) + w = e.run(LA, act_write(e, "objects", tag=case), extra=extra) # unset: the estate never carries it + assert w.code == 2, f"[{case}] {extra}: exit {w.code}: {w.err[-600:]}" + assert variable in w.err, f"[{case}] {extra}: the refusal does not name {variable}: {w.err[-600:]}" + if case == "files-base": + assert f"{ident}@files" in w.err, w.err[-600:] + assert w.calls() == [], f"[{case}] {extra}: a call was made: {w.calls()}" + assert e.c.state() == state, f"[{case}] {extra}: the register changed" + + +# ============================================================ PHASE W: AT-F5 group B, acts racing (T252) +# +# F5 (D5; lane 4's section, "The acts and their expectations"): an act runs +# `act-begin`, exports `LANES_ACT=@:`, and each write +# carries `LANES_EXPECT=" ..."`; the commit refuses with LR009, the +# writer with 2, `expectation: changed at seq by /`, +# when an entry of another act touched a listed scope after the base. The two +# race functions run lane 4's acts to each write of its tables, on a world of +# their own (T252b, below). The other cases run on test 4's estate (T243's +# `Estate`), where the writes are `append-line` of a row-shaped line +# (`row:`) and `log` (`objects:`), a reading of lane 1's derived keys. +# The five cases that read lane 4's tools' text now run their flows on +# T252b's world (lane 3's batch 7, item 5): `[lane-end-gate]` and +# `[skill-step-4]` (T252f) and `[lane-start-reread]` (T252i) at the three +# conditions below, and the two launches (T252h) after them. They FAIL UNTIL +# L1-A6, L4-A2 (T410), L4-B3 and L4-B14 land; without PostgreSQL 16 the RE +# cases skip. + +ACT_RE = re.compile(r"^[0-9a-f]{32}@[1-9][0-9]*:" + UUID_RE + r"$|^[0-9a-f]{32}@0:" + UUID_RE + r"$") + + +def act_begin(e: Estate, lane: str = LA) -> str: + w = e.run(lane, ["act-begin"]) + act = w.out.strip() + assert w.code == 0 and ACT_RE.match(act), f"act-begin exited {w.code}, printed {w.out!r}: {w.err[-300:]}" + return act + + +def act_write(e: Estate, scope: str, lane: str = LA, tag: str = "") -> List[str]: + """The act's write on `scope`, `row` or `objects`. The `objects` write is + an object-kind line, `OPENED` on `opensoft/r9#77`: `NOTED` has been a + lane-kind line since Amendment 13(b) (`aa6cf53d`), so `log NOTED` on an + object is refused with 2 before any write (T252l).""" + if scope == "row": + return ["append-line", f"| `{lane}` | harness `{session_of(lane)}` | {W_WS} / test / brett " + f"| 2026-05-01T00:00Z | none | handoffs/{lane}.md | T252 row {tag} |"] + return ["log", "OPENED", f"{KEY}#77", f"T252 {tag}"] + + +def acted(act: str, *scopes: str) -> dict: + return {"LANES_ACT": act, "LANES_EXPECT": " ".join(scopes)} + + +# ---------------------------------------- the race, on each write of lane 4's tables (T252b) +# +# TP:5848-5853 parametrise the race "for each write of lane 4's tables", and +# lane 3's batch 7 (ruling 5) takes one id per write and drops `[row|objects]`, +# since T233's group A cases hold the commit's half. So each case runs the act, +# lane 4's own tool, to that write on a workstation at `primary`; the shim +# holds that write's commit call; a rival from a second session commits; and +# the hold is let go (TP:5854-5856). The rival writes the lane's own `PAUSED` +# in its log, as a forced release does (Y:5673): a lane-kind line, `bind:`, +# which every write of the tables lists (Y:5640-5680), W1's new lane among +# them (`new-lane`, below). The held case's rival writes that `PAUSED` (or, +# for W1, a row line) for another lane, which no write of the act lists, +# whatever the act's own rival is (`held_rival`). +# +# THE RIVAL LEAVES THE VERB'S ANSWER AS IT WAS (lane 3's ruling, option (b), +# #165 6860075071). It commits after the attempt's snapshot, so the held +# commit has lost its turn (LR001, Y:2570), and a new attempt runs the verb +# again, on the register with the rival in it (Y:2546-2552, Y:2602). Where +# the verb itself decides on a scope the act lists, a rival that changes its +# answer ends the act before any commit, and LR009 is never met. So: +# - W8's `request-handoff` reads the lane's binding, and a `PAUSED` makes it +# answer 8 (`7893339:lanes-edit.sh:31788-31791`), after which `lane-start` +# goes on (Y:5656). Its rival is the holder's `RESUMED` from the holder's +# own place, Falcon (`resumed`), which leaves the lane bound elsewhere; +# from the act's own window `request-handoff` would refuse first +# (`:31808-31810`). `lane`'s `request-handoff` takes the same rival. +# - `retire-rows` proves again that the row is dormant, and refuses 2 on a +# live session here and on any lane-kind line in its log +# (`7893339:lanes-edit.sh:24740-24837`). Its rival appends an id that no +# session record holds to the row's session cell, a write on `row:` +# that leaves the row dormant (`session-id`). +# - W1's `add-row` refuses 2 where its lane has a row, "already has a row +# ... (Amendment 15(a))" (`add_row_checks`, +# `44f487d1:lanes-edit.sh:15341-15361`), and reads no lane-kind line. So +# its rival is the new lane's own `PAUSED` in its log (`new-lane`), a +# `bind:` line that W1's `LANES_EXPECT` lists (`ls_expect_for`, +# `44f487d1:lane-start:4621`, called at `:4672`) and that leaves the lane +# without a row, so the retried `add-row` passes its checks and its +# commit meets LR009 (lane 3, #165 6869860086, taking 6860075071's rule; +# T252y). UNTIL T252y its rival was a row line for it, `row:`, and the +# retried `add-row` refused 2 before any commit call, so this id never +# reached LR009 (lane 4's R6J-A, #165 6869806372). +# +# THE WORLD is `test_lane_start_claude_current`'s `Sandbox` (fake `tmux`, +# `claude` and `gh`) holding the register N1's arms run on (lane 2's T217): +# repoA-1 PAUSED here, repoA-2 LIVE here, repoA-3 LIVE on Falcon, repoH-1 +# DORMANT, and no repoZ-1. It is imported into a clone of `tmpl` +# that `Clone.to_primary` brings to `primary`, with the flipped `lanes-index` +# beside `lanes-edit.sh`, `mode=primary` configured, and its `PATH` masked +# (`_masked_path`, its own bin spared its `lanes-index`), as test 4's estate is. +# Each of its five lanes has its own register checkout beside W, the +# `workspace.yaml` checkout `ws` (`ActWorld.lane_checkout`; lane 3's (119)). +# Readings: each act's argv (N1's arms at `2a057a6d`, with `--no-launch` for +# W1, `lane-end --retire-dormant` and `lane --request-handoff`); the write's +# commit call, found by a text its entry's JSON carries (its verb, or its +# line's kind, lane or state); the skill's step-4 block, the fenced block of +# its section 4 that opens with `act-begin`; and the guard's child, run as +# T502 writes its body, `guard-bind --body `, whose record's path T502 +# owns: the world writes that record, `mode=binding`, and gives the window +# the session it binds, live and named for its lane, as the guard's answer 2 +# leaves them for its child (`ActWorld.offer`, `guard_session`; T252q). +# +# `lane-end`'S CLOSE-OUT GATE (T252g): every `lane-end` act runs with +# `LANE_WORKTREES` set to a stand-in that prints nothing and exits 0, as +# `[lane-end-gate]`'s does, so the gate finds nothing left and W13 and W14 are +# reached. Without it the gate runs whatever `lane-worktrees` the host has: +# py-bench's own `/usr/local/bin/lane-worktrees`, and on a host with none an +# exit 127, on which `lane-end` refuses 1 before W13 (round 5, #165 +# 6810924785; OR12's report). `--retire-dormant` exits after `retire-rows`, +# before the gate (`lane-end:764-952` at V-round5), and never runs it. +# +# They FAIL UNTIL L1-A6, L4-A2 (T410, T411), L4-B3, L4-B5 and L4-B14 (T428 to +# T432) land; the skill's id also until T431's block, and the guard's until +# T502's `guard-bind` and T510's expectations on its `set-row-state`. Without +# PostgreSQL 16 each skips. + +#: The em dash as the entry's ASCII JSON spells it. +EM_JSON = json.dumps(EM)[1:-1] +#: The world's lanes (N1's arms) and their sessions. +R_PAUSED, R_HERE, R_FAR, R_DORMANT, R_NEW = "repoA-1", "repoA-2", "repoA-3", "repoH-1", "repoZ-1" +R_SID = {R_PAUSED: "aaaaaaaa-0002-4000-8000-00000000a002", R_HERE: "aaaaaaaa-0003-4000-8000-00000000a003", + R_FAR: "aaaaaaaa-0253-4000-8000-00000000a253", R_DORMANT: "aaaaaaaa-0254-4000-8000-00000000a254", + R_NEW: "aaaaaaaa-0255-4000-8000-00000000a255"} +#: repoH-1's state cell (T252j). `lane-end --retire-dormant ` retires +#: only DORMANT rows, "a row with no object log and no live session here" +#: (Amendment 19(a); `lanes-edit.sh lanes --closed`), and a cell in Amendment +#: 13's phrase whose word is not ENDED, RETIRED or MIGRATED keeps a row with +#: no log listed. So repoH-1 has no log and this cell, which is no phrase. +DORMANT_CELL = "parked long ago, before every lane had a log" +#: For each lane, a lane that none of its acts' writes lists (the held case). +R_OTHER = {R_PAUSED: R_FAR, R_HERE: R_FAR, R_FAR: R_HERE, R_DORMANT: R_FAR, R_NEW: R_FAR} +#: The id the `session-id` rival appends to a row's session cell: no session +#: record holds it, so it is live nowhere. +RIVAL_SID = "aaaaaaaa-0259-4000-8000-00000000a259" + + +def verb_is(verb: str) -> str: + return f'"verb":"{verb}"' + + +def lane_line(kind: str, lane: str) -> str: + return f"{kind} {EM_JSON} lane {lane}" + + +#: Each rival's kind (`ActWorld.rival`): the scope prefix and the verb of its +#: entry, which the refused write's `expectation:` line names (`commit_write`'s +#: LR009, `7893339:lanes-index:9981-9983`), and the kind the held case writes +#: for another lane. +#: `new-lane` is W1's (T252y): the `bind` rival's `PAUSED`, for a lane with no +#: row, whose held case keeps W1's row line for another lane, as before. +RIVALS = {"bind": ("bind", "log", "bind"), "row": ("row", "append-line", "row"), + "resumed": ("bind", "log", "bind"), "session-id": ("row", "append-session-id", "bind"), + "new-lane": ("bind", "log", "row")} + + +class ActWrite: + """One write of lane 4's tables: the act that makes it (`tool`, `argv`), + the lane it writes, its commit call (`match`, a text its entry carries, + the first commit call that carries it), the rival's kind (`rival`, one + of `RIVALS`), its entry's scope prefix and verb (`rival_scope`, + `rival_verb`) and the held case's rival (`held_rival`), and the act's + exit where lane 4's arm names one for a refused write (`code`; else only + never 0).""" + + def __init__(self, tool: str, argv: Tuple[str, ...], lane: str, match: str, rival: str = "bind", + code: Optional[int] = None, env: Optional[dict] = None) -> None: + self.tool, self.argv, self.lane, self.match = tool, argv, lane, match + self.rival, self.code, self.env = rival, code, env or {} + self.rival_scope, self.rival_verb, self.held_rival = RIVALS[rival] + + +_HANDOFF = ("--lane", R_HERE, "T252b's race") +#: TP:5848-5853's list, in its order: `lane-start`'s W1, W8 and `start-record` +#: (W2 to W7); `lane-handoff`'s late `log PAUSED --utc`, W9, W11 and W12; +#: `lane-end`'s W13, W14 and `retire-rows`; the handoff skill's step-4 writes; +#: the guard's answer 2, its child's `set-row-state` on the lane left; and +#: `lane`'s `request-handoff`. The codes: `start-record`'s and W1's 2 die 2 +#: (T428, "any 2 dies"); W8's arm passes the code (T428); `:2817` keeps 2 +#: (T429); W13 under `set -e` and W14's note arm exit with the writer's code +#: (T430); `lane`'s `2)` arm dies 2, as Y:5722 and Y:5740 say (T432; ERR-36; +#: `7893339:lane:1382-1386`; lane 3, #165 6860075071); the handoff skill's step-4 +#: stop ends 2 alone, its writer's code staying in that writer's own stderr +#: (T431; lane 3's question 4, #165 6835210568; T252n); and the guard's child +#: ends 0, its refused `set-row-state` a `note=`, since the bind landed (lane +#: 3's point 4(b), #165 6841918192; T252s). W1 runs with `LANE_START_SESSION_ID=0`, +#: minting off: where a transcript uuid is knowable, T428 carries W1 inside the +#: one `start-record --add-row` call, and no commit call is `add-row`'s; with +#: none, W1 stays the separate `add-row` that TP:5848 names +#: (`f9e1449c:lane-start:4315-4320`, `:4363-4364`, `:4562-4563`, `:4640-4644`; +#: lane 2's TR21, section 2a, option 1; T252t). `[lane-start-w1-start-record]` +#: is W1's twin with minting on, the form every new lane takes where a uuid is +#: knowable: the act mints one (`:4315-4320`), and W1's row rides in the one +#: `start-record --add-row` call, with the union of the expectations +#: (`7893339:lane-start:4589-4612`, `:4626-4630`, `:5094-5097`); its 2 dies 2 +#: (`:5129-5130`; T428; lane 2's RV23, SHOULD 2). Its rival is the lane's +#: `PAUSED` (`bind:`), not a row: the retried attempt runs the verb again, +#: and a row for the lane makes `add_row_checks` refuse it 2, "already has a +#: row", before LR009 (`7893339:lanes-edit.sh:15352-15354`, `:29789`; lane 3's +#: option (b), #165 6860075071). +ACT_WRITES = { + "lane-start-w1-add-row": ActWrite("lane-start", ("repoZ", "1", "--no-launch"), R_NEW, + verb_is("add-row"), rival="new-lane", code=2, + env={"LANE_START_SESSION_ID": "0"}), + "lane-start-w1-start-record": ActWrite("lane-start", ("repoZ", "1", "--no-launch"), R_NEW, + verb_is("start-record"), code=2), + "lane-start-w8-request-handoff": ActWrite("lane-start", ("repoA", "3", "--request-handoff", "--dir", + "{lane_dir}"), R_FAR, + verb_is("request-handoff"), rival="resumed", code=2), + "lane-start-start-record": ActWrite("lane-start", ("repoA", "1"), R_PAUSED, verb_is("start-record"), + code=2), + "lane-handoff-late-paused": ActWrite("lane-handoff", ("--late", "--at", "{utc}", *_HANDOFF), R_HERE, + lane_line("PAUSED", R_HERE), code=2), + "lane-handoff-w9-log-paused": ActWrite("lane-handoff", _HANDOFF, R_HERE, lane_line("PAUSED", R_HERE)), + "lane-handoff-w11-append-line": ActWrite("lane-handoff", _HANDOFF, R_HERE, verb_is("append-line")), + "lane-handoff-w12-set-row-state": ActWrite("lane-handoff", _HANDOFF, R_HERE, verb_is("set-row-state")), + "lane-end-w13-set-row-state": ActWrite("lane-end", (R_HERE,), R_HERE, verb_is("set-row-state"), code=2), + "lane-end-w14-log-ended": ActWrite("lane-end", (R_HERE,), R_HERE, lane_line("ENDED", R_HERE), code=2), + "lane-end-retire-rows": ActWrite("lane-end", ("--retire-dormant", "repoH", "--yes", "--reason=T252b's race"), + R_DORMANT, verb_is("retire-rows"), rival="session-id", + env={"LANES_LANE": R_HERE}), + "skill-step-4-writes": ActWrite("skill", (), R_HERE, lane_line("PAUSED", R_HERE), code=2), + "guard-answer-2-child": ActWrite("guard", ("guard-bind", "--body", R_SID[R_PAUSED]), R_HERE, + verb_is("set-row-state"), env={"LANES_LANE": R_PAUSED}), + "lane-request-handoff": ActWrite("lane", (R_FAR, "--request-handoff"), R_FAR, verb_is("request-handoff"), + rival="resumed", code=2), +} + + +class ActWorld(Sandbox): + """A workstation at `primary` whose lanes' acts run their own tools (see + the section's head).""" + + def __init__(self, root: Path, c: "Clone") -> None: + super().__init__(root) + self.c = c + for name in ("lane-end", "lane", "lane-handoff", "lanes"): + shutil.copy2(str(REPO / name), str(self.bin / name)) + (self.bin / name).chmod(0o755) + self.resolver() + seat = "Eagle / test / brett" + rows = [(R_PAUSED, seat, "2026-10-01T00:00Z", f"PAUSED {DOT} 2026-10-01T01:00:00Z {DOT} parked"), + (R_HERE, seat, "2026-10-01T02:00Z", f"LIVE {DOT} 2026-10-01T02:00:00Z {DOT} working"), + (R_FAR, "Falcon / test / brett", "2026-10-01T02:30Z", + f"LIVE {DOT} 2026-10-01T02:30:00Z {DOT} working elsewhere"), + (R_DORMANT, seat, "2026-10-01T03:00Z", DORMANT_CELL)] + lanes = self.wip / "lanes" + (lanes / "log").mkdir(exist_ok=True) + with open(lanes / "LANES.md", "a", encoding="utf-8") as handle: + for lane, ws, utc, state in rows: + handle.write(f"| `{lane}` | harness `{R_SID[lane]}` | {ws} | {utc} | none " + f"| handoffs/{lane}.md | {state} |\n") + for lane, ws, utc, state in rows: + if lane == R_DORMANT: + continue # a dormant row has no object log (Amendment 19(a); T252j) + host = ws.split(" ")[0] + text = (f"STARTED {EM} lane {lane}, session {R_SID[lane]}@{host}, {utc[:-1]}:00Z, " + f"lane:{lane} {ARROW} home opensoft/repoA; dir {self.lane_dir}; profile team-01a\n") + if state.startswith("PAUSED"): + text += (f"PAUSED {EM} lane {lane}, session {R_SID[lane]}@{host}, {state.split(' ')[2]}, " + f"lane:{lane} {ARROW} dir {self.lane_dir}; profile team-01a; agent claude; " + f"transcript {R_SID[lane]}\n") + (lanes / "log" / f"{lane}.md").write_text(text, encoding="utf-8") + self.git("-C", str(self.wip), "add", "-A") + self.git("-C", str(self.wip), "commit", "-q", "-m", "T252b's register") + self.git("-C", str(self.wip), "push", "-q", "origin", "main") + self.git("-C", str(self.ws), "pull", "-q", "--ff-only") + # Each world lane's own register checkout beside W (lane 3's (119)). + self.checkouts = [self.lane_checkout(lane) for lane in R_SID] + import_files(c, {str(p.relative_to(self.wip)): p.read_text(encoding="utf-8") + for p in sorted(lanes.rglob("*.md"))}) + assert c.to_primary() == 1, "T252b's cutover is not at seq 1" + flipped_copy(LANES_INDEX, self.bin, True) + conf = self.home / ".config" / "openRepoTools" / "lanes-index.conf" + conf.parent.mkdir(parents=True, exist_ok=True) + conf.write_text(f"mode=primary\nregister_url={c.db.url(WRITER_LOGIN)}\nreader_url={c.db.url()}\n", + encoding="utf-8") + conf.chmod(0o600) + # Its own bin keeps the flipped copy's `lanes-index` (T243e's `_masked_path`). + path = _masked_path(self.env["PATH"], spared={str(self.bin): ("lanes-index",)}) + self.env.pop("LANES_INDEX", None) + self.env.update(PATH=path, LANES_INDEX_CONFIG=str(conf), LANES_EDIT=str(self.bin / "lanes-edit.sh"), + XDG_STATE_HOME=str(root / "state"), FAKE_CC_PATH=str(self.resolved), + LANES_HOST="eagle", LANES_OS="linux", LANES_CONTAINER="none", LANES_IN_CONTAINER="0") + self._n = itertools.count(1) + self.writers: List[Writer] = [] + self.sessions: list = [] # the guard's live sessions (T252q), closed by `act_worlds` + self.record: Optional[Path] = None # the guard child's bind record (`offer`; T252s) + + def lane_checkout(self, lane: str) -> Path: + """``, lane `lane`'s own register checkout beside this world's W, + `ws`, which `workspace.yaml` names: `-lanes/`, a clone of + the world's origin, as `Estate.lane_checkout` makes one (lane 3's + (119); see `write_lanes_of`). Each of the world's lanes gets one, + made once the register is pushed, and each is pulled with W after a + later push (`handoff`), so it is at W's commit.""" + real = Path(os.path.realpath(self.ws)) + target = real.parent / f"{real.name}-lanes" / lane + target.parent.mkdir(parents=True, exist_ok=True) + self.git("clone", "-q", str(self.origin), str(target)) + return target + + def psql_dir(self, name: str, arms: Sequence[dict] = ()) -> Path: + return Estate.psql_dir(self, name, arms) # the same stamp and shim as test 4's + + def launch(self, name: str, command: Sequence[str], lane: str, arms: Sequence[dict] = (), + extra: Optional[dict] = None) -> Writer: + psql = self.psql_dir(f"{next(self._n):02d}-{name}", arms) + env = dict(self.env, LANES_LANE=lane, LANES_SESSION=R_SID[lane]) + env.update(extra or {}) + env["PATH"] = str(psql) + os.pathsep + env["PATH"] + proc = subprocess.Popen(list(command), cwd=str(self.lane_dir), env=env, stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8", + errors="replace", start_new_session=True) + writer = Writer(name, lane, proc, psql) + self.writers.append(writer) + return writer + + def act(self, write: ActWrite, arms: Sequence[dict]) -> Writer: + """The act that makes `write`, run by its own tool.""" + if write.tool == "skill": + # Step 4's block with the skill's prelude and steps 1 and 2's + # values, as the flow case runs it, `L` being this world's + # `lanes-edit.sh`: alone, the block's `"$L" act-begin` ran an + # empty command and exited 127 (T252p). + command, extra = skill_step_4_command(self, skill_prelude_block(), skill_step_4_block(), + write.lane, "race", self.bin / "lanes-edit.sh") + return self.launch("skill", command, write.lane, arms, dict(extra, **write.env)) + if write.tool == "guard": + # What the guard's answer 2 leaves its child: the record, which + # says `mode=binding`, and this window renamed for the lane it + # binds, with the session it binds live in it (`guard_session`, + # T203c's: a live process, its record naming `testsess:@1.%1`, and + # a stand-in `tmux` that answers for that window). With neither, + # `guard-bind --body` refused 2, "does not say mode=binding (it + # says nothing)"; with the record alone, `lane-start --bind` + # refused 2, "no live session record names this window" (T252q). + sid, to = write.argv[-1], dict(self.env, **write.env)["LANES_LANE"] + self.record = self.offer(sid, write.lane, to) + live = guard_session(self.home, self.root / "guard-session", self.lane_dir, window=to, + session_id=sid) + self.sessions.append(live) + extra = dict(live.env({"PATH": self.env["PATH"]}), LANES_SESSION=sid, **write.env) + return self.launch("guard", ["bash", str(self.bin / "lanes-edit.sh"), *write.argv], write.lane, + arms, extra) + utc = self.c.value("SELECT to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"');") + argv = [a.format(lane_dir=self.lane_dir, utc=utc) for a in write.argv] + extra = dict(write.env) + if write.tool == "lane-end": + extra.setdefault("LANE_WORKTREES", str(self.gate_stand_in())) + return self.launch(write.tool, [str(self.bin / write.tool), *argv], write.lane, arms, extra) + + def offer(self, sid: str, left: str, to: str) -> Path: + """The bind's record for session `sid`, as the name guard's answer 2 + leaves it for its child, `guard-bind --body ` (T502; + `contracts/bind-record.md`): `guard_offer`'s lines, the window's lane + `left` and the session's lane `to`, rewritten by `guard_answer`'s + choice 2 to `mode=binding` with the attempt's first keys, `mode` + first. It is `${CLAUDE_CONFIG_DIR:-$HOME/.claude}/lanes/offers/` + (`guard_offer_file`), 0600 in a 0700 `offers/`, beside the attempt's + empty `.bind.1.out`, which the outer's fork creates. No prefetch ran, + so the record has no `c`, `n_a` or `w_s_ms`, and `bound_s` is + `guard_bind_bound_s`'s default, 500. `guard_bind_body` reads `mode`, + `from`, `to`, `out` and `act`, binds `to` with `lane-start --no-launch + --bind`, and then sets `left`'s row (T252q). + + ITS `act` is the guard's base at the offer, `@:` + (tasks:1426; W:5378), which `guard_offer` writes after `pane`; here it + is taken from `act-begin` on primary (Y:5572), before the rival + commits. Without it the `set-row-state` on `left` sends `LANES_EXPECT` + with no `LANES_ACT`, which the client refuses with 2 before any commit + call (T510's part 5), so the race never reaches its hold (lane 3, + #165 6841918192, point 4(b); lane 5, #165 6842157774; T252s).""" + begun = self.launch("act-begin", ["bash", str(self.bin / "lanes-edit.sh"), "act-begin"], left) + begun.wait(60) + act = begun.out.strip() + assert begun.code == 0 and ACT_RE.match(act), ( + f"act-begin for the offer's act exited {begun.code}, printed {begun.out!r}: {begun.err[-300:]}") + offers = Path(self.env.get("CLAUDE_CONFIG_DIR") or Path(self.env["HOME"]) / ".claude") / "lanes" / "offers" + offers.mkdir(parents=True, exist_ok=True) + offers.chmod(0o700) + record = offers / sid + now = int(time.time()) + utc = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(now)) + bound_s = 500 + lines = ["mode=binding", f"from={left}", f"to={to}", f"session={to}", f"uuid={sid}", f"utc={utc}", + "window=", "pane=", f"act={act}", "bind=1", f"started_utc={utc}", f"bound_s={bound_s}", "bounded=yes", + f"deadline_epoch={now + bound_s + 5}", f"host={self.env['LANES_WORKSTATION']}", + f"container={self.env['LANES_CONTAINER']}", f"out={record}.bind.1.out", "detach=setsid"] + for path, text in ((record, "\n".join(lines) + "\n"), (Path(f"{record}.bind.1.out"), "")): + fd = os.open(str(path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, "w", encoding="utf-8") as handle: + handle.write(text) + return record + + def handoff(self, lane: str) -> None: + """The lane's handoff file, at the path its row names, committed and + published, as a live lane has one (T252h): `lane-handoff --restart` + respawns nothing over a handoff it could not refresh.""" + path = self.wip / "handoffs" / f"{lane}.md" + path.write_text(f"# handoff {EM} lane {lane}\n\nT252h's world: the lane's handoff before its /ctx.\n", + encoding="utf-8") + self.git("-C", str(self.wip), "add", "--", f"handoffs/{lane}.md") + self.git("-C", str(self.wip), "commit", "-q", "-m", f"handoff({lane}): T252h's world") + self.git("-C", str(self.wip), "push", "-q", "origin", "main") + self.git("-C", str(self.ws), "pull", "-q", "--ff-only") + for checkout in self.checkouts: + self.git("-C", str(checkout), "pull", "-q", "--ff-only") + + def gate_stand_in(self) -> Path: + """`lane-end`'s close-out gate for the race's acts (T252g): a + `LANE_WORKTREES` stand-in that logs its argv and `LANES_ACT`, prints + nothing and exits 0, so the gate passes and the act goes on to its + write (see the section's head).""" + return act_logger(self.root / "race-gate", "lane-worktrees") + + def rival(self, kind: str, lane: str) -> Writer: + """A second session's write (`RIVALS`): `bind`, the lane's own + `PAUSED` in its log (lane-handoff's W9 form); `new-lane`, the same + `PAUSED`, W1's, for a lane with no row, which it leaves without one + (T252y); `row`, a row line for the lane; `resumed`, the lane's + `RESUMED`, written by its own session + from the holder's place, Falcon (`LANES_WORKSTATION`, `LANES_HOST`), + which leaves the lane bound there: `lane_binding_scan` reads a + `RESUMED` as `bound` (`7893339:lanes-edit.sh:19808-19815`), at the + `host` that `binding_subfields_add` writes into its payload + (`:15157-15158`), and that host is not this one; `session-id`, an + id that no session record holds, `RIVAL_SID`, appended to the lane's + session cell by `append-session-id`, as `[lane-start-reread]`'s + holder appends one: a dormant row stays dormant, since `retire-rows` + refuses only an id that is live here (`7893339:lanes-edit.sh:24740-24751`).""" + edit = ["bash", str(self.bin / "lanes-edit.sh")] + extra: dict = {} + if kind in ("bind", "new-lane"): + argv = ["log", "PAUSED", f"lane:{lane}", ARROW, "T252b's rival", "the rival's release"] + elif kind == "resumed": + argv = ["log", "RESUMED", f"lane:{lane}", ARROW, "T252b's rival", "the holder resumes in place"] + extra = {"LANES_WORKSTATION": "Falcon", "LANES_HOST": "falcon"} + elif kind == "session-id": + argv = ["append-session-id", lane, R_SID[lane], f"{ARROW} harness `{RIVAL_SID}`", + "T252b's rival: an id no session holds"] + else: + argv = ["append-line", f"| `{lane}` | harness `{R_SID[lane]}` | Eagle / test / brett " + f"| 2026-11-01T00:00Z | none | handoffs/{lane}.md | T252b's rival row |"] + w = self.launch("rival", [*edit, *argv], lane, extra=extra) + w.wait() + return w + + +def skill_step_4_block() -> str: + """The handoff skill's step-4 block that opens with `act-begin` (T431; + Y:5678), the first fenced `bash` block of its section 4 that holds it.""" + text = (REPO / "skills" / "handoff" / "SKILL.md").read_text(encoding="utf-8") + section = text.split("\n## 4.", 1)[-1].split("\n## 5.", 1)[0] + blocks = re.findall(r"```(?:bash|sh)\n(.*?)```", section, re.S) + found = [b for b in blocks if "act-begin" in b] + assert found, "skills/handoff/SKILL.md's step 4 has no block that opens with act-begin (T431)" + return found[0] + + +@pytest.fixture() +def act_worlds(pg_cluster, tmp_path): + """T252b's worlds, each on a clone of `tmpl` of its own.""" + made: List[str] = [] + alive: List[ActWorld] = [] + + def make() -> ActWorld: + name = f"t252_{next(_NAMES)}" + pg_cluster.psql(f"CREATE DATABASE {name} TEMPLATE {W_TEMPLATE};") + made.append(name) + found = pg_cluster.psql(f"SELECT to_regclass('{REGISTER}.register_head') IS NOT NULL;", dbname=name) + assert found.stdout.strip() == "t", ( + f"{W_TEMPLATE} holds no {REGISTER} schema: lane 2's T227 builds it by the bootstrap") + pg_cluster.psql(f"REVOKE ALL ON DATABASE {name} FROM PUBLIC;\n" + f"GRANT CONNECT ON DATABASE {name} TO {CONNECT_TO};\n") + alive.append(ActWorld(tmp_path / f"world-{len(made)}", Clone(pg_cluster, name))) + return alive[-1] + + yield make + for world in alive: + for w in world.writers: + if w.ended is None and w.proc.poll() is None: + os.killpg(w.proc.pid, 9) + for live in world.sessions: + live.close() + for name in made: + pg_cluster.drop_database(name) + + +def race(world: ActWorld, write: ActWrite, rival_lane: str, + kind: Optional[str] = None) -> Tuple[Writer, Writer, List[str], int]: + """The act run to `write`, its commit call held while the rival, of + `kind` (by default `write.rival`), commits on `rival_lane`; then let go. + Returns the act, which may still run (a `lane-start` whose W8 committed + waits for the holder), the rival, the held write's id, read from the + shim's log, which logs a call before its hold, and the head at the hold, + before the rival commits (T252s).""" + fifo = world.root / "hold.fifo" + os.mkfifo(str(fifo)) + act = world.act(write, [{"mode": "hold", "match": write.match, "call": 1, "fifo": str(fifo)}]) + held = hold_point(fifo, act) + base = world.c.head() + calls = [c.get("stdin") or "" for c in act.commit_calls() if write.match in (c.get("stdin") or "")] + ids = [m.group(1) for m in (re.search(r"commit_write\(\s*-?[0-9]+\s*,\s*'(" + UUID_RE + ")'", s) + for s in calls[:1]) if m] + assert ids, f"the held commit call carries no write id: {calls[:1]}" + rival = world.rival(kind or write.rival, rival_lane) + assert rival.code == 0, f"the rival exited {rival.code}: {rival.err[-400:]}" + held.close() + return act, rival, ids, base + + +@CLIENT +@pytest.mark.parametrize("write", list(ACT_WRITES)) +def test_an_act_whose_scope_changed_before_its_commit_writes_nothing(act_worlds, write): + """TP:5854-5862: the act runs to `write`, held at its commit call; a rival + from a second session commits on one of its scopes, leaving the verb's + own answer as it was (`RIVALS`; see the section's head). On release the + held commit has lost its turn, and the retried one refuses with 2 and + `expectation: changed at seq by /`, naming the + rival's seq, lane and verb; the held write is not in the journal; and + the act exits with the code lane 4's arm gives, never 0. + + `[guard-answer-2-child]` alone ends 0 (lane 3's point 4(b), #165 + 6841918192; tasks:1438): the bind landed, so the guard's body exits 0; + its record holds one `note=`, for the refused write, in the facts file + beside `out=` that the outer folds into the record (`guard_bind_facts`); + the `set-row-state` itself exited 2, with its `expectation:` line; and + the journal grew only by the rival's entry after the hold (T252s). + + A `new-lane` rival (`[lane-start-w1-add-row]`) holds its premise too: + it leaves the lane without a row, so the retried `add-row` is never + refused for one, and its output holds no "already has a row" + (`add_row_checks`, `bc9ed80b:lanes-edit.sh:15420`, `:15433`), the + refusal T252y's rival was chosen to remove (lane 2's RV26, NIT 10; + T252z).""" + world = act_worlds() + w = ACT_WRITES[write] + act, rival, ids, base = race(world, w, w.lane) + act.wait(240) + k = max(int(r["seq"]) for r in world.c.json( + f"SELECT coalesce(jsonb_agg(q), '[]'::jsonb) FROM (SELECT seq FROM {REGISTER}.journal " + f"WHERE write_id = ANY(ARRAY{rival.write_ids()}::uuid[])) q;")) + if w.rival == "new-lane": + assert "already has a row" not in act.err + act.out, ( + f"[{write}] the retried add-row was refused for a row, against its rival's premise: {act.err[-900:]!r}") + said = f"expectation: {w.rival_scope}:{w.lane.lower()} changed at seq {k} by {w.lane}/{w.rival_verb}" + assert said in act.err + act.out, f"[{write}] {said!r} is not in {act.err[-900:]!r}" + assert all(world.c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{x}';") == "0" + for x in ids), f"[{write}] the held write is in the journal" + if w.tool == "guard": + assert act.code == 0, f"[{write}] the guard's body exited {act.code}, not 0: {act.err[-600:]}" + facts = Path(f"{world.record}.bind.1.facts") + assert facts.is_file(), f"[{write}] the body left no facts file at {facts}: {act.err[-600:]}" + notes = [n for n in facts.read_text(encoding="utf-8").splitlines() if n.startswith("note=")] + assert len(notes) == 1 and "set-row-state" in notes[0] and "exit 2" in notes[0], ( + f"[{write}] the record's notes are not one for the set-row-state's 2: {notes}") + grew = [int(r["seq"]) for r in world.c.json( + f"SELECT coalesce(jsonb_agg(q ORDER BY q.seq), '[]'::jsonb) FROM (SELECT seq FROM {REGISTER}.journal " + f"WHERE seq > {base}) q;")] + assert grew == [k], f"[{write}] after the hold at {base} the journal grew by {grew}, not the rival's {k} alone" + return + if w.code is not None: + assert act.code == w.code, f"[{write}] the act exited {act.code}, not {w.code}: {act.err[-600:]}" + assert act.code != 0, f"[{write}] the act exited 0 after its write was refused: {act.err[-600:]}" + + +@CLIENT +@pytest.mark.parametrize("write", list(ACT_WRITES)) +def test_an_act_whose_scopes_held_commits(act_worlds, write): + """TP:5863-5864: with a rival that touches no scope the write lists, + another lane's `PAUSED` (or row line), the held write commits. The + rival is `held_rival`, whatever the act's own rival is (`RIVALS`).""" + world = act_worlds() + w = ACT_WRITES[write] + act, _, ids, _ = race(world, w, R_OTHER[w.lane], w.held_rival) + deadline = time.monotonic() + 60 + while True: + ended = act.ended is not None + if all(world.c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{x}';") == "1" + for x in ids): + break + assert not ended and time.monotonic() < deadline, ( + f"[{write}] the held write {ids} did not commit; the act " + f"{'exited ' + str(act.code) if ended else 'still runs after 60 s'}: {act.err[-600:]}") + time.sleep(0.2) + + +@CLIENT +def test_an_acts_own_earlier_write_never_refuses_its_later_one(estates): + """Two writes of one act on one scope: the second commits, since the + first carries the same `act_id`.""" + e = estates() + act = act_begin(e) + for tag in ("first", "second"): + w = e.run(LA, act_write(e, "objects", tag=tag), extra=acted(act, f"objects:{LA.lower()}")) + assert w.code == 0, f"the {tag} write exited {w.code}: {w.err[-600:]}" + assert len(e.journal_after(1)) == 2 + + +@CLIENT +def test_a_handoff_request_never_refuses_the_holders_own_handoff(estates): + """A `HANDOFF-REQUESTED` line is not a lane-kind line: another lane's + request after the holder's base does not refuse the holder's write on + `row:` and `bind:`. + + The holder is bound (`bound_line`), since a request for a FREE lane is + refused with 8 before any write. The request is made with `--no-wait`: + it commits, and its empty wait then ends 2, "THE WAIT ENDED WITH + NOTHING", since nothing releases the lane. So the oracle for the request + is the journal, not its code (T252k).""" + e = estates(bound=(LA, LC)) + act = act_begin(e) + rival = e.run(LB, ["request-handoff", LA, "--session", session_of(LB), "--no-wait"]) + grew = e.journal_after() + assert [r["verb"] for r in grew] == ["request-handoff"], ( + f"the request exited {rival.code} and the journal holds {grew}: {rival.err[-400:]}") + w = e.run(LA, act_write(e, "row", tag="handoff"), extra=acted(act, f"row:{LA.lower()}", f"bind:{LA.lower()}")) + assert w.code == 0, f"the holder's write exited {w.code}: {w.err[-600:]}" + + +@CLIENT +def test_act_variables_change_nothing_in_files_mode(estates): + """At `mode=files`, a write with `LANES_ACT` and `LANES_EXPECT` set + behaves as one without them: both commit to origin, and no `psql` call + is made.""" + e = estates() + conf = e.root / "files.conf" + conf.write_text("mode=files\n", encoding="utf-8") + conf.chmod(0o600) + files = {"LANES_INDEX_CONFIG": str(conf)} + plain = e.run(LA, ["append-line", f"NOTE {EM} lane {LA}: files mode, plain (T252)"], extra=files) + act = dict(files, **acted(f"{'a' * 32}@files", f"row:{LA.lower()}")) + with_act = e.run(LA, ["append-line", f"NOTE {EM} lane {LA}: files mode, act set (T252)"], extra=act) + assert (plain.code, with_act.code) == (0, 0), [(w.code, w.err[-300:]) for w in (plain, with_act)] + assert plain.calls() == [] and with_act.calls() == [] + shown = run_git(e.origin, "show", "main:lanes/LANES.md", env=e.env) + assert "files mode, plain" in shown and "files mode, act set" in shown + + +# ---------------------------------------- the three conditions (T252, T252f) +# +# Brett's "Accept all three" (6721583757, row 22), as TP:5874-5879 states +# them and lane 3's batch 12 rules the gate's (#165 6808010497, "Lanes 2 and +# 4, question 2"; `RULINGS-batch12-analysis-20261111.md` (`b892fb39...`), +# A3, whose B15 text replaces TP:5877-5879's "so its `NOTED` writes do not +# refuse W13"). All three run their flows on T252b's world (lane 3's batch 7, +# item 5). +# +# [lane-start-reread] (T252i): `lane-start repoA 3 --request-handoff --wait +# 120`, a new place (a session of its own) taking the world's lane bound on +# Falcon, on primary, with `$LANES_EDIT` a logging stand-in and the request +# polling every second (`LANES_POLL_SECONDS`). Once its W8, `request-handoff`, +# has committed, a second session, the holder's, writes its handoff: the row's +# state cell (`set-row-state` to PAUSED), then its `PAUSED` line, which +# releases the binding and ends the wait. `lane-start` must then re-base with +# `act-begin --rebase`, which keeps the act's id and takes a base at or past +# the holder's entries, and read the row again (`register-row`) under it +# (Y:5656: "at `:2614` `lane-start` must also re-base, then re-read +# `register-row` and refuse on a change outside the state cell"; T428, "The +# re-base"). Two halves, each on a world of its own. In `[outside]` the holder +# first appends to the row's session cell (`append-session-id`), a change +# outside the state cell: `lane-start` exits non-zero, sends no write after +# its re-base and launches nothing, and W8's is the act's only entry. With the +# state cell alone changed, the act's writes after its re-base commit past +# the holder's entries, carrying its id, with no `expectation:` refusal, and +# `exec` starts `claude` (T252h's pid test). Readings: the change outside the +# state cell is N2-10's, an `append-session-id`, where any other cell would +# do; the holder writes its `PAUSED` line last, so that the re-base is taken +# past every write of its handoff (`lane-handoff` writes W9's line before +# W12's row, a race this case leaves out); and the refusal's code is not +# asserted, since T428's text names none (lane 4's draft dies 2). +# +# [lane-end-gate]: `lane-end`'s close-out of the world's live lane, on +# primary, under its `act-begin` base. `LANE_WORKTREES` is a stand-in that +# logs its argv and `LANES_ACT` and answers the dry run with nothing left, +# and the shim holds W13's commit call. The gate's `lane-worktrees` ran with +# the act's `LANES_ACT`, as `sweep ... --dry-run` (B15's Y:5715: "It +# is `lane-worktrees sweep --dry-run --porcelain` and writes +# nothing"), and at the hold the journal holds no entry after the base: the +# gate wrote nothing. A second session then writes a `NOTED lane:L` line +# with no act, as `lane-worktrees sweep --yes` writes one (its `note_line`: +# `LANES_LANE` and the sweeping session's `LANES_SESSION`). On release, +# W13 and W14 both commit after it, each carrying the act's `act_id`: a +# `NOTED lane:L` line yields only `sid:` keys (Y:1493-1496), and neither +# write lists one. +# +# [skill-step-4]: the handoff skill's step-4 block, run as the agent runs a +# block: the file's first block (`L` and `sread`), then the values steps 1 +# and 2 bound (`window`, `lane`, `ws`, `uuid`, `why`, `kind`), then the +# block, in one `bash`, with `$L` a logging stand-in for `lanes-edit.sh` and +# a `tmux` whose window is named for the world's live lane. Step 4 opens +# with `act-begin` and re-runs the identity's register reads before its +# first write (TP:5876-5877; Y:5724): for a window with a row these are step +# 1's `register-row` and `canon-lane`. Where step 1's `lane` is not the lane +# the window now names, it refuses with 2 before its first write: none of +# its three writes (`SKILL.md:735`, `:743`, `:786`) is sent, and the journal +# is as it was. With the same lane, the three commit, each carrying the act's +# `act_id`. Readings: step 1's lane reaches step 4 as `lane`, the name both +# steps use, and the case makes it differ by giving the agent's `lane` as +# another lane with a row while the window still names its own. +# +# They FAIL UNTIL L1-A6, L4-A2 (T410, T411), L4-B3 and L4-B5 land, and +# `[lane-start-reread]` until T428 (with T427's `start-record`), +# `[lane-end-gate]` until T430, `[skill-step-4]` until T431. Without +# PostgreSQL 16 each flow skips, after `[skill-step-4]`'s first check, its +# block opening with `act-begin`, which needs no engine. + +#: The three conditions, in the test plan's order; each runs its flow. +CONDITIONS = ("lane-start-reread", "skill-step-4", "lane-end-gate") +#: `[lane-start-reread]`'s starter, a session of its own, and the holder's new +#: session-cell entry, `[outside]`'s change outside the state cell. +STARTER_SID, HOLDER_NEW_SID = "aaaaaaaa-0257-4000-8000-00000000a257", "aaaaaaaa-0258-4000-8000-00000000a258" +#: The register writes `lane-start` may send after its re-read: T428's one +#: call, or its parts where no transcript uuid is known. +START_WRITES = ("start-record", "add-row", "append-session-id", "set-row-state", "log") +#: The second session that writes `[lane-end-gate]`'s `NOTED` line. +SWEEP_SID = "aaaaaaaa-0256-4000-8000-00000000a256" +#: The skill's three writes, as `$L`'s first argument names them. +SKILL_WRITES = ("log", "append-line", "set-row-state") + +_ACT_LOGGER = r'''#!/usr/bin/env python3 +# T252f's logging stand-in: its argv, and LANES_ACT and LANES_EXPECT as it +# saw them (null where unset), and its pid (T252h); then the real tool, where +# one is named. +import json, os, sys +HERE = os.path.dirname(os.path.abspath(__file__)) +REAL = REAL_TOOL +with open(os.path.join(HERE, "acts.log"), "a", encoding="utf-8") as log: + log.write(json.dumps({"argv": sys.argv[1:], "act": os.environ.get("LANES_ACT"), + "expect": os.environ.get("LANES_EXPECT"), "pid": os.getpid()}) + "\n") +if REAL: + os.execv(REAL, [REAL] + sys.argv[1:]) +''' + +_SKILL_TMUX = r'''#!/usr/bin/env bash +# T252f: this window is named WINDOW_NAME; every other call is the Sandbox's fake. +case "$*" in + "display-message -p #W" | "display-message -p #{window_name}") printf '%s\n' WINDOW_NAME; exit 0 ;; +esac +exec FAKE_TMUX "$@" +''' + + +def act_logger(where: Path, name: str, real: Optional[Path] = None) -> Path: + """A logging stand-in named `name` in `where`, logging to `acts.log` + beside it: with `real`, it then runs that; without, it prints nothing and + exits 0.""" + where.mkdir(parents=True, exist_ok=True) + tool = where / name + tool.write_text(_ACT_LOGGER.replace("REAL_TOOL", repr(str(real) if real else None)), encoding="utf-8") + tool.chmod(0o755) + return tool + + +def act_calls(tool: Path) -> List[dict]: + log = tool.parent / "acts.log" + return [json.loads(l) for l in log.read_text(encoding="utf-8").splitlines() if l] if log.exists() else [] + + +def act_entries(world: ActWorld, after: int) -> List[dict]: + """The journal's entries after seq `after`: seq, verb, `act_id` and + write id, in seq order.""" + return world.c.json(f"SELECT coalesce(jsonb_agg(q ORDER BY q.seq), '[]'::jsonb) FROM (SELECT seq, verb, " + f"act_id, write_id::text AS write_id FROM {REGISTER}.journal WHERE seq > {after}) q;") + + +def seq_base(act: Optional[str], who: str, task: str) -> Tuple[str, int]: + """`LANES_ACT`'s id and seq, where it is a seq base.""" + assert act is not None and ACT_RE.match(act), ( + f"{who} ran with LANES_ACT={act!r}, not the act's @: ({task})") + ident, rest = act.split("@", 1) + return ident, int(rest.split(":", 1)[0]) + + +def skill_prelude_block() -> str: + """The handoff skill's first block, which sets `L` and defines `sread` + for the blocks after it.""" + text = (REPO / "skills" / "handoff" / "SKILL.md").read_text(encoding="utf-8") + head = text.split("\n## 1.", 1)[0] + found = [b for b in re.findall(r"```(?:bash|sh)\n(.*?)```", head, re.S) if "sread()" in b] + assert found, "skills/handoff/SKILL.md has no block before step 1 that defines sread" + return found[0] + + +def lane_end_gate_holds(world: ActWorld) -> None: + """`[lane-end-gate]` (see the section's head).""" + gate = act_logger(world.root / "gate", "lane-worktrees") + fifo = world.root / "hold.fifo" + os.mkfifo(str(fifo)) + act = world.launch("lane-end", [str(world.bin / "lane-end"), R_HERE], R_HERE, + [{"mode": "hold", "match": verb_is("set-row-state"), "call": 1, "fifo": str(fifo)}], + {"LANE_WORKTREES": str(gate)}) + deadline = time.monotonic() + 120 + while not act_calls(gate) and act.ended is None: + assert time.monotonic() < deadline, "lane-end did not reach its close-out gate in 120 s" + time.sleep(0.1) + calls = act_calls(gate) + assert calls, f"lane-end exited {act.code} before its close-out gate ran: {act.err[-600:]}" + ident, base = seq_base(calls[0]["act"], "the close-out gate's lane-worktrees", "T430: the gate inherits it") + argv = calls[0]["argv"] + assert argv[:2] == ["sweep", R_HERE] and "--dry-run" in argv, ( + f"the close-out gate is not `lane-worktrees sweep {R_HERE} ... --dry-run`: {argv}") + held = hold_point(fifo, act) + gained = act_entries(world, base) + assert gained == [], f"between the act's base {base} and W13 the journal gained {gained}: the gate wrote" + noted = world.launch("sweep-noted", ["bash", str(world.bin / "lanes-edit.sh"), "log", "NOTED", f"lane:{R_HERE}", + "swept 1 cache or sandbox director(ies): they hold no work and are " + "rebuilt (T252f's second session)"], + R_HERE, extra={"LANES_SESSION": SWEEP_SID}) + noted.wait() + assert noted.code == 0, f"the second session's NOTED exited {noted.code}: {noted.err[-400:]}" + held.close() + act.wait(240) + assert act.code == 0 and "expectation:" not in act.err, ( + f"lane-end exited {act.code} after a NOTED lane:{R_HERE} written after its base: {act.err[-900:]}") + entries, noted_ids = act_entries(world, base), noted.write_ids() + swept = [e for e in entries if e["write_id"] in noted_ids] + mine = [e for e in entries if e["act_id"] == ident] + assert len(swept) == 1 and swept[0]["act_id"] is None, f"the NOTED line is not one entry of no act: {entries}" + assert [e["verb"] for e in mine] == ["set-row-state", "log"], ( + f"W13 and W14 did not both commit carrying the act's id {ident}: {entries}") + assert swept[0]["seq"] < mine[0]["seq"], f"the NOTED line did not commit before W13: {entries}" + + +def skill_step_4_command(world: ActWorld, prelude: str, block: str, lane: str, tag: str, + edit: Path) -> Tuple[List[str], dict]: + """Step 4's block as the agent runs it, in the window named for the + world's live lane: the skill's prelude, then steps 1 and 2's values, with + `L` as `edit` and step 1's `lane` given, then the block, in one `bash`, + and a `tmux` first on `PATH` naming that window. The command and the + environment it adds. The flow case (`skill_step_4`, T252f) and the race's + skill act (`ActWorld.act`, T252b) both run it (T252p).""" + tmux_dir = world.root / f"skill-{tag}-tmux" + tmux_dir.mkdir() + (tmux_dir / "tmux").write_text(_SKILL_TMUX.replace("WINDOW_NAME", R_HERE) + .replace("FAKE_TMUX", shlex.quote(str(world.fakebin / "tmux"))), encoding="utf-8") + (tmux_dir / "tmux").chmod(0o755) + bound = [f"L={shlex.quote(str(edit))}", f"window={R_HERE}", f"lane={lane}", f"ws={W_WS}", "ws_missing=''", + f"uuid={R_SID[R_HERE]}", "uuid_missing=''", "why='T252f skill case'", "kind=unknown"] + command = ["bash", "-c", "\n".join([prelude, *bound, block])] + return command, {"PATH": str(tmux_dir) + os.pathsep + world.env["PATH"]} + + +def skill_step_4(world: ActWorld, prelude: str, block: str, lane: str, tag: str) -> Tuple[Writer, List[dict]]: + """Step 4's block, run in the window named for the world's live lane, + with step 1's `lane` given: the run, and its `$L` calls.""" + edit = act_logger(world.root / f"skill-{tag}", "lanes-edit.sh", world.bin / "lanes-edit.sh") + command, extra = skill_step_4_command(world, prelude, block, lane, tag, edit) + run = world.launch(f"skill-{tag}", command, R_HERE, extra=extra) + run.wait(240) + return run, act_calls(edit) + + +def verbs_of(calls: List[dict]) -> List[str]: + return [(c["argv"] or [""])[0] for c in calls] + + +def skill_step_4_holds(world: ActWorld, prelude: str, block: str) -> None: + """`[skill-step-4]` (see the section's head).""" + head = world.c.head() + run, calls = skill_step_4(world, prelude, block, R_PAUSED, "differs") + verbs = verbs_of(calls) + assert verbs[:1] == ["act-begin"], f"step 4's first call is {verbs[:1]}, not act-begin (T431): {verbs}" + assert {"register-row", "canon-lane"} <= set(verbs[1:]), ( + f"step 4 did not run the identity's register reads again after act-begin (T431): {verbs}") + sent = [v for v in verbs if v in SKILL_WRITES] + # The identity refusal is a step-4 stop, and the stop ends 2 (T431; lane + # 3's question 4, #165 6835210568: step 5 runs only where the block ended + # 0, 3, 69 or 75; T252o). + assert run.code == 2 and sent == [] and run.commit_calls() == [], ( + f"with step 1's lane {R_PAUSED} and the window's {R_HERE}, step 4 exited {run.code} and sent {sent} " + f"(T431): {run.err[-600:]}") + assert world.c.head() == head, f"the journal grew from {head} though step 1's lane differs" + run, calls = skill_step_4(world, prelude, block, R_HERE, "same") + verbs = verbs_of(calls) + first = next((k for k, v in enumerate(verbs) if v in SKILL_WRITES), len(verbs)) + assert verbs[:1] == ["act-begin"] and {"register-row", "canon-lane"} <= set(verbs[1:first]), ( + f"step 4 did not open with act-begin and the identity's reads before its first write (T431): {verbs}") + acts = {c["act"] for c, v in zip(calls, verbs) if v in SKILL_WRITES} + assert len(acts) == 1, f"step 4's writes ran under LANES_ACT {sorted(map(str, acts))} (T431)" + ident, base = seq_base(acts.pop(), "step 4's writes", "T431") + entries = act_entries(world, base) + assert [(e["verb"], e["act_id"]) for e in entries] == [(v, ident) for v in SKILL_WRITES], ( + f"step 4's three writes did not all commit carrying the act's id {ident}: {entries}: {run.err[-600:]}") + + +def lane_start_reread(world: ActWorld, outside: bool) -> Tuple[Writer, List[dict], List[dict], List[str]]: + """`[lane-start-reread]`'s run (see the section's head): `lane-start` + asks for the lane bound on Falcon and waits; once its W8 has committed, + the holder's second session writes its handoff, `[outside]` first + changing the row's session cell. Returns the run, its verb calls, the + `claude` its `exec` started, and the holder's write ids.""" + tag = "outside" if outside else "state" + edit = act_logger(world.root / f"reread-{tag}", "lanes-edit.sh", world.bin / "lanes-edit.sh") + claude = act_logger(world.root / f"reread-{tag}-claude", "claude") + run = world.launch(f"lane-start-{tag}", [str(world.bin / "lane-start"), "repoA", "3", "--request-handoff", + "--wait", "120", "--dir", str(world.lane_dir)], R_FAR, + extra={"LANES_EDIT": str(edit), "LANES_SESSION": STARTER_SID, "LANES_POLL_SECONDS": "1", + "FAKE_CC_PATH": str(claude)}) + deadline = time.monotonic() + 120 + while run.ended is None and not any(e["verb"] == "request-handoff" for e in act_entries(world, 1)): + assert time.monotonic() < deadline, f"[{tag}] lane-start's W8 did not commit in 120 s" + time.sleep(0.5) + holder: List[str] = [] + if run.ended is None: + writes = [["append-session-id", R_FAR, R_SID[R_FAR], f"{ARROW} harness `{HOLDER_NEW_SID}`", + "T252i's holder: a session-cell change"]] if outside else [] + writes += [["set-row-state", R_FAR, f"PAUSED {DOT} handed off on request (T252i's holder)"], + ["log", "PAUSED", f"lane:{R_FAR}", ARROW, "T252i's holder", "handed off on request"]] + for argv in writes: + w = world.launch("holder", ["bash", str(world.bin / "lanes-edit.sh"), *argv], R_FAR) + w.wait() + assert w.code == 0, f"[{tag}] the holder's {argv[0]} exited {w.code}: {w.err[-400:]}" + holder += w.write_ids() + run.wait(240) + return run, act_calls(edit), [c for c in act_calls(claude) if c.get("pid") == run.proc.pid], holder + + +def lane_start_reread_holds(make) -> None: + """`[lane-start-reread]` (see the section's head): `[outside]`, then the + state cell alone, each on a world of its own from `make`.""" + for outside in (True, False): + world, tag = make(), "outside" if outside else "state" + run, calls, launched, holder = lane_start_reread(world, outside) + verbs = verbs_of(calls) + asked = verbs.index("request-handoff") if "request-handoff" in verbs else len(verbs) + assert "act-begin" in verbs[:asked], f"[{tag}] lane-start ran no act-begin before its W8 (T428): {verbs}" + assert asked < len(verbs), f"[{tag}] lane-start exited {run.code} and sent no W8: {run.err[-600:]}" + ident, _ = seq_base(calls[asked]["act"], f"[{tag}] W8's request-handoff", "T428") + assert holder, f"[{tag}] lane-start exited {run.code} before its W8 committed: {run.err[-600:]}" + rebase = next((k for k in range(asked + 1, len(calls)) + if calls[k]["argv"][:2] == ["act-begin", "--rebase"]), None) + assert rebase is not None, ( + f"[{tag}] lane-start did not re-base with act-begin --rebase after its W8 wait (T428): {verbs[asked:]}") + reread = next((k for k in range(rebase + 1, len(calls)) if calls[k]["argv"][:2] == ["register-row", R_FAR]), + None) + assert reread is not None, ( + f"[{tag}] lane-start did not read {R_FAR}'s row again after its re-base (T428): {verbs[rebase:]}") + entries = act_entries(world, 1) + seqs = {e["write_id"]: e["seq"] for e in entries} + assert set(holder) <= set(seqs), f"[{tag}] the holder's writes {holder} are not all in the journal: {entries}" + released = max(seqs[x] for x in holder) + again, base = seq_base(calls[reread]["act"], f"[{tag}] the row's re-read", "T428") + assert again == ident and base >= released, ( + f"[{tag}] the row's re-read ran under {calls[reread]['act']}: not act {ident} re-based at or past " + f"the holder's seq {released} (T428)") + mine = [e for e in entries if e["act_id"] == ident] + if outside: + sent = [v for v in verbs[rebase + 1:] if v in START_WRITES] + assert run.code != 0 and sent == [] and launched == [], ( + f"[{tag}] with {R_FAR}'s session cell changed during the wait, lane-start exited {run.code}, sent " + f"{sent} after its re-base and launched {len(launched)} claude (T428): {run.err[-600:]}") + assert [e["verb"] for e in mine] == ["request-handoff"], f"[{tag}] the act's entries: {mine}" + else: + later = [e["verb"] for e in mine if e["seq"] > released] + assert later and "expectation:" not in run.err and launched, ( + f"[{tag}] with only {R_FAR}'s state cell changed, lane-start's writes after its re-base committed " + f"{later} and it launched {len(launched)} claude (T428): {run.err[-900:]}") + + +@pytest.mark.parametrize("condition", CONDITIONS) +def test_each_of_the_three_conditions_holds(condition, request): + """[lane-start-reread]: after its W8 wait, `lane-start` re-bases with + `act-begin --rebase` and reads the row again: a change outside the + state cell refuses it with no write after the re-base, and the state + cell alone does not. [skill-step-4]: the skill's step 4 opens with + `act-begin` and re-derives the identity; where `lane` differs from step + 1's it sends none of its three writes, and with the same lane the three + commit. Its block is found with no engine: red until T431 lands. + [lane-end-gate]: `lane-end`'s gate runs with the act's `LANES_ACT` and + writes nothing, and a `NOTED lane:L` line that a second session writes + after the base refuses neither W13 nor W14 (lane 3's batch 12). All + three run their flows on T252b's world (see the section's head).""" + if condition == "skill-step-4": + prelude, block = skill_prelude_block(), skill_step_4_block() + skill_step_4_holds(request.getfixturevalue("act_worlds")(), prelude, block) + elif condition == "lane-end-gate": + lane_end_gate_holds(request.getfixturevalue("act_worlds")()) + else: + lane_start_reread_holds(request.getfixturevalue("act_worlds")) + + +# ---------------------------------------- the launches (T252, T252h) +# +# TP:5889-5892 (Y:5596): before a launch `LANES_ACT` is unset (`exec`, +# `respawn-pane`), so the session and its guard do not inherit it: the fake +# `claude` started by `exec`, and the command the fake `tmux` logs for +# `respawn-pane`, see no `LANES_ACT`, in a run where `act-begin` had exported +# one. Both run their flows on T252b's world (lane 3's batch 7, item 5), with +# `$LANES_EDIT` a logging stand-in (T252f's `act_logger`). Its log is the +# proof that the act exported one: a verb call after `act-begin` ran with a +# seq base. +# +# [exec]: `lane-start repoA 1`, the resume of the world's paused lane, run to +# its launch. `claude-current` names a logging stand-in as the `claude` to +# launch, and the one that ran in `lane-start`'s own process, by its pid, is +# the one `exec` started. It saw neither `LANES_ACT` nor `LANES_EXPECT` +# (T428: both are unset before the launch; Y:5581: `LANES_EXPECT` "is never +# exported"). +# +# [respawn-pane]: `lane-handoff --restart --lane repoA-2`, the live lane's +# `/ctx`, run to its respawn, with `$TMUX_PANE` naming its pane and the +# lane's handoff file in the world (`ActWorld.handoff`), since a restart +# respawns nothing over a handoff it could not refresh. A `tmux` +# wrapper logs `respawn-pane`'s argv, with the two variables as tmux saw them, +# and answers 0; every other call is the Sandbox's fake. Neither variable was +# in its environment (T429), and its command names neither, since a `-e` or a +# prefix in the respawn line would hand them on too. +# +# Readings: the export is read from the act's verb calls, so an act that +# passed its base only as a prefix on each call would pass too; the oracle, +# what the launch saw, is the same. 5e's daily report, which T428 also has +# unset both for, stays off in the world (conftest's +# `LANE_WORKTREES_REPORT=off`), so it is not asserted. +# +# They FAIL UNTIL L1-A6, L4-A2 (T410, T411), L4-B3 and L4-B5 land, `[exec]` +# until T428 and `[respawn-pane]` until T429. Without PostgreSQL 16 each +# skips. + +#: Each launch: the act's tool, its argv and its lane in T252b's world. +LAUNCHES = {"exec": ("lane-start", ("repoA", "1"), R_PAUSED), + "respawn-pane": ("lane-handoff", ("--restart", "--lane", R_HERE, "T252h's restart"), R_HERE)} + +_RESPAWN_TMUX = r'''#!/usr/bin/env bash +# T252h: respawn-pane is logged, with LANES_ACT and LANES_EXPECT as tmux saw +# them, and answers 0; every other call is the Sandbox's fake. +if [ "${1-}" = respawn-pane ]; then exec RESPAWN_LOGGER "$@"; fi +exec FAKE_TMUX "$@" +''' + + +def act_exported(calls: List[dict], who: str, task: str) -> Tuple[str, int]: + """The act's id and base, from the first verb call after `act-begin` + that ran with a seq base: the proof that the act exported one.""" + verbs = verbs_of(calls) + assert "act-begin" in verbs, f"{who} never ran act-begin ({task}): {verbs}" + acts = [c["act"] for c in calls[verbs.index("act-begin") + 1:] if c["act"]] + based = [a for a in acts if ACT_RE.match(a)] or acts + return seq_base(based[0] if based else None, f"{who}'s verb calls after act-begin", task) + + +def launch_flow(world: ActWorld, launch: str) -> Tuple[Writer, List[dict], List[dict]]: + """The act that launches, run to its launch (see the section's head): + the run, its verb calls, and what its launch saw.""" + tool, argv, lane = LAUNCHES[launch] + edit = act_logger(world.root / f"{launch}-edit", "lanes-edit.sh", world.bin / "lanes-edit.sh") + seen = act_logger(world.root / f"{launch}-seen", "claude" if launch == "exec" else "tmux-respawn") + extra = {"LANES_EDIT": str(edit)} + if launch == "exec": + extra["FAKE_CC_PATH"] = str(seen) + else: + wrapper = world.root / f"{launch}-tmux" + wrapper.mkdir() + (wrapper / "tmux").write_text(_RESPAWN_TMUX.replace("RESPAWN_LOGGER", shlex.quote(str(seen))) + .replace("FAKE_TMUX", shlex.quote(str(world.fakebin / "tmux"))), + encoding="utf-8") + (wrapper / "tmux").chmod(0o755) + extra.update(PATH=str(wrapper) + os.pathsep + world.env["PATH"], TMUX_PANE="%1") + world.handoff(lane) + run = world.launch(launch, [str(world.bin / tool), *argv], lane, extra=extra) + run.wait(240) + saw = act_calls(seen) + if launch == "exec": + saw = [c for c in saw if c.get("pid") == run.proc.pid] + return run, act_calls(edit), saw + + +@pytest.mark.parametrize("launch", list(LAUNCHES)) +def test_lanes_act_is_unset_before_a_launch(launch, request): + """`LANES_ACT` is unset before `exec` (`lane-start`) and before + `respawn-pane` (`lane-handoff`), so the session and its guard never + inherit it: in a run whose verb calls carried the act's seq base, the + `claude` that `exec` started, and tmux's `respawn-pane`, saw neither + `LANES_ACT` nor `LANES_EXPECT`, and the respawn's command names + neither. Both run their flows on T252b's world (see the section's + head): red until T428 and T429 land.""" + tool, task = LAUNCHES[launch][0], {"exec": "T428", "respawn-pane": "T429"}[launch] + run, calls, saw = launch_flow(request.getfixturevalue("act_worlds")(), launch) + act_exported(calls, tool, task) + what = "no claude ran in its own process" if launch == "exec" else "tmux was asked for no respawn-pane" + assert saw, f"[{launch}] {tool} exited {run.code} before its launch: {what}: {run.err[-900:]}" + assert (saw[0]["act"], saw[0]["expect"]) == (None, None), ( + f"[{launch}] the launch saw LANES_ACT={saw[0]['act']!r} and LANES_EXPECT={saw[0]['expect']!r} ({task})") + named = [a for a in saw[0]["argv"] if "LANES_ACT" in a or "LANES_EXPECT" in a] + assert named == [], f"[{launch}] the launch's command hands on {named} ({task})" + + +@CLIENT +def test_an_act_that_decided_on_canon_lane_is_refused_by_a_row_added_on_its_walk(estates): + """Alias lines lead `a` to `b` and `b` to `c`, which has a row: an act that + decided on `canon-lane a` takes the names from `canon-lane --walk a` and + lists `row:` and `alias:` for each (Y:5624), so a rival `add-row` for `b` + after the base refuses its write with 2, LR009 naming `row:b` + (TP:5871-5873; lane 3's batch 7, ask 3).""" + e = estates(aliases=WALK_ALIASES) + act = act_begin(e, LC) + walked = e.run(LC, ["canon-lane", "--walk", WALK_A]) + names = walked.out.split() + assert walked.code == 0 and names == [WALK_A, WALK_B, LC], ( + f"canon-lane --walk {WALK_A} exited {walked.code} and printed {walked.out!r}: {walked.err[-400:]}") + expect = [f"{kind}:{name.lower()}" for name in names for kind in ("row", "alias")] + rival = e.run(WALK_B, ["add-row", w_row(WALK_B)]) + assert rival.code == 0, f"the rival add-row exited {rival.code}: {rival.err[-400:]}" + w = e.run(LC, act_write(e, "objects", lane=LC, tag="walk"), extra=acted(act, *expect)) + assert w.code == 2 and f"expectation: row:{WALK_B.lower()} changed" in w.err, (w.code, w.err[-600:]) + + +@CLIENT +@pytest.mark.parametrize("case", ["after-loss-cutover", "base-past-n"]) +def test_the_client_refuses_an_acts_stale_base_with_2_at_the_snapshot(estates, case): + """An act whose generation is not the switch's, or whose base is past + `n`: exit 2 at the snapshot, no commit call, nothing written.""" + e = estates() + gen = e.c.switch()["generation"] + n = e.c.head() + act = (f"{'b' * 32}@{n}:{uuid.uuid4()}" if case == "after-loss-cutover" + else f"{'b' * 32}@{n + 5}:{gen}") + state = e.c.state() + w = e.run(LA, act_write(e, "objects", tag=case), extra=acted(act, f"objects:{LA.lower()}")) + assert w.code == 2, f"[{case}] exit {w.code}: {w.err[-600:]}" + assert w.commit_calls() == [] and e.c.state() == state + + +@pytest.mark.parametrize("case", ["generation", "base-past-head"]) +def test_the_commits_act_backstops_are_lr007_act_generation_and_lr004(clones, case): + """The client's checks bypassed by a direct commit call (T223's + `Clone`): an act of another generation gets LR007 `act-generation`, a + base past the head LR004; nothing is written. `commit_write` checks an + act's generation and base only where it lists an expectation (step 3b, + `IF cardinality(v_expect) > 0`), before it compares any scope, so each + act lists one: the writer's own row. (The hold line derives no scope.) + + The register is a clone of `tmpl`, not of `tmpl_imported`, brought to + `primary` by AT-LR006's route, as T221's step-6 cases and T253b's case + bring theirs and as this module's phase W estates do: `import_files`' + `import` entry at seq 0, then `Clone.to_primary`'s `cutover` at seq 1 + and `record_marker`. So the ids need no T115, whose `import --at` builds + `tmpl_imported` after T112: they fail only until T107's step 3b, T227's + `tmpl` and T228's `lanes_w_fx` land, and T112's `-k` runs them (lane 3's + way 1, #165 6841609469; T252r).""" + c = clones(W_TEMPLATE) + import_files(c, {LANES_MD: "# LANES.md: the act backstops' register (T252r)\n"}) + assert c.to_primary() == 1, "the backstops' cutover is not at seq 1 (AT-LR006's route)" + c.no_taker() + entry = c.append(L1, *hold(c, L1)) + head, gen = c.head(), c.switch()["generation"] + expect = [f"row:{L1.lower()}"] + entry["act"] = ({"id": "c" * 32, "base": head, "generation": str(uuid.uuid4()), "expect": expect} + if case == "generation" + else {"id": "c" * 32, "base": head + 5, "generation": gen, "expect": expect}) + if case == "generation": + c.refused(entry, "LR007", "act-generation") + else: + c.refused(entry, "LR004", None) + + +@CLIENT +@pytest.mark.parametrize("case", ["space", "no-break-space"]) +def test_a_line_deriving_a_key_that_is_not_a_scope_exits_2_on_lr010_once(estates, case): + """`append-line` with a row-shaped line whose first backticked token holds + a space or a no-break space: the commit refuses LR010, the writer exits + 2 naming the path, the line and the key, with one commit call, never + retried, and nothing written. The key is named with each white-space + character as `[U+hhhh]` (Y:1504), `row:x[U+0020]y` and `row:x[U+00A0]y` + (T252m).""" + e = estates() + gap = " " if case == "space" else chr(0x00A0) + line = f"| `x{gap}y` | T252 | a key that is not a scope |" + state = e.c.state() + w = e.run(LA, ["append-line", line]) + assert w.code == 2, f"[{case}] exit {w.code}: {w.err[-600:]}" + key = f"row:x[U+{ord(gap):04X}]y" + assert "LR010" in w.err and LANES_MD in w.err and key in w.err, w.err + assert len(w.commit_calls()) == 1 and e.c.state() == state + + +# ============================================================ PHASE N: AT-LR5's other cases and AT-LR009 (T232) +# +# LR005'S RULES AND THE ACT'S KEY, ON `commit_write`'S SIDE (AT-LR5, +# TP:4310-4476; AT-LR009, TP:4834-4941). The test plan puts them in phase W; +# F13's block writes them in phase N, as T223's above, since they check the +# same function (R1's M3; FR-059): each case is a direct `commit_write` call as +# `lanes_w_fx`, in the commit call's own form, on a clone of `tmpl_imported` +# that `Clone.to_primary` brings to `primary`, with the entry built here as the +# client builds it. AT-F5 holds the client's half (T233, T252). The forms are +# lane 1's, read at `refs/l1/T112-fu2` and `refs/l1/R-merge6`, `lanes-index`: +# * the entry's keys and its `act`, `{id, base, generation, expect}`, the +# scopes in lower case in the order given: `commit_write`'s head comment +# and its act checks, and `register_act_of_env`; +# * a log line's `log_lines` row, and a lane's `object_holds` row on its +# object, with D19's spellings on a claim's alone: `_register_log_row`, +# `_register_last_hold` and `register_object_spellings`; +# * a slot line's `repo_key`, canonical and lower case, and its +# `repo_spellings`, the spelling as typed first: `_register_slot_row`; +# * a row's `lanes` row: `_register_lane_row`; the cutover's abort: +# `switch_to_files` in one SERIALIZABLE transaction (R-merge6); +# * the stray entries' kinds, verbs and `git_commit`: lane 2's T221 +# (`test_sql_register_schema.py` at line W3b's `8f645fce`). +# THE ROLLBACK'S ENTRY is T304's form (`rollback_entry`), as line R's +# `lanes-index rollback` builds it from merge14 on, and as T231's schema module +# sends it: kind and verb `rollback`, one edit removing `lanes/STORE`, its one +# `files` row not present, and `not_after`. +# Readings: the lines' texts (a row's cells, `RENAMED`'s words) are this +# module's, in the grammar `lanes-edit.sh` and `parse_log_line` read; only the +# keys they derive are asserted. `rename-lane` and `archive-rows` need step 6's +# derived deletes (ERR-48), which R-merge6 has and T112-fu2 alone has not. +# Every case FAILS UNTIL L1-A6 (T107's `commit_write`, its steps 3b, 5a, 7 and +# 8a) and lane 2's T227 and T228 land, as T223's do; without PostgreSQL 16 +# each skips. + +LA5, LB5, LC5, LD5 = "L1-t232", "L2-t232", "L3-t232", "L4-t232" +LB5_NEW = "L2b-t232" +FOO = "foo-03" +#: Each lane's session uuid, fixed so a case can name its `sid:` key. +SID5 = {lane: str(uuid.uuid5(uuid.NAMESPACE_URL, f"t232/{lane}")) for lane in (LA5, LB5, LC5, LD5)} +OBJ5, OBJ5_ALIAS, OBJ5_FREE = f"{KEY}#41", f"{ALIAS}#41", f"{OTHER_KEY}#9" +RETIRED_MD, ALIASES_TSV = "lanes/archive/LANES-retired.md", "lanes/aliases.tsv" +STRAY_COMMIT = "3" * 40 +ACT_ID = "act-t232" +#: D18's and D19's spellings of a repository as a line types it, the spelling +#: as typed first (`register_repo_spellings`), with `repos.tsv` mapping `r9`. +REPO_SPELLINGS = {KEY: [KEY, ALIAS], ALIAS: [ALIAS, KEY], OTHER_KEY: [OTHER_KEY]} +CLAIM_VERBS = ("CLAIMED", "TAKEOVER") +#: Every table a refused call must leave as it was (AT-LR5's and AT-LR009's oracles). +TABLES5 = ("register_head", "journal", "journal_edits", "journal_files", "journal_scopes", "files", + "doc_lines", "lanes", "log_lines", "object_holds", "register_lines", "register_switch") +#: A call made while a session holds the head row: past its lock_timeout it would end 55P03. +LOCKED_SQL = "SET lock_timeout = '1500ms';\n" + COMMIT_SQL +#: The cutover's abort, as R-merge6's `cutover --abort` sends it, less its read of the switch. +ABORT_SQL = ("BEGIN ISOLATION LEVEL SERIALIZABLE;\n" + f"SELECT {REGISTER}.switch_to_files(:client, :seq, :'lane', :'ws');\n" + "COMMIT;\n") +_ARGS5 = itertools.count(1) + + +def log_path(lane: str) -> str: + return f"lanes/log/{lane}.md" + + +def object_spellings(obj: str) -> Optional[List[str]]: + """`register_object_spellings`: the object as written, then the same + object under each of its repository's spellings; a lane's has none.""" + if obj.startswith("lane:"): + return None + repo, _, rest = obj.partition("#") + return [spelling + "#" + rest for spelling in REPO_SPELLINGS.get(repo, [repo])] + + +def stamp5(c: Clone, minutes_ago: float = 0.0) -> Tuple[str, str]: + """A log line's UTC, to the second as `log_lines`' primary CHECK takes it, + and its `utc_ts`, by the database's clock less `minutes_ago`.""" + said = c.value("SELECT to_char(t AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') || '|' || t::text " + f"FROM (SELECT date_trunc('second', clock_timestamp() - make_interval(secs => {minutes_ago * 60})) " + "AS t) s;") + utc, utc_ts = said.split("|") + return utc, utc_ts + + +def row_text(lane: str, sid: Optional[str], state: str = "ACTIVE", note: str = "") -> str: + """A `lanes/LANES.md` row: the lane's backticked name first (ROW_AWK), then + the cells `_register_lane_row` reads: the sessions, the workstation, the + start, the objects, the handoff and the state.""" + return (f"| `{lane}`{note} | {sid or '—'} | {WS} | 2026-11-10T09:00Z | — | " + f"lanes/handoff-{lane}.md | {state} |") + + +class Draft: + """One entry, built against the clone's files as `register_commit_entry` + builds it: the edits in order, each touched path's `files` row as it + stands after them, and the structured rows of the lines written.""" + + def __init__(self, c: Clone, lane: str, verb: str, kind: str = "write", **journal) -> None: + self.c, self.lane, self.verb, self.kind, self.extra = c, lane, verb, kind, journal + self.images: Dict[str, list] = {} + self.touched: List[str] = [] + self.gone: List[str] = [] + self.edits: List[dict] = [] + self.rows: Dict[str, list] = {"lanes": [], "log_lines": [], "register_lines": [], "object_holds": []} + + def _image(self, path: str) -> list: + if path not in self.images: + final_newline, lines = self.c.file(path) + self.images[path] = [final_newline, list(lines)] + return self.images[path] + + def _edit(self, **edit) -> None: + edit["ord"] = len(self.edits) + 1 + self.edits.append(edit) + for path in (edit["path"], edit.get("to_path")): + if path and path not in self.touched: + self.touched.append(path) + + def lines(self, path: str) -> List[str]: + return list(self._image(path)[1]) + + def append(self, path: str, text: str) -> int: + image = self._image(path) + if image[0] is None: + self._edit(op="create", path=path) + image[0] = True + image[1].append(text) + self._edit(op="append", path=path, new_text=text) + return len(image[1]) + + def replace(self, path: str, line_no: int, text: str) -> None: + image = self._image(path) + old = image[1][line_no - 1] + self._edit(op="replace", path=path, line_no=line_no, old_sha256=sha256(old.encode("utf-8")), + new_text=text) + image[1][line_no - 1] = text + + def delete(self, path: str, line_no: int) -> None: + image = self._image(path) + old = image[1].pop(line_no - 1) + self._edit(op="delete", path=path, line_no=line_no, old_sha256=sha256(old.encode("utf-8"))) + + def rename(self, path: str, to_path: str) -> None: + self.images[to_path] = self._image(path) + del self.images[path] + self.gone.append(path) + self._edit(op="rename", path=path, to_path=to_path) + + def row(self, path: str, lane: str, text: str, line_no: int, *, archived: bool = False, + aliases: Sequence[str] = ()) -> None: + """The `lanes` row of a row line, as `_register_lane_row` derives it.""" + cells = [cell.strip(" \t") for cell in text.split("|")] + ids = re.findall(UUID_RE, cells[2].lower()) + self.rows["lanes"].append({ + "path": path, "line_no": line_no, "raw": text, "lane_key": lane.lower(), "lane": lane, + "aliases": list(aliases), "home": None, "workstation": cells[3], "started_utc": cells[4], + "handoff_path": cells[6], "state_phrase": cells[7], "objects_cell": cells[5], + "session_ids": ids, "last_session": ids[-1] if ids else None, "archived": archived, + "owner": None}) + + def hold(self, obj: str, lane: str, verb: str, utc_ts: Optional[str], path: str, ordinal: int) -> None: + """A lane's `object_holds` row: its last line on the object.""" + holds = [h for h in self.rows["object_holds"] if (h["object"], h["lane_key"]) != (obj, lane.lower())] + holds.append({"object": obj, "lane_key": lane.lower(), "lane": lane, "verb": verb, "utc_ts": utc_ts, + "file": path, "ordinal": ordinal, + "spellings": object_spellings(obj) if verb in CLAIM_VERBS else None, "exempt": False}) + self.rows["object_holds"] = holds + + def log(self, verb: str, lane: str, obj: str, minutes_ago: float = 0.0, *, sid: Optional[str] = None, + words: str = "") -> int: + """A line appended to `lane`'s log, its `log_lines` row, and its + lane's `object_holds` row where the object is not a lane's.""" + path, sid = log_path(lane), sid or SID5[lane] + utc, utc_ts = stamp5(self.c, minutes_ago) + text = f"{verb} — lane {lane}, session {sid}@{WS}, {utc}, {obj}" + (f" — {words}" if words else "") + line_no = self.append(path, text) + self.rows["log_lines"].append({ + "path": path, "line_no": line_no, "raw": text, "lane": lane, "verb": verb, "session": sid, + "workstation": WS, "utc": utc, "utc_ts": utc_ts, "object": obj, "ref": "", "payload": "", + "payload_fields": {}, "free_text": words, "written_lane": lane, "unreadable": False, + "unreadable_why": None}) + if not obj.startswith("lane:"): + self.hold(obj, lane, verb, utc_ts, path, line_no) + return line_no + + def entry(self, act: Optional[dict] = None) -> dict: + files = [] + for path in self.touched: + if path in self.gone: + files.append({"path": path, "present": False, "sha256": None, "final_newline": None}) + else: + final_newline, lines = self.images[path] + files.append({"path": path, "present": True, "sha256": sha256(rendered(lines, final_newline)), + "final_newline": final_newline}) + extra = dict(self.extra) + if self.kind == "write": + extra["args_sha256"] = sha256(f"{self.lane}\0{self.verb}\0{next(_ARGS5)}".encode("utf-8")) + entry = {"journal": self.c.journal(self.kind, self.verb, self.lane, + message=f"{self.verb} by {self.lane} (T232)", **extra), + "not_after": self.c.not_after(), "edits": self.edits, "files": files, **self.rows} + if act is not None: + entry["act"] = act + return entry + + +# ------------------------------------------------------------ the entries +def claim5(c: Clone, lane: str, obj: str, verb: str = "CLAIMED", act: Optional[dict] = None, + minutes_ago: float = 0.0) -> dict: + """`claim` (or `release`): one log line on the object, and the lane's hold.""" + d = Draft(c, lane, "release" if verb == "RELEASED" else "claim") + d.log(verb, lane, obj, minutes_ago) + return d.entry(act) + + +def row_line(c: Clone, lane: str) -> Tuple[int, str]: + _, lines = c.file(LANES_MD) + for line_no, text in enumerate(lines, 1): + if text.startswith(f"| `{lane}`"): + return line_no, text + raise AssertionError(f"no row for {lane} in {LANES_MD}") + + +def set_row_state(c: Clone, lane: str, state: str = "PAUSED", act: Optional[dict] = None) -> dict: + """`set-row-state`: the lane's row replaced, its state cell changed.""" + d = Draft(c, lane, "set-row-state") + line_no, _ = row_line(c, lane) + text = row_text(lane, SID5[lane], state) + d.replace(LANES_MD, line_no, text) + d.row(LANES_MD, lane, text, line_no) + return d.entry(act) + + +def rename_lane(c: Clone, old: str = LB5, new: str = LB5_NEW) -> dict: + """`rename-lane ` (Amendment 16): the row's key cell + `` `` *(ex ``, renamed )* ``, the log renamed and a + `RENAMED` line appended to it, and `\\t\\t` appended to + `lanes/aliases.tsv`; with the new lane's `lanes` row and its hold on each + object its log names, at its last line on it.""" + d = Draft(c, old, "rename-lane") + line_no, _ = row_line(c, old) + text = row_text(new, SID5[old], note=f" *(ex `{old}`, renamed 2026-11-10T10:00Z)*") + d.replace(LANES_MD, line_no, text) + d.row(LANES_MD, new, text, line_no, aliases=[old]) + d.append(ALIASES_TSV, f"{old}\t{new}\t2026-11-10T10:00:00Z") + held = c.json(f"SELECT coalesce(json_agg(json_build_array(object, verb, utc_ts::text, ordinal) ORDER BY ordinal), " + f"'[]') FROM {REGISTER}.log_lines WHERE file = '{log_path(old)}' AND NOT unreadable " + "AND object NOT LIKE 'lane:%';") + d.rename(log_path(old), log_path(new)) + d.log("RENAMED", new, f"lane:{new}", sid=SID5[old], words=f"renamed from {old}") + for obj, verb, utc_ts, ordinal in held: + d.hold(obj, new, verb, utc_ts, log_path(new), ordinal) + return d.entry() + + +def archive_rows(c: Clone, lane: str = FOO) -> dict: + """`archive-rows`: the row moved from `lanes/LANES.md` to + `lanes/archive/LANES-retired.md`, and its `lanes` row archived there.""" + d = Draft(c, LA5, "archive-rows") + line_no, text = row_line(c, lane) + d.delete(LANES_MD, line_no) + d.row(RETIRED_MD, lane, text, d.append(RETIRED_MD, text), archived=True) + return d.entry() + + +def stray_import(c: Clone, lane: str = LA5, obj: str = OBJ5_FREE) -> dict: + """A `stray-import` (`import --single`), as T221's: a stray commit's line, + here a claim, taken into the register.""" + d = Draft(c, lane, "import --single", kind="stray-import", git_commit=STRAY_COMMIT) + d.log("CLAIMED", lane, obj) + return d.entry() + + +def stray_revert(c: Clone) -> dict: + """A `stray-revert` (`render --revert`), as T221's: no edit.""" + return {"journal": c.journal("stray-revert", "render --revert", OPERATOR, git_commit=STRAY_COMMIT, + message="render --revert (T232)"), "not_after": c.not_after()} + + +def rollback_entry(c: Clone) -> dict: + """The rollback's entry (S5's step 1; TP's AT-SET fixture: "`commit_write` + with a `rollback` entry, as REGISTER-WRITER"), in T304's form, line R's + `rollback_entry` in `lanes-index` (merge14 on): `journal` with kind and + verb `rollback`; one edit, the `remove` of `lanes/STORE`; its one `files` + row, not present; `not_after`. T231's schema module sends the same entry. + The operator sends it, as the cutover's.""" + return {"journal": c.journal("rollback", "rollback", OPERATOR, message="rollback (T232)"), + "not_after": c.not_after(), + "edits": [{"ord": 1, "op": "remove", "path": STORE}], + "files": [{"path": STORE, "present": False}]} + + +def act5(c: Clone, base: int, expect, act_id: str = ACT_ID) -> dict: + """The entry's `act`, as `register_act_of_env` returns it.""" + return {"id": act_id, "base": base, "generation": c.switch()["generation"], "expect": expect} + + +# ------------------------------------------------------------ the reads +def tables(c: Clone) -> Dict[str, str]: + """Each table's digest, row by row.""" + parts = ", ".join(f"'{t}', (SELECT md5(coalesce(string_agg(x::text, ',' ORDER BY x::text), '')) " + f"FROM {REGISTER}.{t} x)" for t in TABLES5) + return c.json(f"SELECT json_build_object({parts});") + + +def scopes_at(c: Clone, seq: int) -> List[str]: + return c.json(f"SELECT coalesce(json_agg(scope ORDER BY scope), '[]') FROM {REGISTER}.journal_scopes " + f"WHERE seq = {seq};") + + +def act_ids(c: Clone) -> Dict[int, Optional[str]]: + said = c.json(f"SELECT coalesce(json_object_agg(seq, act_id), '{{}}') FROM {REGISTER}.journal;") + return {int(seq): act_id for seq, act_id in said.items()} + + +def orphans(c: Clone) -> int: + """`journal_scopes` rows at a seq the journal no longer holds.""" + return int(c.value(f"SELECT count(*) FROM {REGISTER}.journal_scopes s WHERE NOT EXISTS " + f"(SELECT 1 FROM {REGISTER}.journal j WHERE j.seq = s.seq);")) + + +def refused5(c: Clone, entry: dict, code: str, detail: Optional[str], first: Optional[str] = None) -> List[str]: + """`Clone.refused`, and every table as it was; `first`, where given, is + the first stderr line's message exactly.""" + before = tables(c) + lines = c.refused(entry, code, detail) + if first is not None: + assert lines[0] == f"ERROR: {code}: {first}", lines + after = tables(c) + assert after == before, f"the refused call changed {[t for t in TABLES5 if after[t] != before[t]]}" + return lines + + +def lr5(clones) -> Clone: + """A clone on `primary`, with no hold on the cases' objects.""" + c = primary(clones) + held = c.value(f"SELECT count(*) FROM {REGISTER}.object_holds WHERE object IN ('{OBJ5}', '{OBJ5_ALIAS}', " + f"'{OBJ5_FREE}') OR spellings && ARRAY['{OBJ5}', '{OBJ5_ALIAS}', '{OBJ5_FREE}'];") + assert held == "0", f"{IMPORTED} holds {held} row(s) on {OBJ5} or {OBJ5_FREE}, which the cases claim" + return c + + +def estate5(clones) -> Clone: + """AT-LR009's register (TP:4855-4858), on primary: L1, L2 and L3, each with + a row holding its session uuid and a log opened by `STARTED`; an alias + line; a row named `foo-03`, holding none; and an object, `opensoft/r9#41`, + that L2 claimed five hours before. One write, before every case's base.""" + c = lr5(clones) + d = Draft(c, LA5, "append-line") + for lane in (LA5, LB5, LC5): + text = row_text(lane, SID5[lane]) + d.row(LANES_MD, lane, text, d.append(LANES_MD, text)) + text = row_text(FOO, None, "RETIRED") + d.row(LANES_MD, FOO, text, d.append(LANES_MD, text)) + d.append(ALIASES_TSV, f"L0-t232\t{LA5}\t2026-11-10T08:00:00Z") + for lane in (LA5, LB5, LC5): + d.log("STARTED", lane, f"lane:{lane}") + d.log("CLAIMED", LB5, OBJ5, minutes_ago=300) + c.committed(d.entry()) + return c + + +@pytest.fixture() +def head_holder(pg_cluster): + """A superuser session holding the head row (`SELECT … FOR UPDATE`), + confirmed in `pg_stat_activity`, rolled back at the test's end.""" + started: List[subprocess.Popen] = [] + + def hold(c: Clone, appname: str = "t232-holder") -> subprocess.Popen: + proc = c.db.session(SUPERUSER, appname) + proc.stdin.write(f"BEGIN;\nSELECT 1 FROM {REGISTER}.register_head WHERE id = 1 FOR UPDATE;\n") + proc.stdin.flush() + started.append(proc) + deadline = time.monotonic() + 20 + while c.value(f"SELECT count(*) FROM pg_stat_activity WHERE application_name = '{appname}' " + "AND state = 'idle in transaction';") != "1": + assert time.monotonic() < deadline, "the holder never took the head row" + time.sleep(0.05) + return proc + + yield hold + for proc in started: + if proc.poll() is None: + try: + proc.stdin.write("ROLLBACK;\n") + proc.stdin.close() + except (BrokenPipeError, ValueError): + pass + try: + proc.wait(timeout=20) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait() + + +# ------------------------------------------------------------ AT-LR5: LR005's rules +def test_lr005_refuses_a_claim_whose_spellings_meet_another_lanes_live_claim(clones): + """D19: L1 claims `opensoft/r9#41`; L2's claim of `r9#41`, another object + whose spellings meet L1's, is LR005 `claim-spellings`, and nothing is + written.""" + c = lr5(clones) + c.committed(claim5(c, LA5, OBJ5)) + refused5(c, claim5(c, LB5, OBJ5_ALIAS), "LR005", "claim-spellings", + "a claim's spellings overlap another lane's live claim (D19)") + + +def test_lr005_refuses_a_hold_over_another_lanes_open_taker(clones): + """D18: L1's HOLD on `opensoft/r9` commits; L2's later HOLD on it is + LR005 `main-held`, and nothing is written.""" + c = lr5(clones) + c.write(LA5, hold(c, LA5, spellings=REPO_SPELLINGS[KEY])) + refused5(c, c.append(LB5, *hold(c, LB5, spellings=REPO_SPELLINGS[KEY])), "LR005", "main-held", + "another lane holds main for this repository first (D18)") + assert c.open_takes(LA5, "hold") == [[KEY, None]] and c.open_takes(LB5, "hold") == [] + + +@pytest.mark.parametrize("case", ["hold-key-first", "landing-key-first", "hold-alias-first", + "landing-alias-first"]) +def test_two_spellings_of_one_repository_hold_one_main(clones, case): + """Lane 1's v2.3, in four forms: L2 takes one spelling of the repository, + then L4 the other (`r9` after `opensoft/r9`, or the reverse), each line's + `repo_key` the canonical key and its `repo_spellings` both; L4's take is + LR005 `main-held`, and main has one taker.""" + c = lr5(clones) + kind, first = case.split("-", 1) + typed = [KEY, ALIAS] if first == "key-first" else [ALIAS, KEY] + pr = None if kind == "hold" else PR + slot = "HOLD" if kind == "hold" else "LANDING" + + def take(lane: str, repo: str) -> Tuple[str, dict]: + return line(c, slot, lane, repo=repo, key=KEY, spellings=REPO_SPELLINGS[repo], pr=pr) + + c.write(LB5, take(LB5, typed[0])) + refused5(c, c.append(LD5, *take(LD5, typed[1])), "LR005", "main-held", + "another lane holds main for this repository first (D18)") + assert c.open_takes(LB5, kind) == [[KEY, pr]] and c.open_takes(LD5, kind) == [] + + +def test_lr005_rolls_back_the_head_and_every_row(clones): + """LR005 is raised after the head moved, the journal row went in, the + edits applied and the structured rows went in (steps 4 to 7): the head, + the journal, its edits and files, `doc_lines`, `log_lines` and + `object_holds` all equal their values before, and the write id is absent.""" + c = lr5(clones) + c.committed(claim5(c, LA5, OBJ5)) + head, before = c.head(), tables(c) + proc, write_id = c.commit(claim5(c, LB5, OBJ5_ALIAS)) + assert proc.returncode == 3 and proc.stderr.startswith("ERROR: LR005: "), proc.stderr + after = tables(c) + for table in TABLES5: + assert after[table] == before[table], f"{table} changed under LR005" + assert c.head() == head + assert c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE seq > {head} OR write_id = '{write_id}';") == "0" + assert c.value(f"SELECT count(*) FROM {REGISTER}.doc_lines WHERE path = '{log_path(LB5)}';") == "0" + + +def test_lr005_does_not_fire_on_a_released_claims_spellings(clones): + """L1 claims `opensoft/r9#41` and releases it; L2's claim of `r9#41` then + commits: a released hold carries no spellings and is no live claim.""" + c = lr5(clones) + c.committed(claim5(c, LA5, OBJ5)) + c.committed(claim5(c, LA5, OBJ5, verb="RELEASED")) + seq = c.committed(claim5(c, LB5, OBJ5_ALIAS)) + said = c.json(f"SELECT json_agg(json_build_array(object, lane_key, verb, superseded_by, spellings) " + f"ORDER BY object, lane_key) FROM {REGISTER}.object_holds WHERE object IN ('{OBJ5}', '{OBJ5_ALIAS}');") + assert said == [[OBJ5, LA5.lower(), "RELEASED", None, None], + [OBJ5_ALIAS, LB5.lower(), "CLAIMED", None, [OBJ5_ALIAS, OBJ5]]], said + assert seq == c.head() + + +@pytest.mark.parametrize("token", ["one-live-claim", "claim-spellings", "main-held"]) +def test_each_claim_and_main_rule_lr005_carries_its_detail_token(clones, token): + """U7: each rule's LR005 names it in its message, and its DETAIL is + exactly the rule's token: L2's claim of L1's object (one_live_claim's + 23505, raised again), of its other spelling (D19), and L2's HOLD behind + L1's (D18).""" + c = lr5(clones) + if token == "main-held": + c.write(LA5, hold(c, LA5, spellings=REPO_SPELLINGS[KEY])) + refused5(c, c.append(LB5, *hold(c, LB5, spellings=REPO_SPELLINGS[KEY])), "LR005", token, + "another lane holds main for this repository first (D18)") + return + c.committed(claim5(c, LA5, OBJ5)) + if token == "claim-spellings": + refused5(c, claim5(c, LB5, OBJ5_ALIAS), "LR005", token, + "a claim's spellings overlap another lane's live claim (D19)") + else: + lines = refused5(c, claim5(c, LB5, OBJ5), "LR005", token) + assert lines[0].startswith("ERROR: LR005: the object is held (one_live_claim): "), lines + + +# ------------------------------------------------------------ AT-LR009: the act's key +MALFORMED_ACTS = { + "id-with-an-at": {"id": "act@t232"}, + "id-with-a-space": {"id": "act t232"}, + "id-not-a-string": {"id": 232}, + "base-a-string": {"base": "head"}, + "base-negative": {"base": -1}, + "base-not-whole": {"base": 1.5}, + "generation-not-a-uuid": {"generation": "generation-t232"}, + "expect-not-a-list": {"expect": "row:l1-t232"}, + "element-not-a-string": {"expect": [232]}, + "element-a-star": {"expect": ["*"]}, + "element-row-with-no-name": {"expect": ["row:"]}, + "element-with-white-space": {"expect": ["row:l1 t232"]}, + "on-an-import": None, +} + + +@pytest.mark.parametrize("case", list(MALFORMED_ACTS)) +def test_a_malformed_act_is_refused_lr004_before_the_head_lock(clones, head_holder, case): + """Each of the test plan's rewrites of the `act` key, and an `act` on an + import entry, by a direct call: LR004, `the entry's act is not well + formed`, while a session holds the head row throughout, so a check after + the lock would end 55P03 at the call's lock_timeout; nothing is written.""" + c = lr5(clones) + head = c.head() + if case == "on-an-import": + edits, files, _ = c.appended(LANES_MD, ["# Lanes (T232)"], create=True) + entry = {"journal": c.journal("import", "import", OPERATOR, git_commit=GIT_COMMIT, git_tree=GIT_TREE), + "not_after": c.not_after(), "edits": edits, "files": files, + "act": act5(c, head, [f"row:{LA5.lower()}"])} + else: + act = act5(c, head, [f"row:{LA5.lower()}"]) + act.update(MALFORMED_ACTS[case]) + if case == "base-a-string": + act["base"] = str(head) + entry = claim5(c, LA5, OBJ5_FREE, act=act) + before = tables(c) + head_holder(c) + write_id = str(uuid.uuid4()) + proc = c.call(LOCKED_SQL, base=head, wid=write_id, client=c.client(), gen=c.switch()["generation"], + entry=json.dumps(entry, ensure_ascii=False)) + lines = proc.stderr.splitlines() + assert proc.returncode == 3, f"exit {proc.returncode}: {proc.stderr.strip()}" + assert lines[:1] == ["ERROR: LR004: the entry's act is not well formed"], lines + assert "55P03" not in proc.stderr, proc.stderr + assert tables(c) == before + assert c.value(f"SELECT count(*) FROM {REGISTER}.journal WHERE write_id = '{write_id}';") == "0" + + +def test_act_id_is_the_acts_id_on_a_write_and_null_on_every_other_entry(clones): + """`journal.act_id`: a write made in an act stores the act's id as + given; a write outside any act, the import, the cutover, a + `stray-import`, a `stray-revert` and a rollback store NULL.""" + c = estate5(clones) + said = c.json(f"SELECT json_object_agg(kind, seq) FROM (SELECT kind, max(seq) AS seq FROM {REGISTER}.journal " + "WHERE kind IN ('import', 'cutover') GROUP BY kind) k;") + in_act = c.committed(claim5(c, LA5, OBJ5_FREE, act=act5(c, c.head(), []))) + outside = c.committed(set_row_state(c, LA5)) + stray = c.committed(stray_import(c, LC5, f"{OTHER_KEY}#10")) + revert = c.committed(stray_revert(c)) + ids = act_ids(c) + assert ids[in_act] == ACT_ID + for name, seq in (("a write outside any act", outside), ("the import", said["import"]), + ("the cutover", said["cutover"]), ("a stray-import", stray), ("a stray-revert", revert)): + assert ids[seq] is None, f"{name}, seq {seq}, stores act_id {ids[seq]!r}" + rollback = c.committed(rollback_entry(c)) + assert act_ids(c)[rollback] is None + + +def test_the_act_id_checks_refuse_an_act_id_on_another_kind_and_a_malformed_one(clones): + """As the admin under `SET ROLE lanes_register_owner`: `act_id` set on the + cutover's row fails 23514 on `journal_act_id_write`, and a write's set to + `a@b` or `a b` fails 23514 on `journal_act_id_check`.""" + c = estate5(clones) + write = c.committed(claim5(c, LA5, OBJ5_FREE, act=act5(c, c.head(), []))) + cases = [("kind = 'cutover'", ACT_ID, "journal_act_id_write"), + (f"seq = {write}", "a@b", "journal_act_id_check"), + (f"seq = {write}", "a b", "journal_act_id_check")] + for where, value, constraint in cases: + proc = c.db.psql("\\set VERBOSITY verbose\nSET ROLE lanes_register_owner;\n" + f"UPDATE {REGISTER}.journal SET act_id = '{value}' WHERE {where};\n", check=False) + assert proc.returncode != 0 and "ERROR: 23514: " in proc.stderr, f"{where} = {value!r}: {proc.stderr}" + assert f'violates check constraint "{constraint}"' in proc.stderr, proc.stderr + assert act_ids(c)[write] == ACT_ID + + +@pytest.mark.parametrize("case", ["set-row-state", "lane-kind-line", "claim", "takeover", "rename-lane", + "archive-rows"]) +def test_each_entry_stores_the_keys_its_lines_derive(clones, case): + """Steps 5a and 8a: each committed entry's `journal_scopes` holds exactly + the keys the scopes table's rules give its lines, step 5a's old side and + step 8a's new side, `objects:` for every lane holding an object it wrote + a hold for, and never a key the client sent.""" + c = estate5(clones) + l1, l2, l3, l2b = (lane.lower() for lane in (LA5, LB5, LC5, LB5_NEW)) + if case == "set-row-state": + entry, want = set_row_state(c, LA5), {f"row:{l1}", f"sid:{SID5[LA5]}"} + elif case == "lane-kind-line": + d = Draft(c, LA5, "log") + d.log("PAUSED", LA5, f"lane:{LA5}") + entry, want = d.entry(), {f"bind:{l1}", f"sid:{SID5[LA5]}"} + elif case == "claim": + entry, want = claim5(c, LA5, OBJ5_FREE), {f"objects:{l1}"} + elif case == "takeover": + d = Draft(c, LC5, "claim") + d.log("TAKEOVER", LC5, OBJ5) + entry, want = d.entry(), {f"objects:{l3}", f"objects:{l2}"} + elif case == "rename-lane": + entry = rename_lane(c) + want = {f"row:{l2}", f"row:{l2b}", f"sid:{SID5[LB5]}", f"bind:{l2}", f"bind:{l2b}", + f"objects:{l2}", f"objects:{l2b}", f"alias:{l2}", f"alias:{l2b}"} + else: + entry, want = archive_rows(c), {f"row:{FOO}", f"archive:{FOO}", "archive:foo-3"} + seq = c.committed(entry) + got = scopes_at(c, seq) + assert set(got) == want, f"[{case}] stores {got}, not {sorted(want)}" + if case == "claim": + assert not [k for k in got if k.startswith("bind:")], got + + +def test_an_import_a_cutover_and_a_rollback_store_only_the_star_key(clones): + """`*` is the one key of an import, a cutover and a rollback.""" + c = clones() + imported = c.head() + assert c.value(f"SELECT kind FROM {REGISTER}.journal WHERE seq = {imported};") == "import" + assert scopes_at(c, imported) == ["*"] + cutover = c.to_primary() + assert scopes_at(c, cutover) == ["*"] + rollback = c.committed(rollback_entry(c)) + assert scopes_at(c, rollback) == ["*"] + + +def test_a_stray_import_stores_its_keys_and_a_stray_revert_none(clones): + """A `stray-import` stores its lines' keys as a write does; a + `stray-revert` stores none.""" + c = estate5(clones) + stray = c.committed(stray_import(c)) + assert scopes_at(c, stray) == [f"objects:{LA5.lower()}"] + revert = c.committed(stray_revert(c)) + assert scopes_at(c, revert) == [] + + +def test_an_aborted_cutover_and_a_reimport_leave_no_scopes_at_a_deleted_seq(clones): + """The cascade: a cutover aborted by `switch_to_files`, then an import's + reset before any confirmed cutover, each succeed and leave no + `journal_scopes` row at a seq the journal no longer holds.""" + c = clones() + imported = c.head() + edits, files, _ = c.appended(STORE, ["primary"]) + cutover = c.committed({"journal": c.journal("cutover", "cutover", OPERATOR), "not_after": c.not_after(), + "edits": edits, "files": files}) + assert scopes_at(c, cutover) == ["*"] + proc = c.call(ABORT_SQL, client=c.client(), seq=cutover, lane=OPERATOR, ws=WS) + assert (proc.returncode, proc.stdout.strip()) == (0, str(imported)), proc.stderr + assert scopes_at(c, cutover) == [] and orphans(c) == 0 + edits, files, _ = c.appended(LANES_MD, ["# Lanes (T232's import)"], create=True) + seq = c.committed({"journal": c.journal("import", "import", OPERATOR, git_commit=GIT_COMMIT, git_tree=GIT_TREE), + "not_after": c.not_after(), "edits": edits, "files": files}) + assert scopes_at(c, seq) == ["*"] and orphans(c) == 0 + assert c.value(f"SELECT count(*) FROM {REGISTER}.journal_scopes WHERE seq > {seq};") == "0" + + +def test_lr009_names_the_first_changed_scope_its_seq_lane_and_verb(clones): + """Step 3b: with two entries outside the act after its base, at k1 < k2, + each touching `row:l1`, LR009 names k1, its lane and its verb; with one + entry touching two listed scopes, it names the first in byte order.""" + c = estate5(clones) + row1, sid1 = f"row:{LA5.lower()}", f"sid:{SID5[LA5]}" + base = c.head() + k1 = c.committed(set_row_state(c, LA5, "PAUSED")) + k2 = c.committed(set_row_state(c, LA5, "ACTIVE")) + assert k1 < k2 + refused5(c, claim5(c, LA5, OBJ5_FREE, act=act5(c, base, [row1])), "LR009", None, + f"expectation: {row1} changed at seq {k1} by {LA5}/set-row-state") + base = c.head() + k3 = c.committed(set_row_state(c, LA5, "PAUSED")) + assert {row1, sid1} <= set(scopes_at(c, k3)) + refused5(c, claim5(c, LA5, OBJ5_FREE, act=act5(c, base, [sid1, row1])), "LR009", None, + f"expectation: {row1} changed at seq {k3} by {LA5}/set-row-state") + + +def test_an_entry_outside_any_act_refuses_an_acts_write_lr009(clones): + """Step 3b: an entry after the act's base, made outside any act + (`act_id` NULL), that touched `row:l1` refuses the act's write with + LR009; an `expect` sent as `ROW:L1` is compared in lower case and refused + the same way.""" + c = estate5(clones) + row1 = f"row:{LA5.lower()}" + base = c.head() + k = c.committed(set_row_state(c, LA5)) + assert act_ids(c)[k] is None + for expect in ([row1], [row1.upper()]): + refused5(c, claim5(c, LA5, OBJ5_FREE, act=act5(c, base, expect)), "LR009", None, + f"expectation: {row1} changed at seq {k} by {LA5}/set-row-state") + + +def test_a_star_key_after_the_base_is_reported_as_the_first_listed_scope(clones): + """`*` after the base: an act based at the import, before the cutover's + `*`, is refused LR009 naming its first listed scope, not the first in + byte order, at the cutover's seq, by the cutover's lane and verb.""" + c = clones() + base = c.head() + cutover = c.to_primary() + assert cutover == base + 1 + expect = [f"row:{LC5.lower()}", f"row:{LA5.lower()}"] + refused5(c, claim5(c, LA5, OBJ5_FREE, act=act5(c, base, expect)), "LR009", None, + f"expectation: {expect[0]} changed at seq {cutover} by {OPERATOR}/cutover") + + +def test_an_empty_expectation_records_act_id_and_checks_nothing(clones): + """An empty `LANES_EXPECT`, sent as `expect: []`, records the act's id and + checks nothing, so the write commits after an entry that touched + `row:l1`.""" + c = estate5(clones) + base = c.head() + c.committed(set_row_state(c, LA5)) + seq = c.committed(claim5(c, LA5, OBJ5_FREE, act=act5(c, base, []))) + assert seq == c.head() and act_ids(c)[seq] == ACT_ID + + +def test_lr002_comes_before_lr009_after_a_rollback(clones): + """A direct call after a rollback's entry has set the switch to `files` + gets LR002, not LR009, though an entry after its base touched its listed + scope: step 3 comes before step 3b.""" + c = estate5(clones) + base = c.head() + c.committed(set_row_state(c, LA5)) + c.committed(rollback_entry(c)) + sw = c.switch() + assert (sw["mode"], sw["marker_state"]) == ("files", "removing"), sw + refused5(c, claim5(c, LA5, OBJ5_FREE, act=act5(c, base, [f"row:{LA5.lower()}"])), "LR002", None, + "register switch is files with the marker removing") + + +# ============================================================ PHASE W, NO ENGINE: `register commit`'s path refusal (T243d) +# +# LANE 1'S FOLLOW-UP 2 (line R's merge13, `3a30ba89`, tag `l1-007-R-pathre`; +# #165 6783763467): `register commit` refuses with 2, before the store opens, +# a path that holds a control character, U+0085, U+2028 or U+2029, where +# `changes.tsv` stages it and on either side of a `renames.tsv` line +# (`register_path_unrenderable`, called from `read_attempt_dir`). No render of +# the register could write such a path as one `file=` row, and the database's +# CHECKs admit every character but `/`, so this refusal is the only guard. +# Lane 1: "no test covers the attempt directory's path checks". +# +# NO ENGINE. T203's flipped copy runs `register commit` under a `mode=primary` +# configuration whose URLs name 127.0.0.1, with a `psql` first on `PATH` that +# only logs its calls: "before the store opens" is that log left empty. Each +# attempt directory is contract section 3.3's, whole, as lane 1's hand checks +# build it (`runs/R-pathre/mkattempt.py`): a scratch tree at seq n, then +# `snapshot`, `not_after`, `changes.tsv`, `images/`, `files`, `msg` and +# `author`. A `changes` case adds a log at the path (an A line). A `renames` +# case appends a line to its lane's log (an M line) and names the path on one +# side of a `renames.tsv` line that `changes.tsv` does not name, since a side +# that `changes.tsv` also stages is refused first, as `changes.tsv stages`. +# The place asserted is lane 1's text: the file, the side, and the path as +# `repr` prints it. On merge12 a `changes` case reaches the store, and a +# `renames` case is refused by the D and A check, which names no side, so +# each FAILS there by its own assertion; from merge13 on each passes. + +PATH_LANE, PATH_WS, PATH_N = "L1-t243d", "ws-t243d", 41 +#: Each case: the file that names the path, the side of the `renames.tsv` +#: line (None for `changes.tsv`), and the character the path holds. +UNRENDERABLE = { + "changes-u0001": ("changes.tsv", None, 0x0001), + "changes-u0085": ("changes.tsv", None, 0x0085), + "changes-u2028": ("changes.tsv", None, 0x2028), + "changes-u2029": ("changes.tsv", None, 0x2029), + "renames-old-u2028": ("renames.tsv", "", 0x2028), + "renames-new-u2029": ("renames.tsv", "", 0x2029), +} +#: The prefixes of the test's own environment that the run of `lanes-index` does not inherit. +PATH_DROPPED = ("GIT_", "PG", "LANES_", "TMUX", "RULE6_", "FAKE_PSQL", "CLAUDE_", "PROJECTS_ROOT", + "XDG_", "OPENREPOTOOLS_BIN_DIR", "AGENT_PROTOCOL_ROOT") + + +def unrenderable_attempt(root: Path, case: str, env: dict) -> Tuple[Path, List[str], str]: + """`case`'s whole attempt directory under `root`, the `register commit` + argv that sends it, and the place its refusal must name.""" + where, side, code = UNRENDERABLE[case] + bad = f"lanes/log/a{chr(code)}b.md" + att = root / "attempt" + tree = att / "tree" + tree.mkdir(parents=True) + sid = session_of(PATH_LANE) + + def log_line(verb: str, lane: str, utc: str, what: str) -> str: + return f"{verb} {EM} lane {lane}, session {sid}@{PATH_WS}, {utc}, {what}\n" + + def log_head(lane: str) -> str: + return f"# lane {lane} {EM} object log (lane-collision-protocol Amendment 7)\n" + + log = f"lanes/log/{PATH_LANE}.md" + register = {"lanes/LANES.md": "# Lanes\n", "lanes/repos.tsv": "# alias\towner/repo\nr9\topensoft/r9\n", + "lanes/aliases.tsv": "# old\tnew\tutc\n", "lanes/log/README.md": "# logs\n", + log: log_head(PATH_LANE) + log_line("STARTED", PATH_LANE, "2026-11-01T00:00:00Z", + f"lane:{PATH_LANE} home opensoft/r9")} + for path, text in register.items(): + (tree / path).parent.mkdir(parents=True, exist_ok=True) + (tree / path).write_bytes(text.encode("utf-8")) + run_git(tree, "init", "-q", "-b", "main", env=env) + run_git(tree, "add", "-A", env=env) + run_git(tree, "commit", "-q", "-m", f"register snapshot at seq {PATH_N}", env=env) + if where == "changes.tsv": + path, verb, old = bad, "log", None + new = (log_head("added") + log_line("STARTED", "added", "2026-11-09T12:00:00Z", + "lane:added home opensoft/r9")).encode("utf-8") + else: + path, verb, old = log, "claim", register[log].encode("utf-8") + new = old + log_line("CLAIMED", PATH_LANE, "2026-11-09T12:00:00Z", "opensoft/r9#7").encode("utf-8") + sides = (bad, f"lanes/log/{PATH_LANE}-moved.md") if side == "" else (log, bad) + (att / "renames.tsv").write_bytes(("\t".join(sides) + "\n").encode("utf-8")) + image = att / "images" / path + image.parent.mkdir(parents=True) + image.write_bytes(new) + new_blob = run_git(tree, "hash-object", "--no-filters", str(image), env=env).strip() + base_blob = "0" * 40 if old is None else run_git(tree, "rev-parse", f"HEAD:{path}", env=env).strip() + (att / "changes.tsv").write_bytes( + f"{'A' if old is None else 'M'}\t{path}\t{base_blob}\t{new_blob}\n".encode("utf-8")) + (att / "files").write_bytes(f"{path}\t1\t{hashlib.sha256(new).hexdigest()}\n".encode("utf-8")) + write_id, generation, clock = str(uuid.uuid4()), str(uuid.uuid4()), int(time.time()) + repos_blob = run_git(tree, "rev-parse", "HEAD:lanes/repos.tsv", env=env).strip() + snapshot = {"format": 1, "generation": generation, "write_id": write_id, "switch": "primary", + "marker": "present", "n": PATH_N, "clock": clock, "not_after": clock + 10, "schema": 1, + "repos_blob": repos_blob, "c": 2, "n_a": 1} + (att / "snapshot").write_text("".join(f"{k}={v}\n" for k, v in snapshot.items()), encoding="utf-8") + (att / "not_after").write_text(f"{clock + 10}\n", encoding="utf-8") + (att / "msg").write_text(f"LANES({PATH_LANE}@{PATH_WS}): {verb} {EM} T243d's case\n", encoding="utf-8") + (att / "author").write_text("T243d Tester \n", encoding="utf-8") + argv = ["register", "commit", "--write-id", write_id, "--generation", generation, "--lane", PATH_LANE, + "--verb", verb, "--args-sha256", hashlib.sha256(case.encode("utf-8")).hexdigest(), + "--workstation", PATH_WS, str(att)] + place = f"changes.tsv stages {bad!r}" if side is None else f"renames.tsv line 1's {side} is {bad!r}" + return att, argv, place + + +@CLIENT +@pytest.mark.parametrize("case", list(UNRENDERABLE)) +def test_register_commit_refuses_an_unrenderable_path_with_2_before_the_store_opens(tmp_path, case): + """Lane 1's follow-up 2 (#165 6783763467): an attempt directory whose + `changes.tsv` stages a path holding U+0001, U+0085, U+2028 or U+2029 + ([changes-*]), or whose `renames.tsv` line holds U+2028 on its `` + side ([renames-old-u2028]) or U+2029 on its `` side + ([renames-new-u2029]), makes `register commit` exit 2, with stderr naming + the file, the side and the path, and no `psql` call: the store is never + opened.""" + env = {k: v for k, v in os.environ.items() if not k.startswith(PATH_DROPPED)} + (tmp_path / "home").mkdir() + env.update(HOME=str(tmp_path / "home"), XDG_STATE_HOME=str(tmp_path / "state"), + XDG_CONFIG_HOME=str(tmp_path / "config"), GIT_CONFIG_NOSYSTEM="1", + GIT_CONFIG_GLOBAL=os.devnull, GIT_AUTHOR_NAME="T243d", GIT_AUTHOR_EMAIL="t243d@example.invalid", + GIT_COMMITTER_NAME="T243d", GIT_COMMITTER_EMAIL="t243d@example.invalid", LC_ALL="C") + _att, argv, place = unrenderable_attempt(tmp_path, case, env) + copy = flipped_copy(LANES_INDEX, tmp_path / "bin", True) + (tmp_path / "bin" / "repos.tsv").write_text("# alias\towner/repo\n", encoding="utf-8") + conf = tmp_path / "lanes-index.conf" + conf.write_text("mode=primary\nregister_url=postgresql://lanes_w_t243d@127.0.0.1:9/t243d\n" + "reader_url=postgresql://lanes_r_t243d@127.0.0.1:9/t243d\n", encoding="utf-8") + conf.chmod(0o600) + calls = tmp_path / "psql.log" + _write(tmp_path / "fakebin" / "psql", f'#!/usr/bin/env bash\nprintf "%s\\n" "$*" >> "{calls}"\nexit 2\n') + env.update(LANES_INDEX_CONFIG=str(conf), + PATH=str(tmp_path / "fakebin") + os.pathsep + env.get("PATH", os.defpath)) + r = subprocess.run([sys.executable, str(copy), *argv], cwd=str(tmp_path), env=env, capture_output=True, + encoding="utf-8", errors="replace", timeout=120, stdin=subprocess.DEVNULL, check=False) + sent = calls.read_text(encoding="utf-8").splitlines() if calls.exists() else [] + assert r.returncode == 2, ( + f"[{case}] register commit exited {r.returncode}, not 2, after {len(sent)} psql calls: {r.stderr[-900:]}") + assert sent == [], f"[{case}] register commit called psql before it refused: {sent}" + assert place in r.stderr, f"[{case}] stderr does not name {place!r}: {r.stderr[-900:]}" From a69979ec452e5e9fd5715a3209359e9f7b2a0e62 Mon Sep 17 00:00:00 2001 From: Brett Heap <1513478+brettheap@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:57:49 +0000 Subject: [PATCH 2/2] P04 follow-up 1: T243x, which lane 3 ruled into S007-11a, T243's set, and so into P04 (opensoft/openRepoTools#165 6894116246, corrected in 6894568487): burst() names a writer whose mode question failed and whose claim went to git in files mode This commit is added to P04's branch on its head 8b6e7fa09379334a01d1359e4913737186c6a791, with no force-push: the branch keeps its cut's one commit, and a later commit carries its own explicit message (landing note 23). The ruling. Lane 3 ruled that T243x's set is in P04 (opensoft/openRepoTools#165 comment 6894116246, on lane 2's 6890923175 and 6893203365): T243x is T243's follow-up in the order module. Lane 1 adds lane 2's T243x, 961d8c356081878643f5a8580c65f66293156f84, to this PR (opensoft/openRepoTools-code#91) as a commit of its own with its own message, and lane 3 reviews the new head. That ruling named S007-11, and lane 3 corrected it in comment 6894568487 (correction 1), in its words: "lane 3 wrote S007-11, but T243 is in S007-11a. T243x rides with T243, in S007-11a. Both sets are in P04, so nothing moves; only the part of P04's body that names it changes." So T243x is S007-11a's, and the PR body carries this text in S007-11a's part. What it is. T243x is lane 2's commit, built on N2's T223c d80af8610e215188dbc11c87ec27b802fd1041ed. Line R took it as merge164 9990baf05e9eb538f4fb82fac80b6ea1e9830bd7, tree fee0170117d7c9279cbebf861a0eb630015ab5be, on merge163 8acf1308538659d6ac12d690f310bdc292cb7aea. It changes tests/test_sql_register_order.py only, +29 -2. Why, in lane 2's words. Lane 1's c5 on line R's merge153 (verify/l1/m153-c5, run 43660748778, job 132649292204, commit d52d3836; lane 2's opensoft/openRepoTools#165 6888267089): test_ten_writers_released_together_all_commit_in_order failed on its journal, which held seqs 2 to 10, one row short. In the burst, repoW06-1 made no register call (attempts 0, calls '', 4.13 s, exit 0). Its mode question, lanes-index mode under lanes-edit.sh's own 2 s timer (reg_mode_query, R-merge158:lanes-edit.sh:6360), ran past the timer, so it wrote in files mode with one warning (reg_mode_files). T243's estates hold no lanes/STORE on origin, so T419's marker check let that write through to git with exit 0, and the journal never saw it. Lane 2's TW-27 reproduced it on merge153 and on merge151 by holding one writer's mode question past the timer, with CI's signature: the same warning, calls '', exit 0, seqs 2 to 10. On 4 CPUs under the burst, the slowest of 20 mode questions took 1.36 s and 1.55 s of the 2 s. The change, in tests/test_sql_register_order.py: - FILES_MODE_SAID: lanes-edit.sh's one fallback warning, with the reason it gives in group 1. - burst(), after its record and before its caller asserts anything, fails by name on any writer whose stderr holds that warning, or that exited 0 with no call in its shim's log, with each one's code and the tail of its stderr. The record names them too, as files_mode. - It changes how a failing burst fails, not whether it fails: such a burst already failed later, on the journal. It does not shut the window, which needs lanes-edit.sh's 2 s timer (T409; contract 2.4) or lanes-index's start-up cost changed; neither is this slice's. - burst()'s other callers (:2018, :2163, :2294) get the same check; in none of them can a writer legitimately exit 0 with no call either. No id changes and no gate-table row, so no -k census. Rule 11's three checks are owed for a new id or a table row, and this slice has neither. An offline AST reading of the module before and after (TW-27's scratch) finds the same 67 test functions, with the same decorators (parametrize included) and arguments, and the same SQL_REGISTER_GATE_TABLE. Lane 1's own reading, of the module as P04's head holds it (blob 36b921cbc43481fdaed74b11661a061dff0cce28) and as this commit leaves it (blob 7da748bcd775634f2e989102120b95b293afa539), finds the same: 67 test functions with the same names, arguments and decorators, and a SQL_REGISTER_GATE_TABLE of 100 keys, equal as a literal, each key naming a test of the module. The one name added at module level is FILES_MODE_SAID. Lane 2's runs (tests/run.sh -rA, PostgreSQL 16, OPENREPOTOOLS_REQUIRE_PG=1; merge158 7e11b771 merged with this slice's scratch 3c69d3d6; lane 2's TW-27, wave 27): - red, with TW-27's scratch-only mutant, which holds one writer's mode question past the 2 s timer (run tree 575540ea), -k 'tw27 and slow': 1 failed, inside burst(), on repoW06-1: "1 burst writer(s) never reached the register: ... {'repoW06-1': 'did not answer within 2 s, and was stopped (exit 124)'}". - green (run tree 14b6ea8f), -k test_ten_writers_released_together_all_commit_in_order: 1 passed. How it was made here. cherry-pick -x of 961d8c35 onto 8b6e7fa0 applied with no conflict. T243x's parent, T223c d80af861, holds the order module as blob 36b921cbc43481fdaed74b11661a061dff0cce28, which P04's head holds too, and so do line R's merge158 7e11b771 and merge163 8acf1308, so the diff lands on the same base. Its patch-id, c41aade8aa1196fe079f749da528c8582fda22d2, is T243x's own. After it the module is blob 7da748bcd775634f2e989102120b95b293afa539, which equals line R's merge164 blob. The path is in T243's files: (T243 is S007-11a's) at spec main 775b72c9 (the merge of opensoft/openRepoTools-spec#60; tasks.md blob f56ad0a7, line 2332), where T243's task line is byte for byte the line it was at spec main c304e387 (tasks.md blob 4f806bfc, line 2332); the path is in T223's, T232's, T233's, T248's and T252's too. T243x has no task line of its own, so it adds no marker: tests/sql_register_landed/T243 is already in P04. (cherry picked from commit 961d8c356081878643f5a8580c65f66293156f84) Lane: openRepoTools-1 Co-Authored-By: Claude Opus 5.5 --- tests/test_sql_register_order.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/tests/test_sql_register_order.py b/tests/test_sql_register_order.py index 36b921cb..7da748bc 100644 --- a/tests/test_sql_register_order.py +++ b/tests/test_sql_register_order.py @@ -737,6 +737,13 @@ def test_the_for_lane_checks_refuse_a_for_lane_off_a_landing_expired_and_a_landi MEASURE_DEADLINE_S, D7_PER_P95 = 120, 10 UUID_RE = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" SEQ_SAID = re.compile(r"register seq ([0-9]+)") +#: A WRITE THAT RAN IN FILES MODE (T243x): `lanes-edit.sh`'s one warning where +#: its mode question failed, `lanes-index mode` under its own 2 s timer +#: (`reg_mode_query`, `R-merge158:lanes-edit.sh:6360`, then `reg_mode_files`), +#: with the failure in group 1. These estates hold no `lanes/STORE` on origin, +#: so T419's marker check lets that write through to git: it exits 0, and the +#: journal never sees it (merge153's c5, `verify/l1/m153-c5`; lane 2's TW-27). +FILES_MODE_SAID = re.compile(r"WARNING: `lanes-index mode` (.*?), so `[^`]*` runs in files mode") def session_of(lane: str) -> str: @@ -1405,7 +1412,15 @@ def burst(e: Estate, objects: Sequence[str], window_ms: int, extra: dict = None, so the case's own drain assertion decides a slow burst (T243u). The record ends with each writer's exit code, `codes`, so it shows how the burst ended, the measuring burst's among them (lane 2's RV26, NIT 7; - T243w).""" + T243w). + + A WRITER THAT NEVER REACHED THE REGISTER FAILS THE BURST HERE, after the + record and before its caller asserts anything, by name (T243x; lane 2's + TW-27): one whose stderr holds `FILES_MODE_SAID`, its mode question + failed and its claim written to git instead, or one that exited 0 with no + call in its shim's log. Neither is a burst's end, and without this the + case meets it only as a journal one row short (merge153's c5). The record + names them too, as `files_mode`.""" barrier = Barrier(e.root, len(objects)) arms = [dict(WINDOW, ms=window_ms)] writers = [e.launch(lane, claim(obj, "--no-github"), arms=arms, barrier=barrier, extra=extra) @@ -1418,6 +1433,13 @@ def burst(e: Estate, objects: Sequence[str], window_ms: int, extra: dict = None, drain = max(w.ended for w in writers) - released read = {w.lane: burst_calls(w) for w in writers} kinds = {lane: [call[0] for call in r[0].split()] for lane, r in read.items()} + fell = {} + for w in writers: + said = FILES_MODE_SAID.search(w.err) + if said: + fell[w.lane] = said.group(1) + elif w.code == 0 and not w.calls(): + fell[w.lane] = "no warning, and it exited 0 with no call in its shim's log" record(name, node, drain_s=round(drain, 2), margin_vs_30s=round(D7_S / max(drain, 0.01), 2), attempts={lane: k.count("s") for lane, k in kinds.items()}, reads={lane: k.count("r") + k.count("m") for lane, k in kinds.items()}, @@ -1425,7 +1447,12 @@ def burst(e: Estate, objects: Sequence[str], window_ms: int, extra: dict = None, calls={lane: r[0] for lane, r in read.items()}, losses={lane: r[1] for lane, r in read.items()}, commit_s={lane: r[2] for lane, r in read.items()}, longest_commit_s=max((x for r in read.values() for x in r[2]), default=None), - codes={w.lane: w.code for w in writers}) + files_mode=fell, codes={w.lane: w.code for w in writers}) + assert not fell, ( + f"{len(fell)} burst writer(s) never reached the register: `lanes-index mode`, asked under " + f"lanes-edit.sh's 2 s timer (`reg_mode_query`), failed, so each wrote its claim in files mode, to " + f"git, and nothing to the journal; this estate holds no lanes/STORE on origin to refuse it: {fell}. " + f"Their stderr: {[(w.lane, w.code, w.err[-400:]) for w in writers if w.lane in fell]}") return writers, drain