feat: index reasoning and close the session freshness gap (#18)
Two changes that make the index reflect work as it happens. Reasoning is now indexed. The model's own justification is where a decision is explained, and it never appears in the text a reader sees. It is also enormous — individual parts reach 175 KB — so each part is truncated to 1.2 KB and each session is budgeted 4 KB. It lands in content (searchable) but stays out of the summary, which is what search results and MCP output show. Capturing it exposed a dedupe bug: sessions were skipped when created_at and summary matched, so content that gained reasoning behind unchanged prose was never recorded. IngestResult now carries a content hash and the sweep compares it, which is what the observation layer already used internally. Freshness: the watcher polls every two minutes, so a finished turn stayed unsearchable that long. IngestSessionsNow shares the watcher's logic, sweeps only sessions newer than a stored watermark, and runs in ~45 ms steady state (a full sweep is ~21 s for 59k messages). The plugin calls it at prompt admission, fire-and-forget.
This commit is contained in:
@@ -25,6 +25,7 @@ import { existsSync } from "node:fs"
|
||||
const DEFAULT_BIN = `${process.env.HOME ?? ""}/assistant/knox/knox`
|
||||
const BIN = process.env.KNOX_BIN ?? (existsSync(DEFAULT_BIN) ? DEFAULT_BIN : "knox")
|
||||
const TIMEOUT_MS = 2000
|
||||
const INGEST_TIMEOUT_MS = 60_000
|
||||
|
||||
type Finding = { assertion: string; value: number; unit: string; status: string }
|
||||
|
||||
@@ -140,9 +141,34 @@ function check(turn: Turn): Promise<Finding[]> {
|
||||
})
|
||||
}
|
||||
|
||||
// Guard against overlapping sweeps when prompts arrive quickly.
|
||||
let refreshing = false
|
||||
|
||||
/**
|
||||
* Re-index opencode sessions in the background.
|
||||
*
|
||||
* knox's watcher polls opencode.db every two minutes, so a finished turn is not
|
||||
* searchable for up to that long. Running the scoped sweep at prompt admission
|
||||
* closes the gap: it is fire-and-forget (~45 ms steady state thanks to the
|
||||
* watermark), so it never delays the turn it is triggered by.
|
||||
*/
|
||||
function refreshSessions(): void {
|
||||
if (refreshing) return
|
||||
refreshing = true
|
||||
execFile(BIN, ["ingest", "sessions"], { timeout: INGEST_TIMEOUT_MS }, () => {
|
||||
refreshing = false
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
id: "knox.claims",
|
||||
async setup(ctx: any) {
|
||||
// Ahead of the guardrail: fresher index, and this is the natural place for
|
||||
// recall to read from once it exists.
|
||||
await ctx.session.hook("prompt", () => {
|
||||
refreshSessions()
|
||||
})
|
||||
|
||||
await ctx.session.hook("context", async (event: any) => {
|
||||
try {
|
||||
const turn = splitTurns(event.messages as any[])
|
||||
|
||||
Reference in New Issue
Block a user