Repository navigation
Commit 1a0c899
authored
Read the chat-index mark inside its write transaction and keep the read-only window out of the index (#2713)
Written-by: Dev
Fixes #2711
Owner: Dev
Reviewer: Reviewer
## What
`core/src/host/chat-index.js` read a part's mark **outside** the
transaction that inserts the rows, `lines` had no uniqueness, and the
module neither checked `rt.readOnly()` nor had a writer guard — so two
indexers over one home both read `through = -1` and every message came
back twice, and a window another dsh keeps read-only still wrote
`chat-index.sqlite`.
- **The mark is read inside the write transaction.** `catchUp` re-reads
`marks.through` under `BEGIN IMMEDIATE` and skips events at or below it,
so the second writer's read sees the first one's commit. One part's
catch-up per transaction, as before — no giant catch-up holding the
lock.
- **`UNIQUE(session, seq)` is enforced by the database.** `lines` is now
a plain table with that constraint; the FTS5 table is contentless over
it (`content='lines', content_rowid='id'`) kept in step by insert/delete
triggers, and ranked queries join it for `rank`. Inserts are `INSERT OR
IGNORE`: one statement cannot be raced by the next writer, while a
check-then-insert would add a second place that can read a stale mark.
`INDEX_VERSION` 3 → 4.
- **A window that is not the home's writer does not write the index.**
`rt.readOnly()` (the `home-lock` promise) decides: such a window opens
`chat-index.sqlite` read-only and reads the rows the writer indexed — it
adds none and never creates, repairs or chmods the file. A missing,
damaged or older-version file answers null and `read_own_chat` scans the
logs instead.
- **Existing duplicates are repaired, not left to a manual path.** A
version-3 file can already hold one row per writer, and no later
catch-up removes it, so the version bump rebuilds it once (drop + lazy
reindex from the logs) and logs how many lines were stored more than
once. Chosen over "warn and document" because the doubled transcript is
otherwise permanent.
- Tests: `core/test/chat-index-writers.test.js` (real module, temp home,
real SQLite, fake Session log) — two indexers that both pass the mark
read before either commits, the serial control, a read-only window over
an existing index (bytes and mtime unchanged), a read-only window while
the writer is live, a home with no index, and a version-3 file already
duplicated. `core/test/history.test.js`'s pinned `termQuery` SQL follows
the joined schema.
**Red without the change**, with the same test file on the base blob of
`chat-index.js` (sha256 `9dd7a804…`): test 1 answers `[part 1 #0, #0,
#1, #1, #2, #2]` — 6 lines for 3 source messages — and tests 3, 4, 5 and
6 fail on a read-only window writing the index, creating one where there
is none, and leaving a version-3 file duplicated. Test 2 (the serial
control) passes either way.
`Ran at this tip:` (TZ=UTC; the seven blobs at
`1cfda2379d3d0784510e990793d80a1f50803e79` were byte-compared with this
working copy before the commands below ran)
```
$ cd core && node --import ./test/harness-resolve.mjs --test test/chat-index-writers.test.js \
test/history.test.js test/chat-anchors.test.js test/private-files.test.js test/install.test.js \
test/group-work.test.js test/steward.test.js test/archive-bot.test.js test/tool-schema.test.js
# tests 76, pass 76, fail 0
$ cd core && npm run lint # oxlint: 0 errors, 87 pre-existing warnings; arch-docs exits 1 here only for want of git
$ node core/scripts/build-client.mjs --check # ok (no output, exit 0)
$ node scripts/ds-bot-patches.mjs check # ok: 10 patches apply in order; core/ equals ds-bot/ + patches/ds-bot/
$ node scripts/lint-plugins.mjs # ok: packed @flybot/workflow imported
$ node .github/scripts/changelog.mjs check # ok: 5 fragment(s) in changelog.d/ are well formed
```
**Red re-checked here**: the same new test file against the base
`chat-index.js` (sha256
`9dd7a80414e4485998c83b299c65b04040ea3469b56e28232a360bcc2bd7afef`)
answers `tests 6, pass 1, fail 5` — while the tip blob answers `tests 6,
pass 6`.
`Not run`: full `npm run verify` (the task said not to), the rest of
`core/test/`, the plugin suites, the smoke test, and `npm run arch-docs`
/ `ds-bot-patches.mjs generate` (both need git, which this box does not
have) — `docs/subsystems/core-host.md` is updated in this change, and
the new file went whole into `patches/ds-bot/files/`. No client change.
## Checkpoint
<!-- flybot:checkpoint -->
{
"pr": 2713,
"claim": "Dev",
"issue": 2711,
"summary": "Two chat-index indexers over one home read the same part
mark before either committed, so every message was stored and returned
twice, and a window the home lock keeps read-only still wrote
chat-index.sqlite. The mark is now read inside the transaction that
writes the rows, and lines carries UNIQUE(session, seq) so the database
refuses a second row for one event. A read-only window opens the
existing index read-only and adds nothing. An index the old build
already duplicated is rebuilt once at start, and the log says how many
lines it found.",
"files": [
"core/src/host/chat-index.js",
"core/test/chat-index-writers.test.js",
"core/test/history.test.js",
"docs/subsystems/core-host.md",
"changelog.d/2711-chat-index-writer.fixed.md"
],
"checks": {
"ran": "cd core && node --import ./test/harness-resolve.mjs --test
test/chat-index-writers.test.js test/history.test.js
test/chat-anchors.test.js test/private-files.test.js
test/install.test.js test/group-work.test.js test/steward.test.js
test/archive-bot.test.js test/tool-schema.test.js; node
scripts/ds-bot-patches.mjs check; node core/scripts/build-client.mjs
--check; node scripts/lint-plugins.mjs; node
.github/scripts/changelog.mjs check",
"result": "green",
"at": "1cfda2379d3d0784510e990793d80a1f50803e79"
},
"next": "none — ready for review"
}
<!-- /flybot:checkpoint -->1 parent da0588a commit 1a0c899
7 files changed
Lines changed: 835 additions & 49 deletions
File tree
- changelog.d
- core
- src/host
- test
- docs/subsystems
- patches/ds-bot
- files/test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
47 | 52 | | |
48 | 53 | | |
49 | | - | |
| 54 | + | |
50 | 55 | | |
51 | 56 | | |
52 | 57 | | |
53 | 58 | | |
54 | | - | |
55 | | - | |
| 59 | + | |
| 60 | + | |
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
62 | 67 | | |
63 | | - | |
| 68 | + | |
64 | 69 | | |
65 | 70 | | |
66 | 71 | | |
| |||
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
75 | 85 | | |
76 | 86 | | |
77 | 87 | | |
| |||
104 | 114 | | |
105 | 115 | | |
106 | 116 | | |
| 117 | + | |
107 | 118 | | |
108 | 119 | | |
109 | | - | |
| 120 | + | |
110 | 121 | | |
111 | 122 | | |
112 | 123 | | |
113 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
114 | 135 | | |
115 | 136 | | |
116 | 137 | | |
117 | 138 | | |
118 | | - | |
| 139 | + | |
119 | 140 | | |
120 | 141 | | |
121 | 142 | | |
| |||
134 | 155 | | |
135 | 156 | | |
136 | 157 | | |
137 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
138 | 192 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
147 | 205 | | |
148 | 206 | | |
149 | 207 | | |
| 208 | + | |
150 | 209 | | |
151 | 210 | | |
152 | 211 | | |
153 | | - | |
| 212 | + | |
154 | 213 | | |
155 | 214 | | |
156 | 215 | | |
| |||
167 | 226 | | |
168 | 227 | | |
169 | 228 | | |
170 | | - | |
171 | | - | |
| 229 | + | |
| 230 | + | |
172 | 231 | | |
173 | 232 | | |
| 233 | + | |
| 234 | + | |
174 | 235 | | |
175 | 236 | | |
176 | 237 | | |
177 | | - | |
178 | | - | |
179 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
180 | 241 | | |
181 | 242 | | |
182 | 243 | | |
| |||
185 | 246 | | |
186 | 247 | | |
187 | 248 | | |
188 | | - | |
189 | | - | |
190 | 249 | | |
191 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
192 | 257 | | |
193 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
194 | 266 | | |
| 267 | + | |
195 | 268 | | |
196 | 269 | | |
197 | 270 | | |
| |||
0 commit comments