Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 55 additions & 28 deletions ds-bot/src/host/chat-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@ export function install(rt) {
}
}

let disposed = false
function openIndex() {
if (disposed) return Promise.resolve(null)
if (chatIndex !== undefined) return Promise.resolve(chatIndex)
const opening = indexOpening ??= (async () => {
let DatabaseSync
try {
({ DatabaseSync } = await import('node:sqlite'))
if (disposed) return null
await mkdir(home, { recursive: true })
if (disposed) return null
} catch (error) {
rt.warn('ds-bot: no chat index, read_own_chat scans the logs: %s', error)
return null
Expand All @@ -165,9 +169,11 @@ export function install(rt) {
// index would be deleted under it.
const damaged = error => /malformed|not a database|corrupt|SQLITE_CORRUPT|SQLITE_NOTADB/i.test(String(error?.message ?? error))
for (const fresh of [false, true]) {
if (disposed) return null
try {
if (fresh) {
await Promise.all([indexPath, `${indexPath}-wal`, `${indexPath}-shm`].map(file => unlink(file).catch(() => {})))
if (disposed) return null
}
return createIndex(new DatabaseSync(indexPath))
} catch (error) {
Expand All @@ -180,7 +186,7 @@ export function install(rt) {
return null
})().then((index) => {
// The plugin may have been disposed while the file opened.
if (indexOpening !== opening) {
if (disposed || indexOpening !== opening) {
index?.db.close()
return null
}
Expand Down Expand Up @@ -243,30 +249,40 @@ export function install(rt) {
}
}
ctx.effect(() => () => {
disposed = true
const pending = indexOpening
indexOpening = undefined
chatIndex?.db.close()
chatIndex = undefined
indexOpening = undefined
return pending?.then(() => {
chatIndex?.db.close()
chatIndex = undefined
}, () => {})
}, 'ds-bot: chat index')

// Adds what a part said since its mark. Reads of one part wait for each other, so no
// line is added twice.
const catchingUp = new Map()
const redactText = text => (typeof rt.redactSecrets === 'function' ? rt.redactSecrets(String(text ?? '')) : String(text ?? ''))
function catchUp(index, part, { group = false } = {}) {
if (disposed || index !== chatIndex) return Promise.resolve()
const running = catchingUp.get(part.sessionId)
if (running) return running
const job = (async () => {
if (disposed || index !== chatIndex) return
const mark = index.mark.get(part.sessionId)
if (mark?.sealed) return
const through = mark?.through ?? -1
const events = []
for await (const event of eventsBackwards(part.sessionId, AbortSignal.timeout(INDEX_TIMEOUT_MS), Infinity)) {
if (disposed || index !== chatIndex) return
if (event.seq <= through) break
events.push(event)
}
if (disposed || index !== chatIndex) return
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
if (disposed || index !== chatIndex) return
let keep = mark ? mark.keep === 1 : false
let turn = mark?.turn ?? -1
const seal = part.endedAt !== undefined
Expand Down Expand Up @@ -320,20 +336,24 @@ export function install(rt) {
// existing v5 database still get full tool-output coverage without wiping text rows.
const catchingUpTools = new Map()
function catchUpToolOutputs(index, part) {
if (disposed || index !== chatIndex) return Promise.resolve()
const running = catchingUpTools.get(part.sessionId)
if (running) return running
const job = (async () => {
if (disposed || index !== chatIndex) return
const mark = index.toolMark.get(part.sessionId)
if (mark?.sealed) return
const through = mark?.through ?? -1
const events = []
for await (const event of eventsBackwards(part.sessionId, AbortSignal.timeout(INDEX_TIMEOUT_MS), Infinity)) {
if (disposed || index !== chatIndex) return
if (event.seq <= through) break
events.push(event)
}
if (disposed || index !== chatIndex) return
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
if (disposed || index !== chatIndex) return
const seal = part.endedAt !== undefined
&& !Number.isInteger(part.throughSeq)
&& rt.isBotSession?.(part.sessionId) !== true
Expand Down Expand Up @@ -390,11 +410,15 @@ export function install(rt) {
// (`hidden`): it is in context, and its own question would match.
async function indexedParts(botId, askingId, { roomId } = {}) {
const index = await openIndex()
if (index === null) return { index }
if (index === null || disposed || index !== chatIndex) return { index: null }
const parts = resolveParts(botId, askingId, roomId)
const group = roomId !== undefined
// A part that cannot be read now is searched as far as it was indexed.
for (const part of parts) await catchUp(index, part, { group }).catch(error => rt.warn('ds-bot: indexing %s failed: %s', part.sessionId, error))
for (const part of parts) {
if (disposed || index !== chatIndex) return { index: null }
await catchUp(index, part, { group }).catch(error => rt.warn('ds-bot: indexing %s failed: %s', part.sessionId, error))
}
if (disposed || index !== chatIndex) return { index: null }
const turn = askingId !== undefined && askingId === parts.at(-1)?.sessionId ? index.mark.get(askingId)?.turn : undefined
return { index, parts, hidden: turn === undefined || turn < 0 ? undefined : { session: askingId, from: turn } }
}
Expand All @@ -404,7 +428,7 @@ export function install(rt) {
async function searchOwnChat(botId, query, { askingId, limit = OWN_CHAT_SEARCH, roomId } = {}) {
const terms = [...new Set(query.toLowerCase().split(/\s+/).filter(Boolean))].slice(0, SEARCH_TERMS_MAX)
const { index, parts, hidden } = await indexedParts(botId, askingId, { roomId })
if (index === null) return { lines: await scanOwnChat(botId, query, limit, askingId, { roomId }), every: true }
if (index === null || disposed || index !== chatIndex) return { lines: await scanOwnChat(botId, query, limit, askingId, { roomId }), every: true }
if (parts.length === 0) return { lines: [], every: true }
const sessions = parts.map(part => part.sessionId)
const bounds = parts
Expand Down Expand Up @@ -456,7 +480,7 @@ export function install(rt) {
// The latest messages across all parts, oldest first.
async function latestOwnChat(botId, { askingId, limit = OWN_CHAT_READ, roomId } = {}) {
const { index, parts, hidden } = await indexedParts(botId, askingId, { roomId })
if (index === null) return scanOwnChat(botId, '', limit, askingId, { roomId })
if (index === null || disposed || index !== chatIndex) return scanOwnChat(botId, '', limit, askingId, { roomId })
const rows = []
for (const part of [...parts].reverse()) {
let below = hidden?.session === part.sessionId ? hidden.from : Number.MAX_SAFE_INTEGER
Expand All @@ -471,7 +495,7 @@ export function install(rt) {
// The messages around an anchor, in its part.
async function aroundOwnChat(botId, anchor, askingId, { roomId } = {}) {
const { index, parts } = await indexedParts(botId, askingId, { roomId })
if (index === null) return { error: 'Anchors need the chat index, which is unavailable here. Search with words instead.' }
if (index === null || disposed || index !== chatIndex) return { error: 'Anchors need the chat index, which is unavailable here. Search with words instead.' }
const found = /(?:part\s*)?(\d+)?\s*#\s*(\d+)/i.exec(anchor)
if (found === null) return { error: `"${anchor}" is not an anchor. Anchors look like "part 2 #1234", or "#1234" in the current part.` }
const number = found[1] === undefined ? parts.length : Number(found[1])
Expand Down Expand Up @@ -561,31 +585,34 @@ export function install(rt) {
if (pinnedPart === undefined) return { found: false }
if (Number.isInteger(pinnedPart.throughSeq) && pinnedSeq > pinnedPart.throughSeq) return { found: false }
}
if (index !== null) {
if (index !== null && !disposed && index === chatIndex) {
const targetParts = pinnedPart !== undefined ? [pinnedPart] : parts
for (const part of targetParts) {
if (disposed || index !== chatIndex) break
await catchUpToolOutputs(index, part).catch(error => rt.warn('ds-bot: indexing tool outputs for %s failed: %s', part.sessionId, error))
}
let row
if (pinnedPart !== undefined) {
row = index.db.prepare(
'SELECT call_id, session, seq, time, name, ok, text FROM tool_outputs WHERE call_id = ? AND session = ? AND seq = ? LIMIT 1'
).get(id, pinnedPart.sessionId, pinnedSeq)
} else {
const sessions = parts.map(part => part.sessionId)
const where = ['call_id = ?', sessions.length === 1 ? 'session = ?' : 'session IN (SELECT value FROM json_each(?))']
const args = [id, sessions.length === 1 ? sessions[0] : JSON.stringify(sessions)]
for (const part of parts) {
if (Number.isInteger(part.throughSeq)) {
where.push('NOT (session = ? AND seq > ?)')
args.push(part.sessionId, part.throughSeq)
if (!disposed && index === chatIndex) {
let row
if (pinnedPart !== undefined) {
row = index.db.prepare(
'SELECT call_id, session, seq, time, name, ok, text FROM tool_outputs WHERE call_id = ? AND session = ? AND seq = ? LIMIT 1'
).get(id, pinnedPart.sessionId, pinnedSeq)
} else {
const sessions = parts.map(part => part.sessionId)
const where = ['call_id = ?', sessions.length === 1 ? 'session = ?' : 'session IN (SELECT value FROM json_each(?))']
const args = [id, sessions.length === 1 ? sessions[0] : JSON.stringify(sessions)]
for (const part of parts) {
if (Number.isInteger(part.throughSeq)) {
where.push('NOT (session = ? AND seq > ?)')
args.push(part.sessionId, part.throughSeq)
}
}
row = index.db.prepare(
`SELECT call_id, session, seq, time, name, ok, text FROM tool_outputs WHERE ${where.join(' AND ')} ORDER BY time DESC, seq DESC LIMIT 1`
).get(...args)
}
row = index.db.prepare(
`SELECT call_id, session, seq, time, name, ok, text FROM tool_outputs WHERE ${where.join(' AND ')} ORDER BY time DESC, seq DESC LIMIT 1`
).get(...args)
if (row !== undefined) return formatHit(parts, row)
}
if (row !== undefined) return formatHit(parts, row)
}
// Scan logs newest-first as fallback (also covers live turns when SQLite is unavailable),
// skipping pruned surfaceOp replacement events so original full tool outputs are recovered.
Expand Down Expand Up @@ -657,7 +684,7 @@ export function install(rt) {
}

function forgetChatIndexSession(sessionId) {
if (typeof sessionId !== 'string' || sessionId === '' || chatIndex === undefined) return
if (disposed || typeof sessionId !== 'string' || sessionId === '' || !chatIndex) return
const index = chatIndex
try {
index.db.exec('BEGIN')
Expand Down
Loading
Loading