Take a customer who asks a food manufacturer's labelling AI agent whether the lemon drizzle loaf contains nuts. The agent can answer from revision 3 of the declaration, which lists wheat, egg, and milk. However, a recipe change puts almond flour in the loaf, so revision 4 in the manufacturer's records contains the warning 'Not suitable for a nut allergy'. The record changes while the index holds the version from the last write, so the AI agent answers from an out-of-date declaration without any error.
The authors of a 2026 diagnostic study measure how much stale context affects an answer. When the models retrieve only from an out-of-date snapshot, their answers contain out-of-date references on 15 of 17 samples with one model and on 13 of 17 with the other. The authors report those results as increases of 88.2 and 76.5 percentage points over retrieval from the current state (arXiv 2605.14478). Their samples are changes to Python code; however, a search index can return only what it holds, so an out-of-date index can mislead an agent in the same way.
The model in the files below is a scripted mock that builds each answer from the words of Narsil's hits. Replace it with a real model when you want the phrasing to vary. Each code block below holds one whole file.
An index behind the record
onFile holds the manufacturer's record of every declaration, which insertBatch copies into the index. The code then writes the recipe change, revision 4, to the record alone. In this form of the bug, your code writes a change to only one of two stores.
import { createNarsil } from '@delali/narsil'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { type DeclarationHit, labellingModel, LOAF_QUESTION, LOAF_SEARCH } from './mock-model.ts'
type Declaration = {
id: string
product: string
declaration: string
revision: number
}
const onFile = new Map<string, Declaration>([
[
'lemon-drizzle-loaf',
{
id: 'lemon-drizzle-loaf',
product: 'Lemon drizzle loaf',
declaration: 'Contains wheat, egg, and milk. Made on a line that also handles sesame.',
revision: 3,
},
],
[
'cherry-bakewell-slice',
{
id: 'cherry-bakewell-slice',
product: 'Cherry bakewell slice',
declaration: 'Contains wheat, egg, milk, and almond. Not suitable for a nut allergy.',
revision: 7,
},
],
[
'oat-and-raisin-cookie',
{
id: 'oat-and-raisin-cookie',
product: 'Oat and raisin cookie',
declaration: 'Contains oats and wheat. Made on a line that also handles peanut.',
revision: 2,
},
],
])
const narsil = await createNarsil()
await narsil.createIndex('product_declarations', {
schema: { product: 'string', declaration: 'string', revision: 'number' },
language: 'english',
})
await narsil.insertBatch('product_declarations', [...onFile.values()])
onFile.set('lemon-drizzle-loaf', {
id: 'lemon-drizzle-loaf',
product: 'Lemon drizzle loaf',
declaration: 'Contains wheat, egg, milk, and almond. Not suitable for a nut allergy.',
revision: 4,
})
const labellingTools = {
searchDeclarations: tool({
description: 'Search the allergen declarations by what their text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<DeclarationHit[]> => {
const found = await narsil.query<DeclarationHit>('product_declarations', {
term: text,
document: { include: ['product', 'declaration', 'revision'] },
limit: 1,
})
return found.hits.map((hit) => hit.document)
},
}),
}
const askLabellingAgent = async (question: string, searchText: string): Promise<string> => {
const agent = new ToolLoopAgent({
model: labellingModel(searchText),
instructions: 'Answer from the allergen declarations alone.',
tools: labellingTools,
})
const reply = await agent.generate({ prompt: question })
return reply.text
}
console.log(
JSON.stringify(
{
question: LOAF_QUESTION,
agentAnswered: await askLabellingAgent(LOAF_QUESTION, LOAF_SEARCH),
onFileNow: onFile.get('lemon-drizzle-loaf')?.declaration,
},
null,
2
)
)
await narsil.shutdown(){
"question": "Does the lemon drizzle loaf contain nuts?",
"agentAnswered": "Contains wheat, egg, and milk. Made on a line that also handles sesame. (Lemon drizzle loaf, revision 3)",
"onFileNow": "Contains wheat, egg, milk, and almond. Not suitable for a nut allergy."
}The panel holds the AI agent's answer and the record at the moment of that answer, which differ. A customer can act on a labelling answer, although the revision number in that answer is the only sign of the problem.
- Lemon drizzle loafrev 4rev 3
- Cherry bakewell slicerev 7rev 7
- Oat and raisin cookierev 2rev 2
Write the change to both stores at once
declare is the only function that changes a declaration, so the code writes every change to the record and to the index in the same call. With update, the engine removes the old document under that id and indexes the new one, so from the next query onwards, it ranks the declaration by its new words.
import { createNarsil } from '@delali/narsil'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { type DeclarationHit, labellingModel, LOAF_QUESTION, LOAF_SEARCH } from './mock-model.ts'
type Declaration = {
id: string
product: string
declaration: string
revision: number
}
const onFile = new Map<string, Declaration>([
[
'lemon-drizzle-loaf',
{
id: 'lemon-drizzle-loaf',
product: 'Lemon drizzle loaf',
declaration: 'Contains wheat, egg, and milk. Made on a line that also handles sesame.',
revision: 3,
},
],
[
'cherry-bakewell-slice',
{
id: 'cherry-bakewell-slice',
product: 'Cherry bakewell slice',
declaration: 'Contains wheat, egg, milk, and almond. Not suitable for a nut allergy.',
revision: 7,
},
],
[
'oat-and-raisin-cookie',
{
id: 'oat-and-raisin-cookie',
product: 'Oat and raisin cookie',
declaration: 'Contains oats and wheat. Made on a line that also handles peanut.',
revision: 2,
},
],
])
const narsil = await createNarsil()
await narsil.createIndex('product_declarations', {
schema: { product: 'string', declaration: 'string', revision: 'number' },
language: 'english',
})
await narsil.insertBatch('product_declarations', [...onFile.values()])
onFile.set('lemon-drizzle-loaf', {
const declare = async (declaration: Declaration): Promise<void> => {
onFile.set(declaration.id, declaration)
await narsil.update('product_declarations', declaration.id, declaration)
}
await declare({
id: 'lemon-drizzle-loaf',
product: 'Lemon drizzle loaf',
declaration: 'Contains wheat, egg, milk, and almond. Not suitable for a nut allergy.',
revision: 4,
})
const labellingTools = {
searchDeclarations: tool({
description: 'Search the allergen declarations by what their text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<DeclarationHit[]> => {
const found = await narsil.query<DeclarationHit>('product_declarations', {
term: text,
document: { include: ['product', 'declaration', 'revision'] },
limit: 1,
})
return found.hits.map((hit) => hit.document)
},
}),
}
const askLabellingAgent = async (question: string, searchText: string): Promise<string> => {
const agent = new ToolLoopAgent({
model: labellingModel(searchText),
instructions: 'Answer from the allergen declarations alone.',
tools: labellingTools,
})
const reply = await agent.generate({ prompt: question })
return reply.text
}
console.log(
JSON.stringify(
{
question: LOAF_QUESTION,
agentAnswered: await askLabellingAgent(LOAF_QUESTION, LOAF_SEARCH),
onFileNow: onFile.get('lemon-drizzle-loaf')?.declaration,
},
null,
2
)
)
await narsil.shutdown(){
"question": "Does the lemon drizzle loaf contain nuts?",
"agentAnswered": "Contains wheat, egg, milk, and almond. Not suitable for a nut allergy. (Lemon drizzle loaf, revision 4)",
"onFileNow": "Contains wheat, egg, milk, and almond. Not suitable for a nut allergy."
}With declare, the AI agent answers with revision 4 and the allergy warning. update throws DOC_NOT_FOUND when the index has no document with that id, so you can catch that error to find a record that the index lacks. Read Errors for how to branch on the code with isNarsilError.
Withdraw a product, and wait for the copies
When the manufacturer withdraws a product, remove its document. remove deletes the document from the index, so a query returns nothing for that product, which is the true answer once the manufacturer stops making it.
waitForWrites(indexName) resolves when every worker copy of the index holds each write that completes before the waitForWrites call. For an index with no copies, the promise resolves at once, so the call adds no delay to this three-document index, while the same code stays correct once the index gains worker copies. For one write, pass { wait: true } to that write, while after a series of writes, call waitForWrites once. Read Partitions and workers for the worker copies and for how the engine routes queries to them.
import { createNarsil } from '@delali/narsil'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { type DeclarationHit, labellingModel, LOAF_QUESTION, LOAF_SEARCH } from './mock-model.ts'
import {
COOKIE_QUESTION,
COOKIE_SEARCH,
type DeclarationHit,
labellingModel,
LOAF_QUESTION,
LOAF_SEARCH,
} from './mock-model.ts'
type Declaration = {
id: string
product: string
declaration: string
revision: number
}
const onFile = new Map<string, Declaration>([
[
'lemon-drizzle-loaf',
{
id: 'lemon-drizzle-loaf',
product: 'Lemon drizzle loaf',
declaration: 'Contains wheat, egg, and milk. Made on a line that also handles sesame.',
revision: 3,
},
],
[
'cherry-bakewell-slice',
{
id: 'cherry-bakewell-slice',
product: 'Cherry bakewell slice',
declaration: 'Contains wheat, egg, milk, and almond. Not suitable for a nut allergy.',
revision: 7,
},
],
[
'oat-and-raisin-cookie',
{
id: 'oat-and-raisin-cookie',
product: 'Oat and raisin cookie',
declaration: 'Contains oats and wheat. Made on a line that also handles peanut.',
revision: 2,
},
],
])
const narsil = await createNarsil()
await narsil.createIndex('product_declarations', {
schema: { product: 'string', declaration: 'string', revision: 'number' },
language: 'english',
})
await narsil.insertBatch('product_declarations', [...onFile.values()])
const declare = async (declaration: Declaration): Promise<void> => {
onFile.set(declaration.id, declaration)
await narsil.update('product_declarations', declaration.id, declaration)
}
const withdraw = async (id: string): Promise<void> => {
onFile.delete(id)
await narsil.remove('product_declarations', id)
}
await declare({
id: 'lemon-drizzle-loaf',
product: 'Lemon drizzle loaf',
declaration: 'Contains wheat, egg, milk, and almond. Not suitable for a nut allergy.',
revision: 4,
})
await withdraw('oat-and-raisin-cookie')
await narsil.waitForWrites('product_declarations')
const labellingTools = {
searchDeclarations: tool({
description: 'Search the allergen declarations by what their text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<DeclarationHit[]> => {
const found = await narsil.query<DeclarationHit>('product_declarations', {
term: text,
document: { include: ['product', 'declaration', 'revision'] },
limit: 1,
})
return found.hits.map((hit) => hit.document)
},
}),
}
const askLabellingAgent = async (question: string, searchText: string): Promise<string> => {
const agent = new ToolLoopAgent({
model: labellingModel(searchText),
instructions: 'Answer from the allergen declarations alone.',
tools: labellingTools,
})
const reply = await agent.generate({ prompt: question })
return reply.text
}
console.log(
JSON.stringify(
{
question: LOAF_QUESTION,
agentAnswered: await askLabellingAgent(LOAF_QUESTION, LOAF_SEARCH),
onFileNow: onFile.get('lemon-drizzle-loaf')?.declaration,
},
[
{ question: LOAF_QUESTION, agentAnswered: await askLabellingAgent(LOAF_QUESTION, LOAF_SEARCH) },
{ question: COOKIE_QUESTION, agentAnswered: await askLabellingAgent(COOKIE_QUESTION, COOKIE_SEARCH) },
],
null,
2
)
)
await narsil.shutdown()[
{
"question": "Does the lemon drizzle loaf contain nuts?",
"agentAnswered": "Contains wheat, egg, milk, and almond. Not suitable for a nut allergy. (Lemon drizzle loaf, revision 4)"
},
{
"question": "Does the oat and raisin cookie contain nuts?",
"agentAnswered": "No declaration is on file for that product."
}
]A search for the withdrawn cookie returns nothing, so the AI agent tells the customer that no declaration is on file. When you mark the cookie as withdrawn inside its text, the words by which the engine ranks it stay in the index, so the model can receive the allergen line alongside the marking. That is why the code removes the document.
When a database holds the records in place of a map, connect the same pair of writes to that database's change feed, with one subscriber that applies each committed change to the index. When the process stops between the commit and the index write, the index holds the old version. To repair the index, clear it and load every record from the source again with insertBatch. Clear the index first, because for an id that the index already holds, insertBatch puts that id in failed with DOC_ALREADY_EXISTS.
Where to go next
- Read Indexes and documents for
insert,update,remove, the batch calls, and the errors that each of them throws. - Read Partitions and workers for the worker copies that
waitForWriteswaits on, and for how the engine routes queries to them. - Read Record every search for a journal that holds the hit ids behind every answer, so that a reader can find the revision behind each one.