From 7ca6f0bf3e52a4e45cbe4fa091b5417b81578b52 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:40:25 +0800 Subject: [PATCH] fix: answer the palette's search from a read-only window's own log searchHomeChat answered only the rows the writer's index had reached, so a message already in the log past the mark was missing and more: 0 claimed there was nothing else. A read-only window now matches the events after each mark in memory and merges them with the index answer. The rows come from the same pass catchUp inserts (factored out as markedRows), so the palette reads the shape the index would hold; nothing is written, and more keeps its meaning of hits the cap left out (#3319). --- .../3319-palette-readonly-search.fixed.md | 1 + core/src/host/chat-index.js | 127 ++++++++++++--- core/test/chat-index-writers.test.js | 47 ++++++ docs/subsystems/core-host.md | 3 +- patches/ds-bot/0001-core-fixes.patch | 149 ++++++++++++++---- .../files/test/chat-index-writers.test.js | 47 ++++++ 6 files changed, 322 insertions(+), 52 deletions(-) create mode 100644 changelog.d/3319-palette-readonly-search.fixed.md diff --git a/changelog.d/3319-palette-readonly-search.fixed.md b/changelog.d/3319-palette-readonly-search.fixed.md new file mode 100644 index 000000000..f2cdda865 --- /dev/null +++ b/changelog.d/3319-palette-readonly-search.fixed.md @@ -0,0 +1 @@ +The command palette's **Messages (full text)** group now finds a message in a read-only window even when the other window that holds the folder has not indexed it yet: the host matches its own chat log past the index mark, without writing the index, so a message that already exists is no longer answered as missing with `more: 0` ([#3319](https://github.com/ThinkFlowLab/FlyBot/issues/3319)). diff --git a/core/src/host/chat-index.js b/core/src/host/chat-index.js index 7107bd5a6..2ca2699df 100644 --- a/core/src/host/chat-index.js +++ b/core/src/host/chat-index.js @@ -252,6 +252,34 @@ export function install(rt) { return { at: t === null ? undefined : rt.clock(t) } } + // The rows one pass over a part's events adds, as the writer indexes them: an answer is kept + // only while its turn keeps answers, and any other message with text is kept as said. It + // returns the mark values the pass ends with too. `start` is the mark it continues from. + function markedRows(sessionId, start, events) { + let keep = start?.keep === 1 + let turn = start?.turn ?? -1 + let turnNo = start?.turnNo ?? -1 + let last = start?.through ?? -1 + const rows = [] + for (const event of events) { + if (event.seq <= last) continue + last = event.seq + if (event.type === 'turn/start') { + turn = event.seq + if (Number.isFinite(event.data?.turn)) turnNo = event.data.turn + } + const entry = rt.historyEntry(event, false) + if (entry === undefined) continue + if (entry.answer) { + if (keep) rows.push({ text: entry.text, session: sessionId, seq: event.seq, time: event.time ?? 0, who: 'you', turn: turnNo }) + continue + } + keep = entry.keepAnswers + if (entry.text !== '') rows.push({ text: entry.text, session: sessionId, seq: event.seq, time: event.time ?? 0, who: entry.who, turn: turnNo }) + } + return { rows, last, keep, turn, turnNo } + } + // Adds what a part said since its mark. Reads of one part wait for each other within this // installation; two installations over one home meet again inside the transaction below. const catchingUp = new Map() @@ -286,27 +314,9 @@ export function install(rt) { index.db.exec('COMMIT') return } - let keep = mark ? mark.keep === 1 : false - let turn = mark?.turn ?? -1 - let turnNo = mark?.turnNo ?? -1 - let last = mark?.through ?? -1 - for (const event of events) { - if (event.seq <= last) continue - last = event.seq - if (event.type === 'turn/start') { - turn = event.seq - if (Number.isFinite(event.data?.turn)) turnNo = event.data.turn - } - const entry = rt.historyEntry(event, false) - if (entry === undefined) continue - if (entry.answer) { - if (keep) index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, 'you', turnNo) - continue - } - keep = entry.keepAnswers - if (entry.text !== '') index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, entry.who, turnNo) - } - index.setMark.run(part.sessionId, last, keep ? 1 : 0, seal ? 1 : 0, turn, turnNo) + const built = markedRows(part.sessionId, mark, events) + for (const row of built.rows) index.insert.run(row.text, row.session, row.seq, row.time, row.who, row.turn) + index.setMark.run(part.sessionId, built.last, built.keep ? 1 : 0, seal ? 1 : 0, built.turn, built.turnNo) index.db.exec('COMMIT') // The first commit is what makes `-wal`/`-shm`, so the modes are put right once it exists. if (!index.hardened) { @@ -390,6 +400,75 @@ export function install(rt) { return { rows, every, more: total === undefined ? 0 : Math.max(0, total - rows.length) } } + // The rows a read-only window's log holds past the mark of each session: this window adds + // nothing to the index, so the palette asks what the log holds rather than how far the + // writer's index got. Nothing is written, and the row shape is the writer's own (#3319). + async function unindexedRows(index, sessions, terms) { + const every = [] + const some = [] + for (const sessionId of sessions) { + const mark = index.mark.get(sessionId) + const through = mark?.through ?? -1 + const agent = await rt.resolveAgent(sessionId).catch(() => undefined) + if (agent === undefined || through >= Number(agent.session.seq) - 1) continue + const events = [] + try { + for await (const event of eventsBackwards(sessionId, AbortSignal.timeout(HISTORY_TIMEOUT_MS), Infinity)) { + if (event.seq <= through) break + events.push(event) + } + } catch (error) { + rt.warn('ds-bot: reading %s past its index mark failed: %s', sessionId, error) + continue + } + events.reverse() + for (const row of markedRows(sessionId, mark, events).rows) { + const lower = row.text.toLowerCase() + const hits = terms.filter(term => lower.includes(term)).length + if (hits === 0) continue + some.push({ row, hits }) + if (hits === terms.length) every.push(row) + } + } + const newest = (a, b) => (b.time - a.time) || (b.seq - a.seq) + return { + every: every.sort(newest), + some: some.sort((a, b) => (b.hits - a.hits) || (b.row.time - a.row.time)).map(hit => hit.row), + } + } + + // The index's answer and the rows read past its mark, as one answer: the rows holding every + // term when any exist, else the ones holding some, and `more` from the two totals. The two + // sources have no rank in common, so the answer is newest first, as the palette reads it; + // without a tail the index's own order is untouched (#3319). + function withLogRows(found, tail, limit) { + const every = found.every || tail.every.length > 0 + const rows = [] + const seen = new Set() + const add = list => { + for (const row of list) { + // A row both sources hold comes from one event, not two messages. + const key = `${row.session}#${row.seq}` + if (seen.has(key)) continue + seen.add(key) + rows.push(row) + } + } + const indexTotal = found.rows.length + found.more + let total + if (every) { + if (found.every) add(found.rows) + add(tail.every) + total = (found.every ? indexTotal : 0) + tail.every.length + } else { + add(found.rows) + add(tail.some) + total = indexTotal + tail.some.length + } + const shown = rows.sort((a, b) => (b.time - a.time) || (b.seq - a.seq)).slice(0, limit) + return { rows: shown, every, more: Math.max(0, total - shown.length) } + } + // The whole-text search behind the palette's "Messages (full text)" group (P-820): the // outline previews stop at 50/120 characters, so a hit past that budget is invisible to // the outline read. The sessions are the caller's to name — the channel lists the caller's @@ -403,7 +482,11 @@ export function install(rt) { if (index === null) return { rows: [], more: 0 } const sessions = [...new Set(sessionIds)] for (const sessionId of sessions) await catchUp(index, { sessionId }).catch(error => rt.warn('ds-bot: indexing %s failed: %s', sessionId, error)) - return matchedRows(index, sessions, terms, limit, undefined) + const found = matchedRows(index, sessions, terms, limit, undefined) + if (!readOnly()) return found + // A message newer than the mark was answered as missing, and `more: 0` called that whole. + const tail = await unindexedRows(index, sessions, terms) + return tail.some.length === 0 ? found : withLogRows(found, tail, limit) } // The latest messages across all parts, oldest first; `more` is how many the cap left out (#1906). diff --git a/core/test/chat-index-writers.test.js b/core/test/chat-index-writers.test.js index f6f2c2d14..11e788630 100644 --- a/core/test/chat-index-writers.test.js +++ b/core/test/chat-index-writers.test.js @@ -303,6 +303,53 @@ test('a read-only window reads the index and leaves it alone (#2711)', async (t) } }) +// #3319. The palette's whole-text search in a read-only window answered only what the writer +// had indexed, so a line in the log past the mark was not found, and `more: 0` read as "there +// is nothing else" while the line sat there. The window now matches its own log after the mark +// in memory, without writing the index. +test('a read-only window searches the log past its index mark (#3319)', async (t) => { + const home = scratch(t) + const writer = await start(t, home) + assert.deepEqual(anchorsOf(await writer.rt.latestOwnChat('a', {})), ['0', '1', '2']) + await writer.stop() + + // The writer stopped before indexing the fourth line: the index mark ends at seq 2. + HISTORY.solo.push(line(3, 'a fourth NEEDLE line')) + try { + const reader = await start(t, home, { heldBy: process.ppid }) + await reader.rt.load() + assert.notEqual(reader.rt.readOnly(), undefined, 'this window must be read-only for the case to mean anything') + + // The indexed part of the log is answered as before, from the index. + const older = await reader.rt.searchHomeChat(['solo'], 'third', {}) + assert.equal(older.rows.length, 1, JSON.stringify(older)) + + // The line past the mark is in this window's log, so the query finds it. + const found = await reader.rt.searchHomeChat(['solo'], 'NEEDLE', {}) + assert.equal(found.rows.length, 1, JSON.stringify(found)) + assert.equal(found.rows[0].session, 'solo') + assert.equal(found.rows[0].seq, 3) + assert.match(found.rows[0].text, /NEEDLE/) + assert.equal(found.more, 0) + + // The palette's own call: the channel action answers the same through /api/bot. + assert.notEqual(reader.route, undefined, 'the channel registered its /api/bot route') + const response = await reader.route.fetch(new Request('http://localhost/api/bot', { + method: 'POST', + body: JSON.stringify({ endpoint: 'chats.search', payload: { query: 'NEEDLE' } }), + })) + const result = await response.json() + assert.equal(result.ok, true, JSON.stringify(result)) + assert.equal(result.value.rows.length, 1, JSON.stringify(result.value)) + assert.match(result.value.rows[0].text, /NEEDLE/) + + // A read is a read: the index the writer left is still the writer's. + assert.equal(rowsOf(home), 3) + } finally { + HISTORY.solo.pop() + } +}) + test('a read-only window reads the index while the writer still holds the home (#2711)', async (t) => { const home = scratch(t) const writer = await start(t, home) diff --git a/docs/subsystems/core-host.md b/docs/subsystems/core-host.md index e5492e02e..a643d5ee5 100644 --- a/docs/subsystems/core-host.md +++ b/docs/subsystems/core-host.md @@ -474,7 +474,8 @@ has lines (#2790, `core/test/room-read-floor-clamp.test.js`). - The client's command palette searched each chat's `turnOutline` projection, whose prompt and response previews stop at 50/120 characters, so a hit past that budget — most of any long reply — found nothing. The index this file already keeps holds the whole text, so the search became a host read: `chat-index.js` lifts the term/any-term core of `searchOwnChat` into `matchedRows` (both reads share it; `searchOwnChat` answers byte for byte as before) and adds `rt.searchHomeChat(sessionIds, query, { limit })` — raw rows (`text, session, seq, time, who, turn`), best first, over the sessions the caller names. Each named session is caught up bare first, exactly as a Bot's parts are; no `endedAt` is known there, so nothing seals. Without an index there are no rows and the palette keeps its outline search. - Every indexed line carries the turn it belongs to by number: `lines.turn` (and `marks.turnNo`, what the next catch-up resumes from), tracked from the number a `turn/start` event carries, `INDEX_VERSION` 4 → 5 — a bump rides the same drop-and-rebuild path as #2711's. A line written before any turn, or after a `turn/start` that carried no number, keeps `-1`, which the client reads as no turn to jump to. - The channel endpoint is `chats.search` (`{ query }` → `{ rows: [{ chat, turn, text, part? }], more }`, `text` an `excerpt` around the first term the row holds, `part` the Session id of the part the hit was said in — set only for a Bot's own chat, and absent from a room's row, because a room's member part is not a chat the palette may open (#3114)): the sessions are taken from `view(actor)` — the same narrowing the `state` answer carries — but each Bot's parts map to their chat id only while `sessionOwnedBy` passes, and every member/owned room's parts map unconditionally. The view keeps an other-owner Bot so a shared room renders, and that Bot's own chat is not room content, so the ownership test is what stops a signed-in member from reading another member's Bot chats here, while a room's own parts stay searchable because the room is shared. `authorize` and this channel handler are installed as a pair (`authz.js` defines `rt.authorize`; `channel.js:326` calls it before its switch), and mounting the handler alone is fail-open — `channel.js:370` then skips every ownership test — so the two are never reused separately; a host whose `authzEnforced()` answers `false` is a supported single-user install, not a missing pair. `authz.js` allows the endpoint for any signed-in actor the way `schedules` is allowed (the handler scopes), and the channel's `READS` whitelist carries it, so a second read-only window answers it too. -- Covered by `core/test/chat-index-writers.test.js` (a line whose match sits 300 characters past the preview budget, with its turn; the endpoint scoped to the signed-in member through the real `/api/bot` route; and a shared room where the other member's Bot own chat stays unreadable while the room's own Session is found, with `earlier` and `sessionOwnedBy` still refusing it) and `core/test/authz.test.js` (allowed for a signed-in actor, refused without one). +- A read-only window answers the palette from its own log (#3319). `catchUp` adds nothing there, so a part whose log runs past `marks.through` answered nothing for a message the window held, and `more: 0` called that the whole chat. `searchHomeChat` now reads each such session's events after its mark (`eventsBackwards` from `through`, `HISTORY_TIMEOUT_MS`) and matches them in memory; the rows are built by `markedRows`, the same pass `catchUp` inserts, so the shape the palette reads cannot drift from the index's. Nothing is written, and `more` keeps its meaning — hits the cap left out — instead of naming the index. A window whose index is caught up, and a writer window (which catches up first), answer byte for byte as before. +- Covered by `core/test/chat-index-writers.test.js` (a line whose match sits 300 characters past the preview budget, with its turn; the endpoint scoped to the signed-in member through the real `/api/bot` route; a shared room where the other member's Bot own chat stays unreadable while the room's own Session is found, with `earlier` and `sessionOwnedBy` still refusing it; and a read-only window finding a line past its mark while the index it reads stays the writer's) and `core/test/authz.test.js` (allowed for a signed-in actor, refused without one). ### Out-of-app delivery, first half (#355) diff --git a/patches/ds-bot/0001-core-fixes.patch b/patches/ds-bot/0001-core-fixes.patch index de73f5362..c77be0e90 100644 --- a/patches/ds-bot/0001-core-fixes.patch +++ b/patches/ds-bot/0001-core-fixes.patch @@ -4943,7 +4943,7 @@ diff --git a/src/host/chat-index.js b/src/host/chat-index.js } } catch (error) { db.close() -@@ -130,16 +130,39 @@ export function install(rt) { +@@ -130,16 +130,67 @@ export function install(rt) { indexOpening = undefined }, 'ds-bot: chat index') @@ -4970,6 +4970,34 @@ diff --git a/src/host/chat-index.js b/src/host/chat-index.js + return { at: t === null ? undefined : rt.clock(t) } + } + ++ // The rows one pass over a part's events adds, as the writer indexes them: an answer is kept ++ // only while its turn keeps answers, and any other message with text is kept as said. It ++ // returns the mark values the pass ends with too. `start` is the mark it continues from. ++ function markedRows(sessionId, start, events) { ++ let keep = start?.keep === 1 ++ let turn = start?.turn ?? -1 ++ let turnNo = start?.turnNo ?? -1 ++ let last = start?.through ?? -1 ++ const rows = [] ++ for (const event of events) { ++ if (event.seq <= last) continue ++ last = event.seq ++ if (event.type === 'turn/start') { ++ turn = event.seq ++ if (Number.isFinite(event.data?.turn)) turnNo = event.data.turn ++ } ++ const entry = rt.historyEntry(event, false) ++ if (entry === undefined) continue ++ if (entry.answer) { ++ if (keep) rows.push({ text: entry.text, session: sessionId, seq: event.seq, time: event.time ?? 0, who: 'you', turn: turnNo }) ++ continue ++ } ++ keep = entry.keepAnswers ++ if (entry.text !== '') rows.push({ text: entry.text, session: sessionId, seq: event.seq, time: event.time ?? 0, who: entry.who, turn: turnNo }) ++ } ++ return { rows, last, keep, turn, turnNo } ++ } ++ + // Adds what a part said since its mark. Reads of one part wait for each other within this + // installation; two installations over one home meet again inside the transaction below. const catchingUp = new Map() @@ -4988,7 +5016,7 @@ diff --git a/src/host/chat-index.js b/src/host/chat-index.js const events = [] for await (const event of eventsBackwards(part.sessionId, AbortSignal.timeout(INDEX_TIMEOUT_MS), Infinity)) { if (event.seq <= through) break -@@ -148,26 +148,47 @@ export function install(rt) { +@@ -148,26 +148,29 @@ export function install(rt) { if (events.length > 0 && events.at(-1).seq !== through + 1) throw new Error(`the history of ${part.sessionId} came back with a gap`) events.reverse() if (index !== chatIndex) return @@ -5005,36 +5033,26 @@ diff --git a/src/host/chat-index.js b/src/host/chat-index.js + index.db.exec('BEGIN IMMEDIATE') try { - let last = through +- for (const event of events) { +- last = event.seq +- if (event.type === 'turn/start') turn = event.seq +- const entry = rt.historyEntry(event, false) +- if (entry === undefined) continue +- if (entry.answer) { +- if (keep) index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, 'you') +- continue +- } +- keep = entry.keepAnswers +- if (entry.text !== '') index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, entry.who) + const mark = index.mark.get(part.sessionId) + if (mark?.sealed) { + index.db.exec('COMMIT') + return -+ } -+ let keep = mark ? mark.keep === 1 : false -+ let turn = mark?.turn ?? -1 -+ let turnNo = mark?.turnNo ?? -1 -+ let last = mark?.through ?? -1 - for (const event of events) { -+ if (event.seq <= last) continue - last = event.seq -- if (event.type === 'turn/start') turn = event.seq -+ if (event.type === 'turn/start') { -+ turn = event.seq -+ if (Number.isFinite(event.data?.turn)) turnNo = event.data.turn -+ } - const entry = rt.historyEntry(event, false) - if (entry === undefined) continue - if (entry.answer) { -- if (keep) index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, 'you') -+ if (keep) index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, 'you', turnNo) - continue - } - keep = entry.keepAnswers -- if (entry.text !== '') index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, entry.who) -+ if (entry.text !== '') index.insert.run(entry.text, part.sessionId, event.seq, event.time ?? 0, entry.who, turnNo) } - index.setMark.run(part.sessionId, last, keep ? 1 : 0, seal ? 1 : 0, turn) -+ index.setMark.run(part.sessionId, last, keep ? 1 : 0, seal ? 1 : 0, turn, turnNo) ++ const built = markedRows(part.sessionId, mark, events) ++ for (const row of built.rows) index.insert.run(row.text, row.session, row.seq, row.time, row.who, row.turn) ++ index.setMark.run(part.sessionId, built.last, built.keep ? 1 : 0, seal ? 1 : 0, built.turn, built.turnNo) index.db.exec('COMMIT') + // The first commit is what makes `-wal`/`-shm`, so the modes are put right once it exists. + if (!index.hardened) { @@ -5097,19 +5115,88 @@ diff --git a/src/host/chat-index.js b/src/host/chat-index.js if (rows.length === 0 && terms.length > 1) { every = false const found = new Map() -@@ -227,13 +227,32 @@ export function install(rt) { +@@ -227,13 +227,105 @@ export function install(rt) { } } rows = [...found.values()].sort((a, b) => (b.count - a.count) || (b.row.time - a.row.time)).slice(0, limit).map(hit => hit.row) + if (rows.length === limit) total = count(anyTermCountQuery(terms, sessions, hidden)) + } else if (rows.length === limit) { + total = count(termCountQuery(terms, sessions, hidden)) ++ } ++ return { rows, every, more: total === undefined ? 0 : Math.max(0, total - rows.length) } ++ } ++ ++ // The rows a read-only window's log holds past the mark of each session: this window adds ++ // nothing to the index, so the palette asks what the log holds rather than how far the ++ // writer's index got. Nothing is written, and the row shape is the writer's own (#3319). ++ async function unindexedRows(index, sessions, terms) { ++ const every = [] ++ const some = [] ++ for (const sessionId of sessions) { ++ const mark = index.mark.get(sessionId) ++ const through = mark?.through ?? -1 ++ const agent = await rt.resolveAgent(sessionId).catch(() => undefined) ++ if (agent === undefined || through >= Number(agent.session.seq) - 1) continue ++ const events = [] ++ try { ++ for await (const event of eventsBackwards(sessionId, AbortSignal.timeout(HISTORY_TIMEOUT_MS), Infinity)) { ++ if (event.seq <= through) break ++ events.push(event) ++ } ++ } catch (error) { ++ rt.warn('ds-bot: reading %s past its index mark failed: %s', sessionId, error) ++ continue ++ } ++ events.reverse() ++ for (const row of markedRows(sessionId, mark, events).rows) { ++ const lower = row.text.toLowerCase() ++ const hits = terms.filter(term => lower.includes(term)).length ++ if (hits === 0) continue ++ some.push({ row, hits }) ++ if (hits === terms.length) every.push(row) ++ } ++ } ++ const newest = (a, b) => (b.time - a.time) || (b.seq - a.seq) ++ return { ++ every: every.sort(newest), ++ some: some.sort((a, b) => (b.hits - a.hits) || (b.row.time - a.row.time)).map(hit => hit.row), } - return { lines: rows.sort(byTime).map(row => rowLine(parts, row, terms)), every } -+ return { rows, every, more: total === undefined ? 0 : Math.max(0, total - rows.length) } } - // The latest messages across all parts, oldest first. ++ // The index's answer and the rows read past its mark, as one answer: the rows holding every ++ // term when any exist, else the ones holding some, and `more` from the two totals. The two ++ // sources have no rank in common, so the answer is newest first, as the palette reads it; ++ // without a tail the index's own order is untouched (#3319). ++ function withLogRows(found, tail, limit) { ++ const every = found.every || tail.every.length > 0 ++ const rows = [] ++ const seen = new Set() ++ const add = list => { ++ for (const row of list) { ++ // A row both sources hold comes from one event, not two messages. ++ const key = `${row.session}#${row.seq}` ++ if (seen.has(key)) continue ++ seen.add(key) ++ rows.push(row) ++ } ++ } ++ const indexTotal = found.rows.length + found.more ++ let total ++ if (every) { ++ if (found.every) add(found.rows) ++ add(tail.every) ++ total = (found.every ? indexTotal : 0) + tail.every.length ++ } else { ++ add(found.rows) ++ add(tail.some) ++ total = indexTotal + tail.some.length ++ } ++ const shown = rows.sort((a, b) => (b.time - a.time) || (b.seq - a.seq)).slice(0, limit) ++ return { rows: shown, every, more: Math.max(0, total - shown.length) } ++ } ++ + // The whole-text search behind the palette's "Messages (full text)" group (P-820): the + // outline previews stop at 50/120 characters, so a hit past that budget is invisible to + // the outline read. The sessions are the caller's to name — the channel lists the caller's @@ -5123,7 +5210,11 @@ diff --git a/src/host/chat-index.js b/src/host/chat-index.js + if (index === null) return { rows: [], more: 0 } + const sessions = [...new Set(sessionIds)] + for (const sessionId of sessions) await catchUp(index, { sessionId }).catch(error => rt.warn('ds-bot: indexing %s failed: %s', sessionId, error)) -+ return matchedRows(index, sessions, terms, limit, undefined) ++ const found = matchedRows(index, sessions, terms, limit, undefined) ++ if (!readOnly()) return found ++ // A message newer than the mark was answered as missing, and `more: 0` called that whole. ++ const tail = await unindexedRows(index, sessions, terms) ++ return tail.some.length === 0 ? found : withLogRows(found, tail, limit) + } + + // The latest messages across all parts, oldest first; `more` is how many the cap left out (#1906). diff --git a/patches/ds-bot/files/test/chat-index-writers.test.js b/patches/ds-bot/files/test/chat-index-writers.test.js index f6f2c2d14..11e788630 100644 --- a/patches/ds-bot/files/test/chat-index-writers.test.js +++ b/patches/ds-bot/files/test/chat-index-writers.test.js @@ -303,6 +303,53 @@ test('a read-only window reads the index and leaves it alone (#2711)', async (t) } }) +// #3319. The palette's whole-text search in a read-only window answered only what the writer +// had indexed, so a line in the log past the mark was not found, and `more: 0` read as "there +// is nothing else" while the line sat there. The window now matches its own log after the mark +// in memory, without writing the index. +test('a read-only window searches the log past its index mark (#3319)', async (t) => { + const home = scratch(t) + const writer = await start(t, home) + assert.deepEqual(anchorsOf(await writer.rt.latestOwnChat('a', {})), ['0', '1', '2']) + await writer.stop() + + // The writer stopped before indexing the fourth line: the index mark ends at seq 2. + HISTORY.solo.push(line(3, 'a fourth NEEDLE line')) + try { + const reader = await start(t, home, { heldBy: process.ppid }) + await reader.rt.load() + assert.notEqual(reader.rt.readOnly(), undefined, 'this window must be read-only for the case to mean anything') + + // The indexed part of the log is answered as before, from the index. + const older = await reader.rt.searchHomeChat(['solo'], 'third', {}) + assert.equal(older.rows.length, 1, JSON.stringify(older)) + + // The line past the mark is in this window's log, so the query finds it. + const found = await reader.rt.searchHomeChat(['solo'], 'NEEDLE', {}) + assert.equal(found.rows.length, 1, JSON.stringify(found)) + assert.equal(found.rows[0].session, 'solo') + assert.equal(found.rows[0].seq, 3) + assert.match(found.rows[0].text, /NEEDLE/) + assert.equal(found.more, 0) + + // The palette's own call: the channel action answers the same through /api/bot. + assert.notEqual(reader.route, undefined, 'the channel registered its /api/bot route') + const response = await reader.route.fetch(new Request('http://localhost/api/bot', { + method: 'POST', + body: JSON.stringify({ endpoint: 'chats.search', payload: { query: 'NEEDLE' } }), + })) + const result = await response.json() + assert.equal(result.ok, true, JSON.stringify(result)) + assert.equal(result.value.rows.length, 1, JSON.stringify(result.value)) + assert.match(result.value.rows[0].text, /NEEDLE/) + + // A read is a read: the index the writer left is still the writer's. + assert.equal(rowsOf(home), 3) + } finally { + HISTORY.solo.pop() + } +}) + test('a read-only window reads the index while the writer still holds the home (#2711)', async (t) => { const home = scratch(t) const writer = await start(t, home)