memory-consolidate
v0.2.15Scheduled hygiene for the memory worker: deterministic dedup of near-duplicate memories and an opt-in verify tier that checks notes against their source transcript; every write goes through memory's supersede and correct seams, pinned untouchable. Install, stop, or remove it without touching stored memory.
Install
iii call compose::add worker=memory-consolidate@nextRun in your project with the iii engine running. Exact versions are immutable and digest-pinned.
Full markdown
/registry/workers/memory-consolidate.md?version=next. Paste it into an LLM prompt or pipe it through curl from a worker.Install
iii call compose::add worker=memory-consolidate@0.2.15Dependencies
Readme
memory-consolidate
Scheduled hygiene sibling of the memory worker. Finds near-duplicate memories in every bank and merges them: the duplicate retires with a superseded_by pointer (never a delete), the survivor absorbs the observation as corroboration. A memory from the user always survives over a note an agent saved from a turn, and that note never retires it. An alias tier retags memories whose entity handle is another spelling of a handle the bank already uses. An opt-in promote tier rewrites a decision written with a ticket key without the key, which moves to its entities, so it outlives the ticket's status. An opt-in verify tier checks extraction fidelity: it compares each note with the span of conversation it was extracted from and replaces a note that span contradicts. Every write goes through memory's supersede, update and correct seams (memory::supersede, memory::update, memory::correct), pinned memories untouchable, every change visible as a memory::on-item-changed event. Install, stop, or remove it without touching stored memory.
Install
iii call compose::add worker=memory-consolidateRequires the memory worker. Scheduling reuses the cron worker: memory-consolidate::on-tick observes cron::on-tick with args { expression: "0 0 * * * *" }, an hourly heartbeat, and the tick runs a pass only when interval_hours (default 24) have elapsed since the last one — the last completed pass persists in the state worker. Catch-up-on-boot: a pass missed while this worker was down runs shortly after boot instead of waiting for the next heartbeat; a slim backstop loop also keeps the schedule alive on rigs without the cron worker.
Quickstart
Plan without writing; the run response is the plan:
iii call memory-consolidate::run dry_run=trueA dry run is read-only: it reports the deterministic groups and the alias pairs it would merge, skips the LLM, semantic, promote and verify tiers and the alias judge (no recall, embedding, transcript, model or rule calls), and writes no memory, cursor, pass report or last_run, so memory-consolidate::status keeps showing the last real pass. The configured dry_run: true (scheduled planning mode) makes the same read-only plan but records it in reports and checkpoints last_run, so the schedule still waits interval_hours between passes.
Apply for one bank only:
iii call memory-consolidate::run bank=blogThe report names every group: the surviving memory, the retired duplicates, and anything skipped because it is pinned. The retired records stay on disk and remain queryable with include_superseded: true.
Each bank's report also counts the live memories the pass read, dry runs included, from the same read and with no model call: scanned, live_chars (their text length in characters, not bytes), ticket_lines (those that name a ticket key such as KAN-162, by memory's rule), agent_notes (notes an agent saved from inside a turn) and transient (those tagged transient). Compared across the reports status keeps, they show whether a bank grows; they do not show how much the memories compress the transcripts they came from, which the counts never read.
Functions
| Function | Input | What it does |
|---|---|---|
memory-consolidate::run |
{ bank?, dry_run? } |
One consolidation pass on demand, over every configured bank or one bank; returns the per-bank report |
memory-consolidate::status |
{} |
The schedule and the last passes: enabled, interval_hours, last_run, due, and reports, the last 30 pass reports newest first ({ ms, dry_run, banks }, each bank with its counts of live memories and its LLM calls, tokens and cost; only the newest keeps its banks' groups, aliases and semantic lists) |
memory-consolidate::alias-undo |
{ bank, from } |
Undo an alias merge: the pair turns distinct in the ledger, then each memory the merge retagged that is live, unpinned, still carries the canonical handle and does not already carry from gets it back; returns { bank, from, to, restored, skipped }. Operator-only, like run |
memory-consolidate::on-tick |
the cron::on-tick event |
Internal: the schedule heartbeat |
memory-consolidate::on-config-change |
the configuration::on-updated event |
Internal: hot-reloads the configuration |
memory-consolidate::rest |
the http worker's HttpRequest |
Internal: serves the run, status and alias-undo REST routes |
The worker hosts no observable events of its own: every change it makes is a memory::on-item-changed call of the memory worker. It observes:
cron::on-tickwith args{ expression: "0 0 * * * *" }, targetmemory-consolidate::on-tick;configuration::on-updated, filtered on its own entry id andconfiguration:updated, targetmemory-consolidate::on-config-change;http::on-requestwith args{ method: "POST", path }forrun,statusandalias-undo(POST /memory-consolidate/run,POST /memory-consolidate/status,POST /memory-consolidate/alias-undo), targetmemory-consolidate::rest(internal). It receives theHttpRequest, runs the function with the request's JSON body as its input (an empty body is{}), and returns theHttpResponse:200with the function's result, or{ error }with400(the body does not fit the input),404or500(the function failed, a bank outside the allowlist included).curl -X POST localhost:3111/memory-consolidate/run -H 'content-type: application/json' -d '{"bank":"blog","dry_run":true}'plans one bank.
Each observer stays pending until its function registers, so the cron, configuration and http workers can boot in any order.
Configuration
All fields hot-reload through the configuration worker: enabled, interval_hours, dry_run (scheduled passes plan-only), banks (allowlist, empty = all), max_supersedes_per_run (safety cap; remainder waits for the next pass), max_llm_calls_per_run (200: router::complete calls per pass, the judge, alias, semantic, promote and verify tiers of every bank together; the rest waits for the next pass with a deferred note; 0 = no model calls), alias_merge_enabled (on by default, see the alias tier below), promote_decisions_enabled (off by default, see the promote tier below), verify_enabled (off by default) and verify_max_per_run (20), see the verify tier below.
To run without model calls, set max_llm_calls_per_run to 0 (spelling aliases still merge, near pairs wait with a note), or turn off alias_merge_enabled and semantic_enabled; llm_assist_enabled, promote_decisions_enabled and verify_enabled are off by default and call the model only when turned on.
The model
llm_model serves the judge, alias, semantic, promote and verify tiers. A pinned provider::id is used as written, and the router is not asked. Left empty, the model is automatic, by the same rule as memory's extraction_model:
- A provider is usable when it is available and signed in, or owns its sign-in (no credential env var).
- Tier 1: the default model of the first usable provider, by provider id.
- Tier 2, only when no usable provider declares a chat default: the first chat model
router::models::listlists for a usable provider, by provider and then model id. - An id that contains
embedorrerankis never picked. With nothing left, the tier stops with a note that names each provider and why it was skipped.
The pick is kept for 60 s, so a pass does not ask the router before every model call. It is logged at INFO when it changes, as automatic consolidation model model=<provider::id> reason=<provider_default|first_listed_model>. A failed router::complete call and a provider error reply both end in (model <id>), so the pass notes name the model. On the judge tier, such an error is noted as llm tier skipped. The pick can change shortly after a restart, until every provider has finished its first model refresh, and a provider whose sign-in has expired can still be picked. Pin llm_model to rule this out.
The alias tier (on by default)
The extraction model can write one thing under two entity handles: build cache on some memories and build-cache on others, or dentista beside dentist. Recall and the hook compare handles as strings, so the two read as two subjects. alias_merge_enabled (on by default) runs this tier on each bank right after the deterministic pass, on the same scan less the memories that pass retires. It retags in place: each memory carrying the alias gets the canonical handle through memory::update (same id, revision + 1, the prior revision stays in memory's log). Nothing is read through an alias map, so recall code is unchanged.
The rule. Similarity is pg_trgm's: lowercase, split into runs of letters and digits, pad each word with two blanks before and one after, take every 3-character window, Jaccard of the two sets. A pair is a candidate only when every guard holds:
- Neither handle is
user, the speaker's fixed handle. It is never retagged, and no handle is retagged into it. - No live memory carries both. Two handles on one memory are two things; the pair is recorded as
distinctand never asked about. - Tier
spelling: the same characters once whitespace,-and_are dropped, case aside, the rule memory's extraction snaps handles with (build cache/build-cache,vs code/vscode). It merges with no model call. Any other character counts:c++/c#andc/c++arenearpairs for the judge, andjava 1.8/java 18never pair (other words and digit runs). - Otherwise tier
nearneeds the same number of words, the same digit runs (us-east-1/us-east-2,pg15/pg16andpython/python3never pair), each word pair equal or at 0.6 or more, and the whole handles at 0.6 or more (dentist/dentista0.70,postgres/postgresql0.67).payments-api/ledger-apifails twice:paymentsagainstledgeris 0.00, the whole handles 0.20. - A handle with two or more candidate partners merges with none of them; its pairs are listed as ambiguous.
The canonical handle is the one on more live memories, then the one whose first memory is older, then the smaller string.
The judge. No similarity floor tells dentist / dentista from two people: gabriel / gabriela scores the same 0.70. So a near pair merges only after a router::complete judge says both handles name the very same person, project, place or tool (another spelling, a plural, another language's word for it); two people, two services, a product and its part are different, and when unsure, different. One call per bank per pass, only when the bank has a near pair with no recorded verdict, for at most 20 pairs, each with both handles and up to two memory texts each; the rest wait for the next pass. The reply is strict JSON, {"pairs":[{"n":1,"same":true,"reason":"..."}]}, and its output budget holds 20 verdicts. A reply cut short, that does not parse, or that answers a pair twice or numbers one outside the batch halves the batch, one call per half, as the semantic tier does. A pair that still fails alone is recorded distinct (kept apart: no judge reply about this pair parsed) once another reply of the pass parsed, so it never holds up the others; when no reply parsed, nothing is recorded and the pairs are asked again on the next pass. Every call counts in llm_calls, tokens and cost and against max_llm_calls_per_run. With no router, or the cap reached, near pairs stay in the report as waits for the judge, with a note.
The ledger. Every decided pair is kept in the state worker (scope memory_consolidate, key aliases:{bank}): { from, to, tier, verdict, reason, ms, ids }, verdict same or distinct, ids the memories the merge retagged. A same entry retags later memories that carry the alias with no judge call, and a distinct entry is never asked again. The entry is written before the retags; a ledger that cannot be read or saved skips the tier with a note, so it never judges or writes blind.
Writes. Each retag is memory::update { bank, id, entities, expected_memory, reason }: entities is the memory's list with the alias replaced, order kept, repeats dropped; expected_memory is the record as this pass read it, so memory refuses one edited since; reason (alias: dentista -> dentist (<why>)) rides the memory::on-item-changed event. Pinned memories are never retagged and are listed in skipped_pinned. A memory whose retag would carry more than eight handles is left alone with a note: an older memory worker keeps only the first eight, which would drop user. Each retag spends one unit of max_supersedes_per_run; the rest counts in capped and waits for the next pass. A memory that absorbed a duplicate earlier in the same pass is retagged on the next pass. A failed retag is a bank error, so the pass stays due. A retag bumps updated_at, so the semantic and verify tiers look at that memory again. An older memory worker ignores expected_memory and reason and retags all the same.
Report. Each bank lists aliases: { from, to, tier, similarity, applied, ids, skipped_pinned, reason } per pair merged, judged distinct this pass, waiting for the judge, or ambiguous. A dry run lists what it would merge, applied: false, and never calls the judge or writes.
Undo. memory-consolidate::alias-undo { bank, from } turns the pair's entry distinct first, so no pass merges it again, then puts from back on each recorded memory that is live, unpinned, still carries to, does not already carry from (on a memory with both, the merge or a person may have put to there, so undo drops neither; it is listed in skipped for a hand edit) and holds at most eight handles, with the reason alias merge undone: <to> -> <from>. A failed write leaves the rest for a second call. No pass merges user, so an entry naming it (written by hand) is refused, and user is never taken off or put on a memory.
Not covered: accents (júlia / julia is 0.33), a name against a full name (rafael / rafael souza, different word counts), and aliases across two banks.
The LLM tier (optional)
llm_assist_enabled (off by default) adds one router::complete judge call per bank after the deterministic pass:
- Reorder groups — word-order matches the deterministic pass surfaces report-only ("The deploy runs on Fridays" vs "On Fridays the deploy runs") are merged only when the judge confirms the meaning is identical; role swaps ("Alice manages Bob") stay untouched.
- Rule promotion — memories re-observed
promote_corroboration_threshold+ times are offered as candidates, except a note an agent saved from a turn (confidencestatedwith asource.turn_id, pinned or not) and a line the rule's cap moved back (demoted-rule); ones the judge classifies as standing instructions land as one-line entries in the bank's auto-managedlearnedrule, fingerprint-deduped, append-only. The judge sees thelearnedlines already in force: a candidate one of them already states is never promoted, and the judge lists its id ascovered. The ids of promoted and covered memories are kept in the state worker (scopememory_consolidate, keypromoted:{bank}), so a later pass does not offer them again. The write re-readslearnedright before it and sends that read asexpected; when the rule changed in between, it merges again, up to three tries, then waits for the next pass with a note. Hand-authored rules are never touched, and the judge can only act on candidates it was offered — it cannot invent targets.
Fail-soft: an unavailable router just skips the tier (named in the report's notes, so it never blocks the checkpoint), and a malformed reply is treated as "no merges, no promotions". Either way, the deterministic pass has already completed. Each bank's report counts the tiers' model calls in llm_calls, the router's reported usage in llm_input_tokens and llm_output_tokens, and its cost in llm_cost_usd when the router priced the call; past max_llm_calls_per_run the tier is deferred with a note.
The semantic tier (on by default)
semantic_enabled (on by default; false stops this tier's model calls only, the alias judge still calls the model: see above for a pass with none) runs one more tier, last, on a fresh fetch. It costs one router::complete call per 8 new memories, capped at semantic_max_per_run per bank per pass. Without a router or an embed provider, it stops with a note and never blocks the pass. While memories still lack a vector, it waits with a note (see Vectors still landing below). Its calls count in the bank's llm_calls, llm_input_tokens, llm_output_tokens and llm_cost_usd, and it is deferred with a note once the pass reaches max_llm_calls_per_run. A router::complete judge looks at each memory written since the last pass, next to its nearest neighbours, and decides:
- merge: both state the same fact (paraphrase, another language). The duplicate retires and the survivor absorbs its observations. The survivor must keep every detail; when both are equally complete, the older one survives. A merge the judge gives to an agent's note over a memory that is not one is swapped, so the user's line survives and the note retires into it (the number and operator checks then apply to the swapped pair), and an agent's note merged into another memory adds nothing to it.
- supersede: a state change ("moved to", "switched to", a rescheduled date). The outdated record retires in favour of the current one, which is the one stated last.
- nothing: distinct facts on one topic (two allergies, two projects). This is the normal answer.
Every change goes through memory::supersede and carries the judge's one-sentence reason on memory's memory::on-item-changed events. How it works:
- Candidates. Neighbours come from the raw lexical and raw semantic
memory::recallarms, interleaved, because a fused rank buries the near-duplicate. A memory that names a ticket key (KAN-162, by memory's rule for work items) also gets the bank's other live memories naming that key, newest first, up tosemantic_candidates, so a newer status of the ticket can supersede an older one that shares no other word. - Targets. The judge may only pair a memory with one of its own candidates, through short per-batch aliases.
- Validation. Before anything is written, every action is checked: pinned records are never retired; an unpinned agent's note never retires a memory that is not one (a merge is swapped, a supersede refused); direction follows
last stated; a merge may not drop a number the survivor lacks or change an operator or sign; the reason is required; the per-run cap applies. - Freshness. Each record is re-read right before the write, so a record edited since the judge read it drops its action.
- Failure handling.
- Transport, auth or quota errors stop the tier for that bank with a note, and the cursor stays put.
- An unparseable or truncated reply bisects the batch down to one memory. A memory that still fails alone is listed in
semantic_failedand skipped once another reply of the pass parses. Until one does, the cursor stays before it and the tier goes on to the next batch; when that batch has failed replies too and none parsed, the tier stops with a note naming the first such memory and the cursor stays put. - Vectors still landing. Each semantic recall reports
vector_coverage {covered, live}: how many live memories of the bank have a vector for the current embedding model. A memory without one is missing from the semantic arm, so its twin would pass as "no action". Whilecovered < live, the tier defers before any judge call: the cursor stays put and the bank gets the notesemantic tier deferred: N of M memories have no vector for the current embedding model yet; cursor held. This happens during a re-embed after a model change, an embed outage backlog, or a save seconds before the pass. The next scheduled pass tries again. The first pass at least 30 minutes after the first deferral judges with what exists and notessemantic tier judged without N of M memories whose vectors have not landed. A bank with no vector at all for the current model keeps deferring. Any pass that judges clears the wait.
- Cursor. Progress is a per-bank cursor in the state worker, keyed by
(updated_at, id), so edited or resurrected records are judged again. It advances only over batches that were fully judged and fully applied. While the tier waits for vectors, the same value also holdswaiting_since, the time of the first deferral in ms. - Dry runs skip this tier with an
unevaluatednote.
Config: semantic_batch (8 memories per LLM call), semantic_candidates (4 per arm, and up to 4 more naming the same ticket), semantic_max_per_run (48 per bank per pass). The model comes from llm_model. router::complete has no temperature control, so the provider default applies.
Judge eval. tests/judge_golden.rs runs the real prompt and validator over 18 cases from memory's recall golden set (8 supersedes, 4 merges and 6 keeps) and 3 Portuguese ticket cases (2 status supersedes, and a decision written with the key that must survive a status of its ticket). It reports accuracy and harmful actions, which must be 0. With claude-sonnet-5-5 the 18 golden cases scored 18/18 with 0 harmful; the ticket cases have not been run yet. Without an engine, tests/offline_safety.rs replays the golden merge pairs as supplied decisions through the validator on every cargo test. UPDATE_CONSOLIDATION=1 records the golden cases' decisions into memory's tests/recall_quality/consolidation.json, which memory's recall eval replays in its hybrid-consolidated mode: traps go from 2 to 0 and duplicate pollution from 23 to 0.
III_NAMESPACE=<ns> cargo test --test judge_golden -- --ignored --nocaptureThe promote tier (opt-in, off by default)
Memory stops giving a turn a line that names a work item (a ticket key, a pull request, a commit) 7 days after it was last stated: a ticket's status ends with the ticket. A decision, a pitfall or a way of doing something written with the key ("ficou decidido (premissa no KAN-156) que …") expires with it. promote_decisions_enabled (off by default) runs one more tier per bank, after the semantic tier and before the verify tier, on a fresh fetch: such a line is rewritten without the key, and the key moves to the line's entities, so the line stays in turns and stays linked to the ticket.
Candidates. Live, unpinned memories whose text names a ticket key, by memory's rule:
[A-Z]{2,10}-<digits>as a whole word, less memory's list of standards and zones (UTF-8,SHA-256,UTC-3,RFC-…). Pull requests and commits are not keys. A note an agent saved from a turn is a candidate too, as in the verify tier.Judge. One
router::completecall per 10 candidates, oldest first, with a strict JSON reply. Anything else fails to parse.{"lines":[{"line":"l1","kind":"status"},{"line":"l2","kind":"durable","text":"<the line without its keys>"}]}statusis a work item's progress or state and is left alone.durableis a decision, a pitfall, a how-to or a fact that outlives the ticket. The judge is told to answerstatuswhen unsure. A line it does not answer is left alone.Check. A durable rewrite is written only when it drops nothing but the ticket keys and the connector words next to them. Over normalized words (the normalization duplicates use, each symbol it would read as a space, such as
≤,≠,$or?, a word of its own), the rewrite must be the old line with runs of words taken out. A run holds keys and these connector words:a and ao as by card conforme da das de do dos e em for from in issue na nas no nos o of on os para pela pelo per premissa see the ticket ver via. Each connector in it leads to a key (premissa no KAN-156,in MOT-12 and KAN-3), exceptcard,issueorticketright after one (the KAN-1 ticket). So no word or symbol is added, changed or moved, every number other than the keys' stays, and the rewrite is not empty. It names no ticket key by memory's rule, so a key inside a longer word (KAN-2/KAN-3) is refused too. A connector away from a key, or after it, is a word of the fact ("use no retries", "KAN-1: no retries") and is never dropped. The alignment tries every way to pair the words, so inDecidido no KAN-1 no deploy->Decidido no deploythe firstnogoes with the key.ficou decidido (premissa no KAN-156) que X->ficou decidido que Xpasses. A refused rewrite counts indecisions_refused, writes nothing, and ends in one note per bank, for examplepromote: refused 1 (a number of the line is gone 1).The write. First
memory::update { bank, id, entities, expected_memory, reason }adds the keys, lowercased, to the line's entities, guarded with the line as this pass read it. Thenmemory::correctreplaces the line with the rewrite, guarded with the record the update returned. The new record keeps the old one's entities (the keys included), tags, confidence, source and statement date, and the old one retires into it with asuperseded_bypointer. The reason, for examplepromoted off the ticket: kan-156 kept as an entity, is stored assource.correction_reasonand rides the events. When the rewrite already exists as a live memory, the line retires into that twin, which then gets the keys through the same guardedmemory::update; a pinned twin, or one with no room for them, is left as it is with a note:promote: the record that took `<id>` does not carry kan-156. When memory refuses the correction (changed,edited,text_exists, ...), the line stays as it was, with the key now also an entity, and a note counts it:promote: skipped 1 (edited 1). A line whose keys are not all entities yet and would then carry more than eight handles is left alone (more than 8 handles). A line that also names a pull request or a commit still expires after the rewrite.Cursor. A per-bank cursor in the state worker (scope
memory_consolidate, keypromote_cursor:{bank}), keyed by(updated_at, id)and saved after each batch, so a status line is judged once, and again only when it is rewritten.Caps. Judge calls count in
llm_calls, tokens and cost, and againstmax_llm_calls_per_run. Each promotion spends one unit ofmax_supersedes_per_runand counts indecisions_promoted, notsuperseded. When either cap runs out the tier isdeferredwith a note; a batch cut short by the write cap is judged again on the next pass. The model comes fromllm_model.Failure handling. A reply that does not parse or is cut short splits the batch in halves, down to one line, as in the semantic tier. A lone line whose reply is still unusable is passed once another reply of the pass has parsed (
promote: skipped 1 (no usable reply 1)); while none has, a second such line or the batch's end stops the tier, the cursor before the batch. A transport error or a failed write stops the tier for that bank with a note, the cursor before the batch. Memory'sforbidden(a run started inside an agent turn) is a bank error.Dry runs make no model call and no write. The bank gets the note
dry-run: promote tier unevaluated; N unpinned memories name a ticket key.
Judge eval. tests/promote_golden.rs sends 10 labelled lines (4 status, 3 decisions, 3 pitfalls, in Portuguese and English) as one batch through the real prompt, parser and check. A status line rewritten is harmful and must be 0. A durable line left alone or refused is a miss. It has not been run yet.
III_NAMESPACE=<ns> cargo test --test promote_golden -- --ignored --nocaptureThe verify tier (opt-in, off by default)
verify_enabled (off by default) runs one more tier per bank, after the semantic tier. It checks extraction fidelity: for each note it reads the note's own span, the lines the note was extracted or saved from, and asks a router::complete judge whether that span contradicts the note. A contradicted note is replaced by a corrected one. The old note retires with a superseded_by pointer and stays visible with include_superseded: true.
The tier catches a note that misstates its own source. It does not catch a note that went out of date later: it never reads a conversation after the span, a span that does not mention the note counts as support, and a reinforced note is skipped.
Transcript. The tier never calls session-manager. It asks memory for the note's source through
memory::transcript, which returns the user, assistant and notice lines of that span, redacted, a long line as its head and tail. An extracted note's span runs from the first to the last entry of its extraction run. A note an agent saved from a turn is read as that turn, plus the user message right before it.Order. Priority notes go first: corrections this tier or the promote tier wrote,
inferrednotes, andstatednotes saved from a turn. Plain extracted notes follow, with what is left ofverify_max_per_run. Pinned notes are never checked.Cursor. Each bank keeps two positions in the state worker (scope
memory_consolidate, keyverify_cursor:{bank}): one for priority notes, one for the rest, each keyed by(updated_at, id). It is saved after each note that cost a call, so a crash never pays for a judge call twice. A note edited since (a tag or entity edit included) is checked again. A cursor that cannot be read or decoded stops the tier with a note.Judge reply. Strict JSON, one of two shapes. Anything else fails to parse.
{"verdict":"supported"} {"verdict":"contradicted","quote":"<verbatim transcript text>","corrected":"<the note rewritten>"}The judge is told that silence means
supported, and that the corrected text changes only what the transcript contradicts.Guards. A correction is written only when all three hold. Otherwise nothing is written and the note counts in
verify_rejected.- Text. The corrected text is not empty, differs from the note after normalization, is at most 200 chars longer than the note, and holds no numeral other than ASCII digits.
- Quote. The normalized quote is at least 8 chars and appears as whole tokens in one user or assistant line. Notice lines and the
role [date]:prefix never count. - Numbers. Every number in the corrected text appears in the message text of a user or assistant line, or in the note itself. A line's date does not count. The note counts because extraction writes resolved dates ("on 2026-10-15" for "tomorrow") that no message holds; a correction may keep them, but the judge cannot add a number of its own.
Normalization is the one duplicates use: lowercase; every char that is not a letter, digit or operator becomes a space, except
-,.,,,:or/between two letters or digits; whitespace collapsed. A number is a whole normalized token with at least one ASCII digit, so "1,500", "1.5", "10:30", "2026-10-15" and "-5" are each one number and none of them vouches for a fragment ("500") or a flipped sign. Number words ("three") are not numbers. The guards are English only.The write.
memory::correcttakesexpected_memory, the note as this pass read it, and refuses a note edited since. It appends the new record and the old record's tombstone in one write. The new record keeps the old note's confidence, tags, entities, source and statement date. Its corroboration starts at 0 and it is not pinned. When the corrected text already exists as a live note, the old note retires into it; when it exists only as a retired note, the correction is skipped, so a note never flips back.Stored reason.
source.correction_reasonis built in code:the source conversation says: "<quote>", with the normalized quote cut to 200 chars. It also rides thememory::on-item-changedevents. The quote stays on the record after its session is deleted.Skipped and counted. A skip counts in
verify_skippedand moves the cursor past the note. A skip is final: the note is not checked again until it is rewritten. These skips cost no judge call:- records written before spans were kept, and notes with no session or span;
- reinforced notes (
corroborationabove 0): later evidence the span does not cover; - edited notes, whose text no longer matches their id;
- a missing session, entry or turn. An extracted span is read along its own branch, so a note from a rewound branch is still checked;
off_pathmeans the span's first entry is not on its last entry's path. An agent-saved note is walked along the session's current path and is skipped asturn_not_foundwhen its turn is not on it; - a source over 24,000 chars, a span over 1,000 user and assistant messages, a turn walk over 3,000 user and assistant messages, or a read that could not finish within 10 s (a read is not started without its full 5 s left).
When
memory::correctrefuses a correction that passed the guards (changed,pinned,edited,no_change,text_exists,invalid_text,flagged), the note also counts inverify_skippedunder that reason and the cursor moves past it, but its judge call was spent and counts inllm_calls.Skips end as one note per bank, for example
verify: skipped 5 (no recorded span 3, session_not_found 2). Nothing goes intoerrors.Caps.
verify_max_per_run(20) counts the notes whose transcript was requested, per bank per pass; skips with no call are free. Judge calls count in the bank'sllm_calls, tokens and cost, and againstmax_llm_calls_per_run. The tier runs last, so when earlier tiers use the cap it isdeferredwith a note. A correction uses themax_supersedes_per_runbudget and counts incorrected, notsuperseded. The model comes fromllm_model.Report. Each bank counts
verified(supported),corrected,verify_skippedandverify_rejected, each left out at zero. The report carries no per-note list and no quotes.Failure handling. A transport error, session-manager down, or a failed cursor save stops the tier for that bank with a note; the cursor stays before the note. A provider error other than a context overflow counts as transport, so a provider that reports a content refusal as an error holds the tier on that note. A context overflow counts in
verify_rejected. An unparseable reply holds the cursor while no reply of the pass has parsed, and two of them with none parsed stop the tier.Older memory worker. A memory worker without
memory::transcriptstops the tier before any model call, with one note:verify tier skipped: memory worker lacks memory::transcript — upgrade memory. Release memory first.Dry runs skip this tier.
Both memory functions are internal, and memory refuses them, like memory::supersede and memory::rule::set, from inside an agent turn: only this worker calls them, from a pass that carries no turn baggage. A memory-consolidate::run started from inside a turn carries that turn's baggage into every memory call it makes, so memory refuses its writes and memory::transcript with forbidden. Each refusal is a bank error (the verify tier stops there, its cursor where it was), so that pass is not checkpointed and the scheduled pass still runs when due.
Verify eval. verify_golden_cases in tests/judge_golden.rs runs the real verify prompt, verdict parser and correction guards over one extracted span and three notes: one the span supports, one it contradicts, and one it never mentions. Each note is judged VERIFY_REPEATS times (default 5). The test prints the correction rate per note and fails if the supported or the unmentioned note is ever corrected, or if any reply gives no verdict (truncated or unparseable), so a run that measured nothing never passes. It has not been run yet, so there is no measured rate.
VERIFY_REPEATS=10 III_NAMESPACE=<ns> cargo test --test judge_golden verify_golden -- --ignored --nocaptureWhat counts as a duplicate
v1 is deterministic and deliberately conservative: two live memories merge only when their normalized text matches (case, punctuation, and whitespace insensitive). Equal token sets (word-order shuffles) are only reported, and merge only when the LLM tier's judge confirms them. One differing word is NOT a duplicate — "always publishes" and "never publishes" must never merge. Survivor choice is stable: pinned first, then a memory that is not an agent's note (confidence stated with a source.turn_id), then highest corroboration, then the oldest record (it carries the original provenance). A pinned agent's note counts as the user's: the pin is the user's own act. Semantic near-duplicate merging is a later, LLM-assisted tier.
Normalization keeps operators and signs (<, <=, !=, +, #, -5) and any -, ., ,, : or / between two letters or digits, so "timeout > 5s" and "timeout < 5s", "C++" and "C#", or "0.05" and "005" never merge.
Boundaries
- Never touches memory's files; every mutation goes through
memory::updatefor an alias retag or a promoted line's key,memory::supersede(withreinforceon merges, so the survivor absorbs the duplicate's observations in the same call; never from an agent's note, which is not the user saying it again, nor from ademoted-ruleline, so its count cannot carry a twin back into the rule), ormemory::correctfor the promote and verify tiers — the same append-only, last-wins contract as everything else. Every change carries areasonon itsmemory::on-item-changedevents. - Pinned memories are never superseded, even inside a duplicate group, and never retagged.
- Every
memory::supersedecarriesexpected_memoryandexpected_superseded_by, both records as this pass read them, so a memory worker that knows these guards refuses a record edited since; the pass reports the error and stays due. An older memory worker ignores the fields. - A memory worker whose
memory::recalldoes not reportvector_coverageis judged as before, with one note per bank:memory worker did not report vector coverage; semantic tier judged without knowing it; upgrade memory. - Every
memory::rule::setoflearnedcarriesexpected, the rule as this pass last read it, so the memory worker refuses it withrule_changedwhen the rule changed since. An older memory worker ignoresexpectedand writes unguarded. - Not extraction (the memory worker captures), not recall, not rule management. One job: keep banks clean.
API reference (JSON)
{
"functions": [
{
"description": "Undo an entity-handle alias merge: every memory the merge retagged that is live, unpinned, still carries the canonical handle and does not already carry `from` gets it back through memory::update, and the pair is recorded as distinct so no pass merges it again.",
"metadata": {},
"name": "memory-consolidate::alias-undo",
"request_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"bank": {
"description": "Bank of the merge.",
"type": "string"
},
"from": {
"description": "The handle the merge retagged away.",
"type": "string"
}
},
"required": [
"bank",
"from"
],
"title": "UndoRequest",
"type": "object"
},
"response_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"bank": {
"type": "string"
},
"from": {
"type": "string"
},
"restored": {
"description": "Memories given `from` back.",
"items": {
"type": "string"
},
"type": "array"
},
"skipped": {
"description": "Recorded memories left alone: gone, retired, pinned, no longer carrying `to`, already carrying `from`, or carrying more than eight handles.",
"items": {
"type": "string"
},
"type": "array"
},
"to": {
"description": "The handle the merge had retagged to.",
"type": "string"
}
},
"required": [
"bank",
"from",
"restored",
"skipped",
"to"
],
"title": "UndoResponse",
"type": "object"
}
},
{
"description": "Internal: hot-reload memory-consolidate from the authoritative configuration when it changes.",
"metadata": {
"internal": true,
"trace_hidden": true
},
"name": "memory-consolidate::on-config-change",
"request_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "The `configuration::on-updated` event, as `memory-consolidate::on-config-change` receives it. Advisory: the handler re-fetches the authoritative value.",
"properties": {
"id": {
"default": null,
"type": [
"string",
"null"
]
}
},
"title": "OnConfigChangeEvent",
"type": "object"
},
"response_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"ok": {
"type": "boolean"
}
},
"required": [
"ok"
],
"title": "OnConfigChangeResponse",
"type": "object"
}
},
{
"description": "Internal: schedule heartbeat (observer of cron::on-tick + boot catch-up backstop). Runs a pass only when interval_hours have elapsed since the last one.",
"metadata": {
"internal": true,
"trace_hidden": true
},
"name": "memory-consolidate::on-tick",
"request_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "The `cron::on-tick` event (all advisory; the handler re-derives due-ness from persisted state). Typed so the published interface carries a real schema.",
"properties": {
"actual_time": {
"default": null,
"type": [
"string",
"null"
]
},
"expression": {
"default": null,
"type": [
"string",
"null"
]
},
"scheduled_time": {
"default": null,
"type": [
"string",
"null"
]
}
},
"title": "TickInput",
"type": "object"
},
"response_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"ran": {
"description": "A pass actually ran (false = not due yet or disabled).",
"type": "boolean"
}
},
"required": [
"ran"
],
"title": "TickResponse",
"type": "object"
}
},
{
"description": "Internal: target of the memory-consolidate REST routes, the http::on-request observers with args { method: POST, path: /memory-consolidate/run | /memory-consolidate/status | /memory-consolidate/alias-undo }. Receives the HttpRequest, runs the function the route's path names with the request's JSON body, and returns the HttpResponse { status_code, body }: 200 with the function's result, or 400/404/500 with { error }.",
"metadata": {
"internal": true
},
"name": "memory-consolidate::rest",
"request_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "The `HttpRequest` fields the adapter reads. The http worker sends more (`method`, `query_params`, `path_params`, `headers` and the stream channels); they are ignored.",
"properties": {
"body": {
"default": null,
"description": "The request body parsed as JSON; `null` when it is empty or not JSON."
},
"path": {
"description": "The matched route's path: `/memory-consolidate/run`, `/memory-consolidate/status` or `/memory-consolidate/alias-undo`.",
"type": "string"
}
},
"required": [
"path"
],
"title": "RestRequest",
"type": "object"
},
"response_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "The `HttpResponse` the http worker answers with.",
"properties": {
"body": true,
"status_code": {
"format": "uint16",
"minimum": 0,
"type": "integer"
}
},
"required": [
"body",
"status_code"
],
"title": "RestResponse",
"type": "object"
}
},
{
"description": "Run one consolidation pass now (all configured banks, or one bank). Deterministic near-duplicate dedup, entity-handle alias merge (a spelling variant retagged through memory::update, a near spelling only once a judge confirms it), plus the optional LLM tiers when enabled (reorder judge; semantic merge/supersede of new memories, each with a reason; promotion of a decision written with a ticket key, rewritten without the key, which moves to its entities; verify of notes against their source transcript, replacing a contradicted one), applied strictly through memory::supersede, memory::update and memory::correct; pinned memories are untouchable. dry_run plans without writing.",
"metadata": {},
"name": "memory-consolidate::run",
"request_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"bank": {
"default": null,
"description": "Consolidate only this bank (must be in the configured allowlist, when one is set).",
"type": [
"string",
"null"
]
},
"dry_run": {
"default": null,
"description": "Override the configured dry_run for this call.",
"type": [
"boolean",
"null"
]
}
},
"title": "RunRequest",
"type": "object"
},
"response_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ActionKind": {
"enum": [
"merge",
"supersede"
],
"type": "string"
},
"AliasAction": {
"description": "One alias pair as reported.",
"properties": {
"applied": {
"description": "Memories were retagged this pass (never on a dry run).",
"type": "boolean"
},
"from": {
"description": "The handle retagged away.",
"type": "string"
},
"ids": {
"description": "Memories retagged; on a dry run, the ones that would be.",
"items": {
"type": "string"
},
"type": "array"
},
"reason": {
"description": "The rule, the judge's reason, or what the pair waits for.",
"type": "string"
},
"similarity": {
"description": "pg_trgm similarity of the two handles.",
"format": "double",
"type": "number"
},
"skipped_pinned": {
"description": "Memories left alone because they are pinned.",
"items": {
"type": "string"
},
"type": "array"
},
"tier": {
"$ref": "#/definitions/Tier"
},
"to": {
"description": "The canonical handle: on more live memories, then the older, then the smaller string.",
"type": "string"
}
},
"required": [
"applied",
"from",
"reason",
"similarity",
"tier",
"to"
],
"type": "object"
},
"BankReport": {
"properties": {
"agent_notes": {
"description": "Scanned notes an agent saved from inside a turn.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"aliases": {
"description": "Entity-handle alias pairs: merged (memories retagged through `memory::update`, each spending the per-run supersede cap), judged distinct, waiting for the judge, or ambiguous. A dry run lists what it would merge, `applied: false`.",
"items": {
"$ref": "#/definitions/AliasAction"
},
"type": "array"
},
"bank": {
"type": "string"
},
"capped": {
"description": "Work left behind by the per-run cap; the next pass picks it up.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"corrected": {
"description": "Notes the verify tier replaced through `memory::correct` (each one also spends the per-run supersede cap; not counted in `superseded`).",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"decisions_promoted": {
"description": "Durable lines the promote tier rewrote without their ticket key through `memory::correct`, the key kept as an entity (each also spends the per-run supersede cap; not counted in `superseded`).",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"decisions_refused": {
"description": "Promote-tier rewrites the check refused (more dropped than the ticket keys and the connector words next to them); never written.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"errors": {
"description": "Real failures (reads, writes). Any error keeps the pass due.",
"items": {
"type": "string"
},
"type": "array"
},
"groups": {
"items": {
"$ref": "#/definitions/PlannedGroup"
},
"type": "array"
},
"live_chars": {
"description": "Characters (not bytes) in the scanned memories' texts.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"llm_calls": {
"description": "Model call attempts by the judge, alias, semantic, promote and verify tiers, failed ones included.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"llm_cost_usd": {
"description": "The router's cost in USD, summed over the calls the router priced; absent when none.",
"format": "double",
"type": [
"number",
"null"
]
},
"llm_input_tokens": {
"description": "Prompt tokens the router reported, cached ones included.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"llm_merged": {
"description": "Reorder groups merged after the LLM judge confirmed equivalence.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"llm_output_tokens": {
"description": "Output tokens the router reported.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"notes": {
"description": "Soft skips (an optional tier unavailable, a sibling too old) — telemetry only: they never block the checkpoint, so an absent router cannot re-run the whole pass on every heartbeat.",
"items": {
"type": "string"
},
"type": "array"
},
"promoted": {
"description": "Standing-instruction lines the judge promoted into the bank's `learned` rule.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"reinforced": {
"description": "Winner reinforcements applied (one per absorbed duplicate).",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"scanned": {
"description": "Live memories scanned.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"semantic": {
"description": "Applied semantic-tier decisions, each with the judge's reason (the tier never runs on dry runs).",
"items": {
"$ref": "#/definitions/SemanticAction"
},
"type": "array"
},
"semantic_failed": {
"description": "New memories the judge could not decide even alone while another reply of the pass parsed (a poison input); skipped past the cursor.",
"items": {
"type": "string"
},
"type": "array"
},
"semantic_rejected": {
"description": "Judge actions dropped by validation (unknown alias, pinned target, wrong direction, a lost number, ...).",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"superseded": {
"description": "Supersedes actually applied (0 on dry runs).",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"ticket_lines": {
"description": "Scanned memories that name a ticket key (`KAN-162`).",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"transient": {
"description": "Scanned memories tagged `transient`.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"verified": {
"description": "Notes the verify tier found supported by their source transcript.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"verify_rejected": {
"description": "Verify-tier judge replies refused: a correction that failed a guard (quote, numbers, length), or a reply cut short or never parsed.",
"format": "uint",
"minimum": 0,
"type": "integer"
},
"verify_skipped": {
"description": "Notes the verify tier passed without a verdict or a write (no span, reinforced, session gone, record changed, ...); a note lists why.",
"format": "uint",
"minimum": 0,
"type": "integer"
}
},
"required": [
"agent_notes",
"bank",
"groups",
"live_chars",
"reinforced",
"scanned",
"superseded",
"ticket_lines",
"transient"
],
"type": "object"
},
"PlannedGroup": {
"description": "One planned merge: `losers` retire in favor of `winner`.",
"properties": {
"loser_ids": {
"items": {
"type": "string"
},
"type": "array"
},
"loser_texts": {
"description": "Texts of the losers, aligned with `loser_ids` (the judge tier and human review need the words, not just ids).",
"items": {
"type": "string"
},
"type": "array"
},
"report_only": {
"description": "True = surfaced for review only (token-set match: same words, different order — potentially different meaning). Never written automatically.",
"type": "boolean"
},
"skipped_pinned": {
"description": "Losers left alone because they are pinned (pinned is untouchable).",
"items": {
"type": "string"
},
"type": "array"
},
"winner_id": {
"type": "string"
},
"winner_text": {
"type": "string"
}
},
"required": [
"loser_ids",
"loser_texts",
"winner_id",
"winner_text"
],
"type": "object"
},
"SemanticAction": {
"description": "One applied semantic-tier decision as reported (and kept in the pass reports). Actions dropped by the pre-write freshness check are not listed; they count in `semantic_rejected` with a note.",
"properties": {
"keep_id": {
"description": "Merge survivor / supersede replacement.",
"type": "string"
},
"keep_text": {
"type": "string"
},
"kind": {
"$ref": "#/definitions/ActionKind"
},
"reason": {
"type": "string"
},
"retire_id": {
"description": "Merge duplicate / supersede outdated record.",
"type": "string"
},
"retire_text": {
"type": "string"
}
},
"required": [
"keep_id",
"keep_text",
"kind",
"reason",
"retire_id",
"retire_text"
],
"type": "object"
},
"Tier": {
"oneOf": [
{
"description": "The same characters, case, whitespace, `-` and `_` aside: merged with no model call.",
"enum": [
"spelling"
],
"type": "string"
},
{
"description": "A near spelling: merged only after the judge confirms it.",
"enum": [
"near"
],
"type": "string"
}
]
}
},
"properties": {
"banks": {
"items": {
"$ref": "#/definitions/BankReport"
},
"type": "array"
},
"checkpointed": {
"description": "The pass completed cleanly AND its last-run checkpoint persisted. False = the schedule will retry the pass next check.",
"type": "boolean"
},
"dry_run": {
"type": "boolean"
},
"superseded": {
"description": "Total supersedes applied across banks this pass.",
"format": "uint",
"minimum": 0,
"type": "integer"
}
},
"required": [
"banks",
"checkpointed",
"dry_run",
"superseded"
],
"title": "RunResponse",
"type": "object"
}
},
{
"description": "Check the consolidation schedule and last pass: enabled, interval, last run, due, and the reports of the last 30 passes (newest first), each with per-bank results and LLM calls, tokens and cost; only the newest lists its groups, alias pairs and semantic actions.",
"metadata": {},
"name": "memory-consolidate::status",
"request_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "StatusRequest",
"type": "object"
},
"response_schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"dry_run": {
"type": "boolean"
},
"due": {
"description": "A scheduled pass is overdue right now.",
"type": "boolean"
},
"enabled": {
"type": "boolean"
},
"interval_hours": {
"format": "uint64",
"minimum": 0,
"type": "integer"
},
"last_run": {
"description": "Milliseconds since epoch of the last completed pass; 0 = never.",
"format": "uint64",
"minimum": 0,
"type": "integer"
},
"reports": {
"description": "The last 30 passes, newest first: `{ ms, dry_run, banks }`. Older passes' banks leave out `groups`, `aliases` and `semantic`.",
"items": true,
"type": "array"
}
},
"required": [
"dry_run",
"due",
"enabled",
"interval_hours",
"last_run",
"reports"
],
"title": "StatusResponse",
"type": "object"
}
}
]
}