diff --git a/changelog.d/3139-palette-fulltext-search.fixed.md b/changelog.d/3139-palette-fulltext-search.fixed.md new file mode 100644 index 000000000..dfc5f7fd9 --- /dev/null +++ b/changelog.d/3139-palette-fulltext-search.fixed.md @@ -0,0 +1 @@ +The command palette now searches the whole text of a chat's messages, not only the opening the outline preview keeps: a word that appears past the first characters of a long reply lists a **Messages (full text)** group, and choosing a row opens that chat at the turn the line came from ([#3139](https://github.com/ThinkFlowLab/FlyBot/issues/3139)). diff --git a/core/client.js b/core/client.js index 7631654c3..e93e55348 100644 --- a/core/client.js +++ b/core/client.js @@ -873,6 +873,7 @@ var ZH = { "Rejected for {name}": "已拒绝 {name} 的请求", // Command palette Messages: "消息", + "Messages (full text)": "消息(全文)", Commands: "命令", "Search Bots, chats and messages": "搜索 Bot、对话和消息", "Clear search": "清除搜索", @@ -15879,10 +15880,12 @@ var import_react33 = require("react"); var import_dsh_client_ui_primitives3 = require("@deepseek-ai/dsh-client-ui-primitives"); var paletteText = (raw, names) => readableLine(String(raw ?? ""), names); var PALETTE_EXIT_MS = 160; +var SEARCH_DEBOUNCE_MS = 150; function CommandPalette({ start, token, roster, actions, useSessions }) { const [query, setQuery] = (0, import_react33.useState)(start ?? ""); const [cursor, setCursor] = (0, import_react33.useState)(0); const [leaving, setLeaving] = (0, import_react33.useState)(false); + const [wholeText, setWholeText] = (0, import_react33.useState)(null); const wanted = query.trim().toLowerCase(); const close = () => { if (leaving) return; @@ -15914,6 +15917,29 @@ function CommandPalette({ start, token, roster, actions, useSessions }) { } return JSON.stringify(hits); }); + const searchChats = actions.searchChats; + (0, import_react33.useEffect)(() => { + if (wanted.length < 2 || !searchChats) { + setWholeText(null); + return void 0; + } + let current = true; + const controller = new AbortController(); + const timer = setTimeout(() => { + void searchChats(wanted, controller.signal).then( + (rows2) => { + if (current) setWholeText(Array.isArray(rows2) ? rows2 : []); + }, + () => { + } + ); + }, SEARCH_DEBOUNCE_MS); + return () => { + current = false; + clearTimeout(timer); + controller.abort(); + }; + }, [wanted, searchChats]); const matchesName = (...fields) => wanted === "" || fields.some((field) => (field ?? "").toLowerCase().includes(wanted)); const sections = []; const people2 = [ @@ -15961,6 +15987,19 @@ function CommandPalette({ start, token, roster, actions, useSessions }) { }; }); if (messages.length) sections.push([t("Messages"), messages]); + const outlined = new Set(messages.map((row) => row.key)); + const fullText = (wholeText ?? []).filter((row) => row?.chat !== void 0 && (roster.byId[row.chat] || roster.roomsById[row.chat]) && !outlined.has(`${row.chat}:${row.turn}`)).map((row) => { + const bot = roster.byId[row.chat]; + const room2 = roster.roomsById[row.chat]; + return { + key: `${row.chat}:${row.turn}`, + title: bot?.name ?? room2?.name ?? t("Bot"), + subtitle: row.text, + avatar: bot ? (0, import_react33.createElement)(BotAvatar, { bot, size: 24, badge: false, live: false }) : (0, import_react33.createElement)(RoomAvatar, { room: room2, roster, size: 24 }), + run: () => actions.openSession(row.chat, { turn: row.turn }) + }; + }); + if (fullText.length) sections.push([t("Messages (full text)"), fullText]); const commands = [ { key: "cmd:new", icon: (0, import_react33.createElement)(PlusIcon), title: t("New chat"), run: () => actions.openNewChat("new") }, { key: "cmd:bot", icon: (0, import_react33.createElement)(PlusIcon), title: t("Create Bot"), run: () => actions.openNewChat("create") }, @@ -23229,11 +23268,25 @@ function apply(ctx) { for (const [id, owner] of pendingAdopt) if (owner === agentId && !ids.includes(id)) ids.push(id); return ids; }; + const seekTurn = (turn) => { + if (!Number.isInteger(turn) || turn < 0 || typeof document === "undefined") return; + let tries = 0; + const seek = () => { + const node = document.querySelector(`[data-chat-turn="${turn}"]`); + if (node !== null) { + node.scrollIntoView({ block: "center" }); + return; + } + if ((tries += 1) > 20) return; + setTimeout(seek, 100); + }; + seek(); + }; const actions = { // The full-pane overlays sit above the conversation, so opening one must dismiss them // or the click appears to do nothing. isAgentSession: (id) => isAgentSessionId(id), - openSession: (id) => { + openSession: (id, { turn } = {}) => { noteNavigation("openSession", id); if (isAgentSessionId(id)) return actions.openAgentSession(id); ui2.set((state) => state.newChat || state.exchange || state.menu || state.palette ? { ...state, newChat: null, exchange: null, menu: null, palette: null } : state); @@ -23241,7 +23294,11 @@ function apply(ctx) { const target = partFor(id); expected = target; ctx.uiWorkspace.openSession(target); + seekTurn(turn); }, + // The palette's whole-text message search (P-820): the host answers with the + // caller's own chats, whole lines with the turn they belong to. + searchChats: (query, signal) => call("chats.search", { query }, signal).then((value) => value?.rows ?? []), // The conversation replacements withdraw for a plain Session; set the surface // before the navigation so the Bot skin never flashes. openAgentSession: (sessionId) => { diff --git a/core/src/client/i18n.js b/core/src/client/i18n.js index be91f2dbc..6bb4bb758 100644 --- a/core/src/client/i18n.js +++ b/core/src/client/i18n.js @@ -817,6 +817,7 @@ const ZH = { 'Rejected for {name}': '已拒绝 {name} 的请求', // Command palette Messages: '消息', + 'Messages (full text)': '消息(全文)', Commands: '命令', 'Search Bots, chats and messages': '搜索 Bot、对话和消息', 'Clear search': '清除搜索', diff --git a/core/src/client/index.js b/core/src/client/index.js index 87ddd71a4..f691ef539 100644 --- a/core/src/client/index.js +++ b/core/src/client/index.js @@ -414,11 +414,26 @@ export function apply(ctx) { for (const [id, owner] of pendingAdopt) if (owner === agentId && !ids.includes(id)) ids.push(id) return ids } + // The palette's whole-text rows name the turn the hit belongs to (P-820). DSH opens a + // session by id only, so the turn is found where the message pills already look: the + // flow's own DOM. A turn the host has not loaded lands on the nearest view — the row's + // snippet still shows the words — and the seek gives up rather than spin. + const seekTurn = (turn) => { + if (!Number.isInteger(turn) || turn < 0 || typeof document === 'undefined') return + let tries = 0 + const seek = () => { + const node = document.querySelector(`[data-chat-turn="${turn}"]`) + if (node !== null) { node.scrollIntoView({ block: 'center' }); return } + if ((tries += 1) > 20) return + setTimeout(seek, 100) + } + seek() + } const actions = { // The full-pane overlays sit above the conversation, so opening one must dismiss them // or the click appears to do nothing. isAgentSession: id => isAgentSessionId(id), - openSession: (id) => { + openSession: (id, { turn } = {}) => { noteNavigation('openSession', id) // A DSH Agent Session id lands on the plain surface, not the Bot skin. if (isAgentSessionId(id)) return actions.openAgentSession(id) @@ -427,7 +442,11 @@ export function apply(ctx) { const target = partFor(id) expected = target ctx.uiWorkspace.openSession(target) + seekTurn(turn) }, + // The palette's whole-text message search (P-820): the host answers with the + // caller's own chats, whole lines with the turn they belong to. + searchChats: (query, signal) => call('chats.search', { query }, signal).then(value => value?.rows ?? []), // The conversation replacements withdraw for a plain Session; set the surface // before the navigation so the Bot skin never flashes. openAgentSession: (sessionId) => { diff --git a/core/src/client/palette.js b/core/src/client/palette.js index a8d042a57..fafea8393 100644 --- a/core/src/client/palette.js +++ b/core/src/client/palette.js @@ -16,10 +16,14 @@ import { seatedCount } from './group-settings.js' // a delegation trace and a fence are routing or data, never the row (#1347). const paletteText = (raw, names) => readableLine(String(raw ?? ''), names) const PALETTE_EXIT_MS = 160 +// The whole-text search is a round-trip the host answers: a pause, so typing a query does +// not ask once per character. +const SEARCH_DEBOUNCE_MS = 150 export function CommandPalette({ start, token, roster, actions, useSessions }) { const [query, setQuery] = useState(start ?? '') const [cursor, setCursor] = useState(0) const [leaving, setLeaving] = useState(false) + const [wholeText, setWholeText] = useState(null) const wanted = query.trim().toLowerCase() const close = () => { if (leaving) return @@ -51,6 +55,26 @@ export function CommandPalette({ start, token, roster, actions, useSessions }) { } return JSON.stringify(hits) }) + // The outline previews stop at 50/120 characters, so any hit past that budget is + // invisible to the read above (P-820). The host searches the whole text over the + // caller's own chats; an answer that lands for a query the user already changed is + // dropped with the effect that asked for it. + const searchChats = actions.searchChats + useEffect(() => { + if (wanted.length < 2 || !searchChats) { + setWholeText(null) + return undefined + } + let current = true + const controller = new AbortController() + const timer = setTimeout(() => { + void searchChats(wanted, controller.signal).then( + rows => { if (current) setWholeText(Array.isArray(rows) ? rows : []) }, + () => {}, + ) + }, SEARCH_DEBOUNCE_MS) + return () => { current = false; clearTimeout(timer); controller.abort() } + }, [wanted, searchChats]) const matchesName = (...fields) => wanted === '' || fields.some(field => (field ?? '').toLowerCase().includes(wanted)) const sections = [] const people = [ @@ -82,6 +106,20 @@ export function CommandPalette({ start, token, roster, actions, useSessions }) { } }) if (messages.length) sections.push([t('Messages'), messages]) + // The host's whole-text rows, newest first (P-820). A row the outline already found for + // the same chat and turn is the same message, so it is not shown twice; a row whose chat + // the roster does not hold is not the caller's to open. + const outlined = new Set(messages.map(row => row.key)) + const fullText = (wholeText ?? []).filter(row => row?.chat !== undefined && (roster.byId[row.chat] || roster.roomsById[row.chat]) && !outlined.has(`${row.chat}:${row.turn}`)).map(row => { + const bot = roster.byId[row.chat] + const room = roster.roomsById[row.chat] + return { + key: `${row.chat}:${row.turn}`, title: bot?.name ?? room?.name ?? t('Bot'), subtitle: row.text, + avatar: bot ? h(BotAvatar, { bot, size: 24, badge: false, live: false }) : h(RoomAvatar, { room, roster, size: 24 }), + run: () => actions.openSession(row.chat, { turn: row.turn }), + } + }) + if (fullText.length) sections.push([t('Messages (full text)'), fullText]) const commands = [ { key: 'cmd:new', icon: h(PlusIcon), title: t('New chat'), run: () => actions.openNewChat('new') }, { key: 'cmd:bot', icon: h(PlusIcon), title: t('Create Bot'), run: () => actions.openNewChat('create') }, diff --git a/core/src/host/authz.js b/core/src/host/authz.js index b62eba237..9f81fea37 100644 --- a/core/src/host/authz.js +++ b/core/src/host/authz.js @@ -294,6 +294,11 @@ export function install(rt) { // writes keep their Session and target gates below, unchanged. if (ep === 'schedules') return ALLOW + // The palette's whole-text search is the same shape (P-820): the ask names no Bot or + // room, and the answer is scoped per actor in the handler (`chats.search` in + // channel.js searches only the sessions the actor's own view holds). + if (ep === 'chats.search') return ALLOW + if (BOT_TARGETED.has(ep)) { if (ep.startsWith('schedule-')) { // The create endpoint keys off `body.target` (Bot or room id); `sessionId` is diff --git a/core/src/host/channel.js b/core/src/host/channel.js index 07326e8aa..6c34b913f 100644 --- a/core/src/host/channel.js +++ b/core/src/host/channel.js @@ -9,6 +9,7 @@ import { readQuietHours } from '../shared/quiet-hours.js' import { readTurnBudget } from './turn-budget.js' import { faceMentions } from './face-mentions.js' import { visibleText } from '../shared/preview.js' +import { excerpt } from './text.js' const NOTIFY_KINDS = ['approval', 'finished', 'failed'] const LANGUAGES = { zh: 'Chinese (简体中文)', en: 'English' } @@ -239,7 +240,7 @@ export function install(rt) { // `diagnostics-clear` is not a read: it truncates `/logs/diagnostics.jsonl`, the file a bug // report is meant to carry, so a host that only reads refuses it instead of erasing the evidence // (#2302). `diagnostics` and `diagnostics-log` stay, because they only read that file. - const READS = new Set(['state', 'models', 'efforts', 'earlier', 'room-progress', 'room-history', 'exchange', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer', 'auth-whoami', 'workflows', + const READS = new Set(['state', 'models', 'efforts', 'earlier', 'chats.search', 'room-progress', 'room-history', 'exchange', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer', 'auth-whoami', 'workflows', 'tasks', 'projects', 'project-read', 'project-board', 'project-team', 'project-of', 'project-decision', 'project-memory', 'folders', 'folder-list', 'connector-app-status']) @@ -356,6 +357,29 @@ export function install(rt) { case 'earlier': await rt.load() return ok(await rt.earlierPage(String(body.sessionId ?? ''), body.cursor ?? undefined)) + case 'chats.search': { + // The palette's whole-text rows (P-820), scoped the way `state` is: the view + // narrows to the actor's own Bots and rooms, and only the sessions behind those + // rows are searched — a member never reads another member's chats through it. + await rt.load() + const query = String(body.query ?? '') + const scoped = view(actor) + const sessionToChat = new Map() + for (const bot of scoped.bots) for (const part of [bot.sessionId, ...(bot.parts ?? [])]) sessionToChat.set(part, bot.id) + for (const room of scoped.rooms) for (const part of room.parts ?? []) if (!sessionToChat.has(part)) sessionToChat.set(part, room.id) + const { rows, more } = await rt.searchHomeChat([...sessionToChat.keys()], query) + const terms = [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))] + return ok({ + rows: rows + .map(row => { + const lower = row.text.toLowerCase() + const wanted = terms.find(term => lower.includes(term)) ?? '' + return { chat: sessionToChat.get(row.session) ?? null, turn: row.turn, text: excerpt(row.text, wanted) } + }) + .filter(row => row.chat !== null), + more, + }) + } case 'usage': await rt.load() return ok(await rt.usageView({ timeZone: typeof body.timeZone === 'string' ? body.timeZone.slice(0, 64) : undefined })) diff --git a/core/src/host/chat-index.js b/core/src/host/chat-index.js index 3ae107700..d852f6c27 100644 --- a/core/src/host/chat-index.js +++ b/core/src/host/chat-index.js @@ -13,7 +13,9 @@ import { ANCHOR_LINE_MAX, HISTORY_PAGE, HISTORY_PAGES_MAX, HISTORY_TIMEOUT_MS, O import { ensurePrivateDir, tightenFile } from './private-files.js' import { excerpt } from './text.js' -const INDEX_VERSION = 4 +// Version 5 added `lines.turn` and `marks.turnNo`: the turn a line belongs to by number, +// which the palette's whole-text rows jump to (the outline only keeps the preview text). +const INDEX_VERSION = 5 const AROUND_LINES = 6 const SEARCH_TERMS_MAX = 6 const ANY_TERM_HITS = 50 @@ -45,8 +47,9 @@ function termWhere(terms, sessions, hidden) { } // The rows carry their own text and columns; a query with a long term is driven by the -// FTS index over them, and one without it scans the rows alone. -const LINE_COLUMNS = 'lines.text, lines.session, lines.seq, lines.time, lines.who' +// FTS index over them, and one without it scans the rows alone. `turn` is the turn +// number the line belongs to, carried since index version 5. +const LINE_COLUMNS = 'lines.text, lines.session, lines.seq, lines.time, lines.who, lines.turn' const linesFrom = ranked => (ranked ? 'lines_fts JOIN lines ON lines.id = lines_fts.rowid' : 'lines') export function termQuery(terms, sessions, limit, hidden) { @@ -167,6 +170,7 @@ export function install(rt) { seq INTEGER NOT NULL, time INTEGER NOT NULL, who TEXT NOT NULL, + turn INTEGER NOT NULL DEFAULT -1, UNIQUE(session, seq) ); CREATE VIRTUAL TABLE lines_fts USING fts5(text, content='lines', content_rowid='id', tokenize = 'trigram'); @@ -176,7 +180,7 @@ export function install(rt) { CREATE TRIGGER lines_delete AFTER DELETE ON lines BEGIN INSERT INTO lines_fts (lines_fts, rowid, text) VALUES ('delete', old.id, old.text); END; - CREATE TABLE marks (session TEXT PRIMARY KEY, through INTEGER NOT NULL, keep INTEGER NOT NULL, sealed INTEGER NOT NULL, turn INTEGER NOT NULL); + CREATE TABLE marks (session TEXT PRIMARY KEY, through INTEGER NOT NULL, keep INTEGER NOT NULL, sealed INTEGER NOT NULL, turn INTEGER NOT NULL, turnNo INTEGER NOT NULL DEFAULT -1); PRAGMA user_version = ${INDEX_VERSION};` // How many rows an older index stored more than once. A build before #2711 could hold one @@ -207,9 +211,9 @@ export function install(rt) { db, writable, hardened: false, - mark: db.prepare('SELECT through, keep, sealed, turn FROM marks WHERE session = ?'), - setMark: db.prepare('INSERT OR REPLACE INTO marks (session, through, keep, sealed, turn) VALUES (?, ?, ?, ?, ?)'), - insert: db.prepare('INSERT OR IGNORE INTO lines (text, session, seq, time, who) VALUES (?, ?, ?, ?, ?)'), + mark: db.prepare('SELECT through, keep, sealed, turn, turnNo FROM marks WHERE session = ?'), + setMark: db.prepare('INSERT OR REPLACE INTO marks (session, through, keep, sealed, turn, turnNo) VALUES (?, ?, ?, ?, ?, ?)'), + insert: db.prepare('INSERT OR IGNORE INTO lines (text, session, seq, time, who, turn) VALUES (?, ?, ?, ?, ?, ?)'), before: db.prepare('SELECT text, session, seq, time, who FROM lines WHERE session = ? AND seq < ? ORDER BY seq DESC LIMIT ?'), from: db.prepare('SELECT text, session, seq, time, who FROM lines WHERE session = ? AND seq >= ? ORDER BY seq LIMIT ?'), countAll: db.prepare('SELECT COUNT(*) AS n FROM lines WHERE session IN (SELECT value FROM json_each(?))'), @@ -262,21 +266,25 @@ export function install(rt) { } 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) index.db.exec('COMMIT') // The first commit is what makes `-wal`/`-shm`, so the modes are put right once it exists. if (!index.hardened) { @@ -324,12 +332,20 @@ export function install(rt) { const { index, parts, hidden } = await indexedParts(botId, askingId) if (index === null) return { ...(await scanOwnChat(botId, query, limit)), every: true } const sessions = parts.map(part => part.sessionId) + const byTime = (a, b) => (a.time - b.time) || (sessions.indexOf(a.session) - sessions.indexOf(b.session)) || (a.seq - b.seq) + const found = matchedRows(index, sessions, terms, limit, hidden) + return { lines: found.rows.sort(byTime).map(row => rowLine(parts, row, terms)), every: found.every, more: found.more } + } + + // The rows a query matches, best first, and how many the cap left out (#1906): the core + // both searches share. `searchOwnChat` formats these rows for the tool; `searchHomeChat` + // hands them to the channel raw. `every` says every term matched somewhere, not only some. + function matchedRows(index, sessions, terms, limit, hidden) { const run = (wanted, max) => { const { sql, args } = termQuery(wanted, sessions, max, hidden) return index.db.prepare(sql).all(...args) } const count = query => index.db.prepare(query.sql).get(...query.args).n - const byTime = (a, b) => (a.time - b.time) || (sessions.indexOf(a.session) - sessions.indexOf(b.session)) || (a.seq - b.seq) let rows = run(terms, limit) let every = true let total @@ -349,8 +365,23 @@ export function install(rt) { } else if (rows.length === limit) { total = count(termCountQuery(terms, sessions, hidden)) } - const more = total === undefined ? 0 : Math.max(0, total - rows.length) - return { lines: rows.sort(byTime).map(row => rowLine(parts, row, terms)), every, more } + return { rows, every, more: total === undefined ? 0 : Math.max(0, total - rows.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 + // own chats — and each is caught up bare, exactly as `indexedParts` catches up a Bot's + // parts; no `endedAt` is known here, so nothing seals. Without an index there are no rows, + // and the palette keeps its outline search. + async function searchHomeChat(sessionIds, query, { limit = OWN_CHAT_SEARCH } = {}) { + const terms = [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))].slice(0, SEARCH_TERMS_MAX) + if (terms.length === 0) return { rows: [], more: 0 } + const index = await openIndex() + 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) } // The latest messages across all parts, oldest first; `more` is how many the cap left out (#1906). @@ -428,5 +459,5 @@ export function install(rt) { ].join('\n') } - Object.assign(rt, { eventsBackwards, searchOwnChat, latestOwnChat, aroundOwnChat, partIndex, ownChatBrief }) + Object.assign(rt, { eventsBackwards, searchOwnChat, searchHomeChat, latestOwnChat, aroundOwnChat, partIndex, ownChatBrief }) } diff --git a/core/test/authz.test.js b/core/test/authz.test.js index 007545c2f..912eb1e2e 100644 --- a/core/test/authz.test.js +++ b/core/test/authz.test.js @@ -513,3 +513,29 @@ test('a Bot Social read answers only for the actor\'s own Bot (#1788)', async (t // And once authz applies, a missing actor is still refused before any Bot is read. assert.equal(rt.authorize(null, 'flybot/social/share-friends', { botId: 'a1' }).code, 'bot/unauthorized') }) + +// P-820: the palette's whole-text search is a member read the data layer scopes, the shape +// `schedules` set — the channel answers with only the actor's own chats, so any signed-in +// actor may ask and a missing actor is refused like every other member read. +test('chats.search is a signed-in member read, refused without an actor (P-820)', async (t) => { + const saved = { + ...TEAM, + bots: { + ...TEAM.bots, + b1: { id: 'b1', sessionId: 'b1', name: 'B', instructions: '', createdAt: 3, ownerId: 'owner-2' }, + }, + } + const { rt } = runtime(t, saved) + await rt.load() + const actorA = await rt.authenticate((await redeemOwner(rt)).sessionToken) + const actorB = await rt.authenticate((await redeemOwner(rt)).sessionToken) + assert.equal(actorB.memberId, 'owner-2') + + for (const actor of [actorA, actorB]) { + const gate = rt.authorize(actor, 'chats.search', { query: 'needle' }) + assert.equal(gate.ok, true, `any signed-in member may search their own chats: ${JSON.stringify(gate)}`) + } + const none = rt.authorize(null, 'chats.search', { query: 'needle' }) + assert.equal(none.ok, false) + assert.equal(none.code, 'bot/unauthorized') +}) diff --git a/core/test/chat-index-writers.test.js b/core/test/chat-index-writers.test.js index f7a0ca2b6..b72fe4b28 100644 --- a/core/test/chat-index-writers.test.js +++ b/core/test/chat-index-writers.test.js @@ -3,6 +3,11 @@ // dsh keeps read-only still opened the index for writing (#2711). // // Real module, a temporary home, real SQLite, and a fake Session log as the source. +// +// P-820: the palette's message search reads the turnOutline projection, whose prompt and +// response previews stop at 50/120 characters, so a hit past that budget found nothing. The +// index this file already exercises holds the whole text, and the palette reaches it through +// `searchHomeChat` and the channel's `chats.search` — answered with only the caller's chats. import assert from 'node:assert/strict' import { existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs' import { hostname, tmpdir } from 'node:os' @@ -11,6 +16,7 @@ import { DatabaseSync } from 'node:sqlite' import { test } from 'node:test' import { createRuntime, provideFlyBot } from '../index.js' import { fakeContext } from './fake-ctx.js' +import { hashToken } from '../src/host/member-auth.js' const T0 = 1_760_000_000_000 const INDEX = 'chat-index.sqlite' @@ -38,6 +44,36 @@ const seed = () => ({ revision: 1, }) +// Two members, each with their own Bot, and owner-2 signed in: the shape `chats.search` +// has to scope. The seed stays otherwise identical to the single-Bot one. +const TOKEN_OWNER_2 = 'tok-owner-2' +const sharedSeed = () => ({ + version: 2, + mainBotIds: ['a'], + members: { + 'owner-1': { id: 'owner-1', createdAt: 1, designatedChiefId: 'a' }, + 'owner-2': { id: 'owner-2', createdAt: 2 }, + }, + bots: { + a: { id: 'a', sessionId: 'solo', name: 'Solo', role: 'Dev', model: 'deepseek/v4', instructions: '', createdAt: 1, ownerId: 'owner-1' }, + b: { id: 'b', sessionId: 'duo', name: 'Duo', role: 'Dev', model: 'deepseek/v4', instructions: '', createdAt: 2, ownerId: 'owner-2' }, + }, + authSessions: { + s2: { id: 's2', tokenHash: hashToken(TOKEN_OWNER_2), memberId: 'owner-2', createdAt: 3, revokedAt: null }, + }, + rooms: {}, + exchanges: [], + exchangeTurns: {}, + questions: {}, + prefs: {}, + revision: 1, +}) + +// The history the palette's defect lives in: a line whose match sits far past the 120 +// characters the outline projection keeps, inside a turn that is not the first one. +const NEEDLE_LINE = 'x'.repeat(300) + ' NEEDLE' +const turnStart = (seq, turn) => ({ type: 'turn/start', seq, time: T0 + seq * 1000, data: { turn } }) + const scratch = (t, name = 'ds-bot-index-') => { const home = mkdtempSync(join(tmpdir(), name)) t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 })) @@ -81,15 +117,19 @@ function barrier(parties) { // One host over `home`: the whole core with a fake Session controller, as a second dsh // would open the same home. `heldBy` puts a live other pid in `state.lock` first, so this -// host is the read-only window `home-lock` describes. -async function start(t, home, { heldBy, gate } = {}) { +// host is the read-only window `home-lock` describes. `saved` swaps the state seed in, and +// the host's `route` is the /api/bot Fetch route the channel registered. +async function start(t, home, { heldBy, gate, saved } = {}) { if (heldBy !== undefined) { writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: heldBy, host: hostname(), token: 'other', since: Date.now() })) } - writeFileSync(join(home, 'state.json'), JSON.stringify(seed()), { mode: 0o600 }) + writeFileSync(join(home, 'state.json'), JSON.stringify(saved ?? seed()), { mode: 0o600 }) const ctx = fakeContext() const warnings = [] ctx.logger.warn = (...args) => warnings.push(args.map(part => String(part)).join(' ')) + let route + const registered = ctx.connection.fetch.register + ctx.connection.fetch.register = (options) => { route = options; return registered(options) } Object.assign(ctx, { llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) }, sessionController: { @@ -114,7 +154,7 @@ async function start(t, home, { heldBy, gate } = {}) { // parts, returns `{lines: []}` and never calls `sessionController.page()`, so the party // this test's gate waits for never arrives. Wait for the load the host relies on. await rt.load() - const host = { home, ctx, rt, warnings, stop: () => ctx.dispose() } + const host = { home, ctx, rt, route, warnings, stop: () => ctx.dispose() } t.after(() => host.stop()) return host } @@ -261,3 +301,53 @@ test('an index two writers already duplicated is rebuilt once at startup (#2711) assert.equal(rowsOf(home), 3, 'the rebuilt index holds one row per source line') assert.match(host.warnings.join('\n'), /rebuilt the chat index/) }) + +// P-820. The host's whole-text search across given sessions: the palette's outline search +// misses any hit past the 50/120-character preview, and this read is what fills that gap. +// The row names the turn the hit belongs to, which is how the palette jumps to it. +test('the whole-text search finds a line past the outline budget, with its turn (P-820)', async (t) => { + const home = scratch(t) + HISTORY.duo = [turnStart(0, 4), line(1, 'a plain line'), turnStart(2, 5), line(3, NEEDLE_LINE)] + try { + const host = await start(t, home) + const read = await host.rt.searchHomeChat(['solo', 'duo'], 'needle', {}) + assert.equal(read.rows.length, 1, JSON.stringify(read)) + const [row] = read.rows + assert.equal(row.session, 'duo') + assert.equal(row.seq, 3) + assert.equal(row.turn, 5, 'the row names the turn the hit belongs to') + assert.match(row.text, /NEEDLE/) + assert.equal(read.more, 0) + + // A chat the caller did not list is not searched, and the caller's list is what the + // channel scopes per member: this read never widens it on its own. + const own = await host.rt.searchHomeChat(['solo'], 'needle', {}) + assert.equal(own.rows.length, 0) + } finally { + delete HISTORY.duo + } +}) + +// P-820. The channel action the palette calls: `chats.search` answers through /api/bot with +// the signed-in member's chats only — owner-2 signs in, and owner-1's Bot never shows up even +// though its own history is served by the same host. +test('chats.search answers through the channel with only the caller\'s chats (P-820)', async (t) => { + const home = scratch(t, 'ds-bot-index-search-') + HISTORY.duo = [turnStart(0, 4), line(1, 'a plain line'), turnStart(2, 5), line(3, NEEDLE_LINE)] + try { + const host = await start(t, home, { saved: sharedSeed() }) + assert.notEqual(host.route, undefined, 'the channel registered its /api/bot route') + const response = await host.route.fetch(new Request('http://localhost/api/bot', { + method: 'POST', + headers: { 'x-flybot-member-session': TOKEN_OWNER_2 }, + body: JSON.stringify({ endpoint: 'chats.search', payload: { query: 'needle' } }), + })) + const result = await response.json() + assert.equal(result.ok, true, JSON.stringify(result)) + assert.deepEqual(result.value.rows.map(row => row.chat), ['b'], JSON.stringify(result.value)) + assert.equal(result.value.rows[0].turn, 5) + assert.match(result.value.rows[0].text, /NEEDLE/) + } finally { + delete HISTORY.duo + } +}) diff --git a/core/test/history.test.js b/core/test/history.test.js index 3c265f9d6..57885af79 100644 --- a/core/test/history.test.js +++ b/core/test/history.test.js @@ -41,11 +41,11 @@ test('termQuery matches long words with FTS5 and short ones with LIKE', () => { // The rows live in a plain table with UNIQUE(session, seq); the FTS5 table is contentless // over it, so a ranked query joins the index and reads the columns from the rows (#2711). assert.deepEqual(termQuery(['needle', 'a"b"c'], sessions, 20), { - sql: 'SELECT lines.text, lines.session, lines.seq, lines.time, lines.who FROM lines_fts JOIN lines ON lines.id = lines_fts.rowid WHERE lines_fts MATCH ? AND lines.session IN (SELECT value FROM json_each(?)) ORDER BY rank LIMIT 20', + sql: 'SELECT lines.text, lines.session, lines.seq, lines.time, lines.who, lines.turn FROM lines_fts JOIN lines ON lines.id = lines_fts.rowid WHERE lines_fts MATCH ? AND lines.session IN (SELECT value FROM json_each(?)) ORDER BY rank LIMIT 20', args: ['"needle" "a""b""c"', '["s1","s2"]'], }) assert.deepEqual(termQuery(['会议', '5%'], sessions, 5, { session: 's2', from: 40 }), { - sql: "SELECT lines.text, lines.session, lines.seq, lines.time, lines.who FROM lines WHERE lines.session IN (SELECT value FROM json_each(?)) AND NOT (lines.session = ? AND lines.seq >= ?) AND lines.text LIKE ? ESCAPE '\\' AND lines.text LIKE ? ESCAPE '\\' ORDER BY lines.time DESC LIMIT 5", + sql: "SELECT lines.text, lines.session, lines.seq, lines.time, lines.who, lines.turn FROM lines WHERE lines.session IN (SELECT value FROM json_each(?)) AND NOT (lines.session = ? AND lines.seq >= ?) AND lines.text LIKE ? ESCAPE '\\' AND lines.text LIKE ? ESCAPE '\\' ORDER BY lines.time DESC LIMIT 5", args: ['["s1","s2"]', 's2', 40, '%会议%', '%5\\%%'], }) }) diff --git a/core/test/palette-preview.test.js b/core/test/palette-preview.test.js index 75c1c3d80..2355f330e 100644 --- a/core/test/palette-preview.test.js +++ b/core/test/palette-preview.test.js @@ -163,3 +163,132 @@ test('previewOf falls back to an older turn when the newest turn is empty', () = const unsent = outline([{ turn: 1, response: 'older reply', prompt: 'q1' }, { turn: 2, response: 'held reply', prompt: 'What is 2+2?' }]) assert.equal(previewOf(unsent, 's1', [], [2]), 'What is 2+2?') }) + +// P-820. The outline the message search reads holds only the first 50/120 characters of each +// prompt and response, so a hit past that budget found nothing. The host's whole-text search +// answers those rows, and the palette renders them as their own group, newest first. +// +// The rows arrive after a round-trip, so the palette needs state that changes once the +// promise lands: this harness runs the same bundle under a react whose useState setter +// rerenders and whose useEffect runs with its cleanup — a minimal React for one component +// tree. Layout effects stay inert: they place the glide thumb, and nothing here asserts motion. +function livePalette({ start = '', raw = '', rows = [] } = {}) { + let live + const instance = { cursor: 0, hooks: [], pending: [] } + const react = { + createElement: h, Fragment: Symbol('F'), + useLayoutEffect() {}, + useRef: (current) => { const slot = instance.hooks ??= []; return slot[instance.cursor++] ??= { current } }, + useState(init) { + const slot = instance.hooks[instance.cursor++] ??= { value: typeof init === 'function' ? init() : init } + const set = (next) => { + const value = typeof next === 'function' ? next(slot.value) : next + if (Object.is(value, slot.value)) return + slot.value = value + run() + } + return [slot.value, set] + }, + useEffect(effect, deps) { + instance.pending.push({ effect, deps }) + }, + } + const calls = { openSession: [], search: [] } + const actions = { + closePalette() {}, reportError() {}, + openSession: (...args) => { calls.openSession.push(args) }, + searchChats: (query, signal) => { calls.search.push([query, signal]); return Promise.resolve(rows) }, + } + const useSessions = (selector) => selector(outlineOf(raw)) + const module = { exports: {} } + runInNewContext(outputFiles[0].text, { + module, exports: module.exports, + document: { hidden: false }, + window: { addEventListener() {}, removeEventListener() {} }, + navigator: { language: 'en' }, localStorage: { getItem: () => null, setItem() {} }, + setTimeout, clearTimeout, AbortController, + require: (name) => { + if (name === 'react') return react + if (name === 'react-dom') return { createPortal: (node) => node } + if (name === '@deepseek-ai/dsh-client-ui-primitives') return { FishLogo: () => null, MarkdownText: ({ text }) => text } + throw new Error(`Unexpected platform module ${name}`) + }, + }) + const run = () => { + if (instance.rendering) { instance.rerun = true; return } + instance.rendering = true + do { + instance.rerun = false + instance.cursor = 0 + instance.pending = [] + live = module.exports.CommandPalette({ start, token: 't', roster: ROSTER, actions, useSessions }) + for (const { effect, deps } of instance.pending.splice(0)) { + // Deps are compared per hook site: an effect with the same deps does not rerun. + const prior = instance.effectStates ??= [] + const at = instance.effectCursor ??= 0 + instance.effectCursor += 1 + const state = prior[at] ??= { ran: false, cleanup: null, deps: undefined } + const changed = !state.ran || deps === undefined || (deps.length > 0 && !deps.every((dep, index) => Object.is(dep, state.deps?.[index]))) + if (!changed) continue + state.cleanup?.() + state.cleanup = effect() ?? null + state.ran = true + state.deps = deps + } + instance.effectCursor = 0 + } while (instance.rerun) + instance.rendering = false + } + run() + const settle = async (ms = 250) => { await new Promise(resolve => setTimeout(resolve, ms)); for (let i = 0; i < 5; i += 1) await Promise.resolve() } + const walk = (node, visit) => { + if (node === null || typeof node !== 'object') return + if (Array.isArray(node)) { for (const child of node) walk(child, visit); return } + visit(node) + const children = node.props?.children + if (Array.isArray(children)) for (const child of children) walk(child, visit) + else if (children !== undefined) walk(children, visit) + } + const flips = () => { + const out = [] + walk(live, node => { const flip = node.props?.['data-flip']; if (typeof flip === 'string') out.push(node) }) + return out + } + return { + calls, settle, run, + sections: () => flips().map(node => node.props['data-flip']).filter(flip => flip.startsWith('section:')).map(flip => flip.slice('section:'.length)), + rows: () => flips().map(node => node.props['data-flip']).filter(flip => !flip.startsWith('section:')), + choose: (key) => { const node = flips().find(node => node.props['data-flip'] === key); node?.props.onClick?.() }, + } +} + +// The stored outline keeps the first 120 characters: a needle past that is invisible to the +// outline search (the defect this fixes), and the host's row is the only place it can surface. +test('P-820: a hit past the outline preview budget is found by the whole-text group', async () => { + const capped = 'x'.repeat(119) + '…' + assert.deepEqual(hits('needle', capped), [], 'the outline preview holds no needle') + const palette = livePalette({ start: 'needle', raw: capped, rows: [{ chat: 's1', turn: 7, text: 'x'.repeat(24) + ' NEEDLE' }] }) + await palette.settle() + assert.deepEqual(palette.calls.search.map(([query]) => query), ['needle'], 'the host search was asked for the query') + assert.deepEqual(palette.sections(), ['Messages (full text)'], JSON.stringify(palette.sections())) + assert.deepEqual(palette.rows(), ['s1:7']) +}) + +test('P-820: choosing a whole-text row opens the chat at that turn', async () => { + const palette = livePalette({ start: 'needle', raw: 'a plain turn', rows: [{ chat: 's1', turn: 7, text: 'NEEDLE at the far end' }] }) + await palette.settle() + palette.choose('s1:7') + // The row object comes from the palette's realm, so the fields are read, not deep-equalled. + assert.equal(palette.calls.openSession.length, 1) + assert.equal(palette.calls.openSession[0][0], 's1') + assert.equal(palette.calls.openSession[0][1]?.turn, 7) +}) + +// A row the outline search already found for the same chat and turn is the same message: the +// whole-text group carries only what the previews missed. +test('P-820: a row the outline already found is not shown twice', async () => { + const palette = livePalette({ start: 'needle', raw: 'the needle is right here', rows: [{ chat: 's1', turn: 1, text: 'the needle is right here' }, { chat: 's1', turn: 9, text: 'another NEEDLE far past the budget' }] }) + await palette.settle() + assert.deepEqual(palette.sections(), ['Messages', 'Messages (full text)'], JSON.stringify(palette.sections())) + assert.deepEqual(palette.rows(), ['s1:1', 's1:9'], JSON.stringify(palette.rows())) +}) diff --git a/docs/subsystems/core-client.md b/docs/subsystems/core-client.md index 6b6a38fc5..06c6e9d67 100644 --- a/docs/subsystems/core-client.md +++ b/docs/subsystems/core-client.md @@ -251,6 +251,12 @@ The member's own chat keeps its `busyAcks[sessionId]` bubble and its visibility - An ordinary group's `@` still shows the seat as `remoteBotTitle` (`Backend (Friend)`, handle on the second line) but inserts `remoteCallName` (#1141): the Bot's own name (`@Backend`, `@architect`), which `callsIn` also aliases. It does not insert a second `@`, a `.home` handle, or the placeholder `(Friend)`. A handle used as that name goes through `cleanPeerName`, so a newline or a fence fragment in the handle is not the insert token. - A face room's `@` menu shows no DSH Sessions rows (#305, owner decision 2026-10-19: filter in FlyBot for now; #422 asks DSH for an API). DSH's built-in `reference` source cannot be dropped for one Session, so `installHeaderEffects` (`header-effects.js`) starts `actions.filterFaceAtMenu` for a face room, which watches that Session's trigger controller (`inputTriggers.sessionOf`, the same one the composer uses) with `watchFaceMenu` (`face-at-filter.js`): a `