Skip to content

Rules a bot states about itself in a conversation become learnings candidates an admin promotes - #92

Merged
zlareb1 merged 102 commits into
mainfrom
feat/assistant-turn-learnings
Oct 21, 2026
Merged

zlareb1 merged 102 commits into
mainfrom
feat/assistant-turn-learnings

Conversation

@zlareb1

@zlareb1 zlareb1 commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

With LOOM_MEMORY_EXTRACT_AGENT_LEARNINGS on (off by default; turn it on once every replica runs this build), a rule a chat bot states about itself becomes a pending learnings candidate instead of a memory. An admin's learnings.review publishes it to the namespace's knowledge for that bot.

Why

memory.set_from_messages files every extracted fact under the request's user. A statement the assistant makes about its own standing behaviour ("I escalate any refund over $500 to a human") was either written as that user's fact or dropped. Writing it as an agent-level memory would be worse: an end user can talk a bot into saying anything, and an agent-level row reaches every user of the bot. The learnings review queue already exists for this kind of decision, but nothing filed candidates into it.

The extraction output already says whose fact each item is (about: user or other). This adds a third value, agent, asked for by a prompt block appended only when the setting is on. No extra model call: the relation backfill skips rules, since a rule is never keyed by a relation or written as a memory.

The setting ships off: an older build does not erase loom.learnings_supporters, so it is turned on only once no older replica serves (docs/architecture/rolling-upgrades.md rule 15), and a later release can flip the default with an entry in server/settings/_history.py. An A/B check on a hosted model (43 conversations) backs turning it on. Before this change, with 2 runs per arm: on, all 10 bot rules were filed for review in both runs; off, about half were stored as the user's memory and the rest dropped. Recall of the 109 user facts was 108 and 107 on, 106 and 105 off; no user fact was filed as a bot rule; input tokens rose by 1.6% and latency did not change. The one reproducible false positive was a request the bot agreed to: the user asked "always answer me in French", the bot agreed, and the rule "The assistant always answers the user in French" was filed. That is the user's preference, so the prompt block now says behaviour the assistant agrees to at the user's request is a fact about the user, never an about="agent" rule. A run of the shipped block (Tests) filed no rule for that conversation. No check after the prompt reads the user's request: a rule the model still returns for one is filed as a pending candidate, and the reviewer rejects it.

  • Routing. loom.bulk_write moves about="agent" facts out of the batch before anything is written. server/memory/app/assistant_learnings.py files them.
  • Turning it on. Only the deployment setting turns it on. A request's agent_learnings=false turns it off for that call. agent_learnings=true while the setting is off is ignored, and named in ignored_args and the X-Loom-Ignored-Args header. The op reads the setting once per call, so what the extraction does and what ignored_args reports agree even when an override lands mid-call; a deferred replay reads it again when it runs. Coding captures never get the block, nor does a request whose messages hold no assistant turn (a user-only call, an observation replay).
  • Candidate identity. The candidate id is a UUIDv5 of (org, namespace, agent, memory_key, normalised rule text). Normalisation folds case and spacing and drops a trailing period, nothing more.
    • A restatement in another session lands on the same candidate, and a restatement after the first filing never changes the text learnings.list shows and learnings.review promotes. Two sessions that first file the same rule at once, differing only in case or a trailing period, leave either wording.
    • A different rule under the same key is a sibling candidate, so a first wording never collects the support of a later one.
    • The same rule worded differently is a sibling too, so wording drift splits support. The fake LLM cannot measure how often a real model drifts, so this PR makes no further change there.
    • While a bot's rule promoted to knowledge is served, nothing new is filed for that bot under that key (learnings_repo.rule_promoted: one count of live loom.documents FINAL rows whose source_uri is learning://<candidate_id> of a candidate by org, namespace, agent and key, run only for a rule with no candidate yet; the queue side reads loom.learnings_queue without FINAL, since every version of a candidate carries the agent, key and source kind its id is derived from). The check reads no status: only a knowledge promotion writes that document, and rejecting a promoted candidate leaves it, so a rule rejected after its promotion is still served and still stops new filings until its document is deleted. Deleting that document with knowledge.delete is the revision path: a new wording under the key is then filed as a new pending candidate; the promoted wording itself stays decided and its restatements are counted under reason decided. A promoted rule is served back to the bot, which restates it in its own words; without the check each restatement would wait for review again. A rule rejected before any promotion is never served back, so it does not stop a different rule under its key; restated word for word it folds to the same candidate, which is skipped as decided and counted in loom.memory_drops (stage learnings.file, reason decided), so a rejected rule that keeps coming back shows there. The skip is counted in loom.memory_drops (stage learnings.file, reason already_promoted). Only a knowledge promotion stops new filings under its key: a rule promoted with destination="memory" leaves no learning://<candidate_id> document, so each restatement of it is filed as a new pending candidate. extraction.md says so.
    • The extractor's key rewrites (an identifier in the value, a relation key) skip rules, so one rule keeps one key across sessions. The rows recovered from the user's turns (a stated preference, cost or age the model left out) are still recovered when the model returns only rules: the rules stand in as the facts those passes read, and are not rewritten.
  • Where text lives. The candidate row holds the rule (proposed_value), the agent, the key, the status and the decision, and no user's words.
    • Each (candidate, user, session) adds one row to the new loom.learnings_supporters table, a plain append-only MergeTree. The row records the principal that wrote it (actor, writer_actor(): the request's token, or in a replay the enqueuer stamped on the queue row), because a token not pinned to one user can send any user_id. It holds that user's own bounded quotes: asked is the user turn before the cited assistant turn, which may be in last_messages (up to 200 characters), and said is the cited evidence span when a cited assistant turn contains it, else that assistant turn (up to 400), so said never quotes a user's words.
    • A rule whose source_message_indices name no assistant turn is not something the assistant said: it is never filed, and the drop is counted in loom.memory_drops (stage learnings.file, reason no_assistant_turn). Without this, a user's own sentence marked about="agent" would reach the reviewer as Assistant: "...".
    • The supporter row is written before the candidate row, once per session unless two filings for the same (user, session) race (an inline capture and a deferred replay, or a client retry); the counts below read such a session once. The candidate row is written again with every new supporter, at version 1, with its first proposal time and confidence_avg 0.0, not judged on the candidate (the supporters' mean is what is read; a decided rule with no supporters left lists 0). Any review's newer version outranks the rewrite, so a decision is never undone.
    • Against an erasure running at the same time, what holds: the erasure's pending delete re-checks for supporter rows when it runs. A candidate a filing backed after the erasure read it is kept, whether the filing's rows landed before the delete or the filing's version-1 rewrite lands after it.
    • A quote that reads as a credential (sensitivity.reads_high, checked on the whole text before it is cut to length) is stored blank, and a rule that reads as one is never filed, its drop counted in loom.memory_drops (stage learnings.file, reason sensitive).
    • Rules are filed before memory.bulk_set, so filing runs the blacklist (Refuse) itself: a rule that matches a pattern for the org or namespace is never filed, and a quote that matches one is stored blank. Quotes are checked whole, before their spacing is folded and they are cut to length, so a pattern the cut would split still matches. Each match counts its pattern's hit and is logged, as memory.bulk_set does for the entries it drops.
    • learnings.review checks the blacklist again before it promotes a rule, to either destination: a pattern added after filing answers 422 BLACKLISTED, as memory.set does, and nothing is written. knowledge.upsert checks no blacklist, so without this the default knowledge destination would publish the rule.
    • Support never promotes anything.
  • Counting. learnings.list counts support at read time, after collapsing the supporter rows to one per (user, session), its earliest.
    • supporter_count is the distinct users, supporter_sessions is the distinct (user, session) pairs, and supporter_writers is the distinct actors that wrote those rows, so a reviewer sees when many users came from one token. The review queue shows "2 users, 3 sessions, 1 writer".
    • supporters lists the first 50 as six-part entries, and confidence_avg is the mean over the supporters.
    • rationale shows the earliest remaining supporter's quotes, both from one row; a tie on time goes to the lowest (user_id, session_id).
    • Other producers keep their stored counts.
  • Promotion. A promoted rule must reach each user's reads. A memory row at user_id='-' does not: memory.search with a user_id filters on it exactly, and a read bounded to one user never serves '-' rows. The existing path that does reach them is the namespace's knowledge. So learnings.review with destination unset (now None) publishes an assistant_turn rule through knowledge.upsert (learning://<candidate_id>, tagged agent:<agent>), and every user's reads of that bot find it: context.pack(include_knowledge=true) (with read_scope=own_user too), memory.stack, loom_search and memory.ask (its knowledge passages and its whole-document route).
    • Only the agent:<agent> tag names the bot, and a rule for agent '-' (every agent in the namespace) gets no agent tag. The document's author is the reviewer's identity, taken from the review's token.
    • The document's source_class is derived, the nearest class a knowledge write may declare, so knowledge.search(source_classes=["asserted"]) does not return a rule a model extracted from users' conversations.
    • Those four agent reads leave out a promoted rule tagged for another agent (assistant_rules.serves_agent: a document tagged source-kind:assistant_turn and agent:<x> serves only agent x and a namespace-wide read). Over HTTP, a read under agent *, memory.ask's default, spans every agent as memory reads do and is served every bot's rules; a read under no agent (-, loom_search's default) is served no rule promoted for a named bot, so a bot names its agent there; a rule for every agent is still served. Over MCP an unnamed agent is the client's, so those reads serve that agent's rules; loom_search now passes its agent to the fan-out, and the recorded MCP answers in tests/unit/fixtures/loom_search_mcp_answers.json carry it. A memory.ask precedent's shape includes the agent whose rules the read is served when a pinned token makes it differ from the body's agent, so two bots' tokens for one user never share a precedent. Each asks knowledge for its top_k as before (memory.ask's whole-document route for its 20). Only when that first pass held another bot's rule and its top_k is below knowledge.search's cap does it ask again (a pass short of top_k asks too: with collapse_sections on, knowledge folds sibling chunks before it cuts to top_k, so a short pass can still leave hits out) for four times its top_k (at most that cap, MAX_SEARCH_TOP_K, which its top_k bound also reads), drop those rules and keep top_k, so another bot's rules ranked first do not empty its knowledge section. If that second search fails, the first pass's served hits answer and a warning is logged; one helper (agent_rules.served_hits, beside request_rule_agent in server/memory/app/agent_rules.py) holds this for all four reads. A read whose first pass holds no rule promoted for another agent runs the same one knowledge query as before; the second, wider query runs only when such a rule was dropped, whether or not the setting is on now (a rule promoted while it was on keeps its tags, and any namespace writer can upsert a document with them). No system.query_log comparison was taken. The candidate belongs to one bot, so a promotion stays that bot's decision. Other documents, agent: tags on them included, are served as before. knowledge.search is not filtered by agent; a bot finds its own rules with knowledge.search(tags=["agent:<agent>"]).
    • Other producers keep memory as their default, and the Python SDK's review leaves destination unset unless given.
    • destination="memory" still writes the agent's memory row through memory.set (agent=<candidate agent>, user_id='-', session_id='-'). Namespace-wide reads find that row; reads bounded to one user do not.
    • The promotion uses the supporters' mean confidence.
    • A bot-rule candidate with no supporter left (its rule blanked once decided, or still pending mid-erasure) answers 404, as a missing candidate does.
    • On the memory path, source_trace is learning:<candidate_id>, and a new trace prefix maps learning: to source_class extracted. The approver is the row's actor, and memory.lineage reports that actor as promoted_by, with supporter_count from the supporter rows.
    • Other producers keep their current promotion path.
  • Pinned tokens. For a token pinned to one end user (read_user_id), the candidate's agent is the token's agent_principal, never the body's agent. A pinned token that names no agent files nothing. Any other token, an own_user token without a pin included, files for the body's agent.
    • The four agent reads resolve their agent the same way (agent_rules.request_rule_agent): a pinned token is served its signed agent's rules whatever agent the body sends, and no rule promoted for a named bot when it names no agent (a rule for every agent is still served).
    • A token Loom mints (loom mint-token --read-user-id) carries no agent claim, so it names no agent: it files no bot's rule and is served no rule promoted for a named bot. A bot that needs its own promoted rules for one user reads with an end-user token its issuer signed for that agent, or with an unpinned token. This PR does not add an agent claim to Loom-minted tokens: agent_principal also carries the agent's collection grants, so minting it is an authorization change of its own.
    • The handler takes the resolved rule agent as a parameter (rule_agent).
    • A deferred request stores it in the queue payload (key rule_agent, stripped before the replay validates the request). The handler is the only caller of extraction_queue.enqueue / payload_for that stores one; any other enqueue stores none and its replay files nothing.
    • A payload without it replays with '', which files nothing.
  • Quality rollup. learnings.quality keeps counting the conversation seeder by default, because it reports extraction quality. source_kind="assistant_turn" reads the bot rules.
  • Dashboard.
    • A decided bot rule whose last supporter was erased shows "Rule removed by a user erasure; the decision stands."
    • The Learnings tab shows each candidate's producer in words ("A bot's rule about itself", "Seen in conversations", "Backed by several writers"; an unknown kind shows its name).
    • A bot rule shows its agent, or "every agent in this namespace" when it has none, and its users and sessions.
    • Under a pending bot rule, one line says what Promote does: it publishes the rule to the namespace's knowledge for the named bot and every user, and reads under agent * and knowledge.search also find it; or for every agent, for a rule with none.
    • The rationale quotes the user turn, then what the assistant said, one per line.
    • The empty state says what a learning is.

Scope and risk

  • Off by default. With the setting on, the block changes the extraction prompt; one run of the shipped block is in Tests. No benchmark from bench/ was run.
  • Migration 064_learnings_agent_and_supporters.sql adds agent to loom.learnings_queue and creates loom.learnings_supporters (MergeTree, ORDER BY (org, namespace, candidate_id, user_id, session_id)). A supporter row carries the whole identity tuple and the writing actor: its agent is the candidate's, the bot the rule was said to, so a row read on its own (an erasure audit, a raw-row check) names its bot without a join on a candidate that may already be deleted. It is additive and takes the next free number on main (063 is memory_webhooks).
  • Erasure.
    • A user erasure (learnings_queue.delete in the user saga) deletes that user's supporter rows, and their quotes with them. No candidate row holds a user's words.
    • It then handles every assistant_turn candidate that has no supporter left but still holds its rule. Each delete is bounded to what the step read, so a review landing after the read survives (holding the rule), the census counts it, and the reopened step blanks it. A pending candidate loses only its pending rows, and only while no supporter row names it. A decided one loses only its rows that hold the rule at a version no newer than the one it read for that candidate.
    • A decided one keeps its status row with the rule blanked, so the decision stands and a rejected rule never comes back as pending. The blank row is written before the rows that hold the rule are deleted, so a step that fails between the two is finished by a retry.
    • The census counts the user's supporter rows and any such unsupported candidate, and a namespace erasure deletes both tables. The unsupported count is org-wide, since the erased user's supporter rows are gone by then. So a saga can fail on residue that another erasure in the org left mid-step, and its reopened step then cleans it. This fails closed, never open, and is documented in the predicate and retention-and-erasure.md. Restricting it to the erased user's candidates would mean carrying their ids through the saga's checkpoint, and this PR does not do that.
    • A promoted rule is the admin's write and is not removed by an end user's erasure.
  • Identity. The candidate belongs to the agent. The end user appears only on their own supporter rows.
  • Failure. A queue write that fails is named in pipeline_errors (learnings_queue_failed:<key>) and the rule is dropped. It is never written as a memory.
  • Transport. arg_check.attach keeps an op's own ignored_args beside the transport's unknown keys instead of overwriting them, and X-Loom-Ignored-Args lists them too. Only memory.set_from_messages returns them through this path today.
  • The fake LLM provider reads an assistant's "I always ..." / "I never ..." sentence as a rule when the extraction prompt carries the block. tests/integration/test_assistant_turn_learnings.py turns the setting on through a runtime override (admin/settings.set) and clears it after; while it is on, other integration tests run with it on too, and no other integration test's fake-LLM assistant turn opens a sentence with "I always" or "I never", so none files a rule.

Compatibility, rollout and rollback

  • Wire:
    • memory.set_from_messages gains agent_learnings: bool | None (hidden from the MCP schema; it can only turn filing off). Its response gains learning_candidates: int (rules that wrote a new candidate or this session's support of one) and ignored_args: list[str].
    • learnings.list rows gain agent, supporter_sessions and supporter_writers.
    • learnings.review.destination defaults to unset (was 'memory'): unset promotes an assistant_turn rule to knowledge and every other producer to memory, as before; the Python SDK's review() no longer sends destination unless given.
    • A Python SDK before this release sends destination="memory" from review(); upgrade it, or pass destination="knowledge", before promoting assistant_turn rules.
  • Operator steps:
    • New setting LOOM_MEMORY_EXTRACT_AGENT_LEARNINGS (default false), declared in the settings catalog and live: a runtime override takes effect on the next extraction. Run migration 064 before the new build serves; N-1 tolerates the column and the table.
    • Turn the setting on (loom settings set memory_extract_agent_learnings true) only once every replica runs this build. An N-1 erasure saga does not erase loom.learnings_supporters, the table where users' quotes live. docs/architecture/rolling-upgrades.md (rule 15) and docs/operations/upgrades.md name this step and the rollback step.
    • Review assistant_turn candidates only after the rollout completes. Until then an N-1 replica can serve the review, and it promotes the rule as the approver's own memory (agent='-', user_id=<approver>), because the older build defaults to memory and takes its seeder path.
  • N-1 during a rollout:
    • An N-1 replica lists assistant_turn candidates with the stored count (0) and no rationale.
    • An N-1 replica that promotes an assistant_turn candidate takes its seeder path. The rule is written as the approver's own memory (agent='-', user_id=<approver>), not to knowledge, and the status row it writes has no agent.
    • An N-1 worker replays a deferred request without assistant-turn learnings. Pydantic ignores the unknown field.
  • Precedents: a memory.ask under a read_user_id-pinned token gets a new precedent key after the upgrade, a one-time cache miss, even with the setting off; N-1 and N do not share those keys during the rollout.
  • Rollback: first turn the setting off and delete the rows of loom.learnings_supporters (ALTER TABLE loom.learnings_supporters DELETE WHERE 1, or per org), because an older build's erasure does not cover that table and its rows hold users' quotes. Then delete the assistant_turn candidates (ALTER TABLE loom.learnings_queue DELETE WHERE source_kind = 'assistant_turn'): an older build lists a pending one and, on promote, takes its seeder path, writing the bot's rule as the approver's own memory (agent='-', user_id=<approver>). The agent column and the emptied tables stay. The older build has no per-agent filter on knowledge reads, so it serves a promoted bot rule to every agent in the namespace; to prevent that, delete the documents tagged source-kind:assistant_turn with knowledge.delete before rolling back, or accept the wider serving. Both docs pages say so.

Tests

  • Unit: tests/unit/test_assistant_learnings.py covers:

    • parsing about, and the prompt block on and off in both prompt sizes;
    • the switch off: the fact is the user's before the settle passes, so it is re-keyed and graded like any user fact;
    • only the setting turning learnings on, a request turning them off, the coding-capture gate, no agent to file for, and no assistant turn (user-only and observation messages);
    • one candidate per agent, key and text (case, spacing and a trailing period folded), with sibling candidates for different text, and the first wording kept through a restatement's list and promotion;
    • a rule keeping its key where a user fact is re-keyed;
    • a supporter's quotes (the user turn, then the evidence first, both bounded) on its own row, and a candidate row with no quote;
    • one filing row and one supporter row per session, the supporter first, and a repeat session left as it was;
    • a supporter racing a review keeping the decision, a decided candidate taking no supporters, a promoted rule reworded under its key not filed again (another bot's still is), a revised rule filed as a new pending candidate once the promoted rule's document is deleted (and rule_promoted on clickhouse-local counting a candidate, promoted or rejected after its promotion, only while its document is live), a promoted rule rejected later still stopping a reworded restatement, and a rejected rule not stopping a different rule under its key;
    • the learning agent for each token shape (end-user token with and without agent, a read_user_id token, an unpinned token), a request unable to turn filing on (both setting values), and a replay filing for the stored agent or for none;
    • changed: test_ask_shape_hashes_the_identity_the_token_pins in tests/unit/test_memory_ask_precedents.py now computes the body naming alice under alice's token; an unpinned body naming alice is served every bot's rules and the pinned token none, so it no longer shares the pinned shape;
    • the transport keeping an op's ignored_args in the body and in X-Loom-Ignored-Args;
    • loom.bulk_write never passing a rule to memory.bulk_set;
    • learnings.list counting users, sessions and writers, and showing the earliest supporter's quotes;
    • a supporter row naming the principal that wrote it;
    • supporter_stats on clickhouse-local counting a session with two rows once (count and mean), counting one writer behind two users, and taking both quotes from one supporter when two start at the same millisecond;
    • promotion through learnings.review writing (agent, '-', '-') with learning:<id> and the supporters' mean confidence, a knowledge promotion naming the bot only in its tag, with no supporter data on the status row, and a blanked rule answering 404;
    • a credential in a quote stored blank, a credential rule never filed, a blacklisted rule never filed, a blacklisted quote stored blank (also when the pattern straddles the 200-character cut), a pattern added after filing refusing the knowledge promotion, a failed wider knowledge search answering the first pass's served hits, and a candidate an erasure removed mid-filing written again;
    • the supporter column set held to the table;
    • a promoted rule tagged for one bot serving only that bot and a namespace-wide read (*), and a rule for every agent or any other document serving every agent;
    • memory.ask with an unpinned token and the default agent serving support-bot's rule in its knowledge passages and whole-document route, one knowledge query each;
    • hits at MAX_SEARCH_TOP_K with another bot's rule among them answering without a second query;
    • three rules for another bot ranked above one plain document, with the document still packed;
    • for context.pack, memory.stack, loom_search, and memory.ask's knowledge passages and whole-document route, a token signed for support-bot whose body names sales-bot: three sales-bot rules ranked first are dropped, support-bot's own rule and a plain document fill top_k, and the wider knowledge query runs only after the first pass dropped a rule;
    • a rule citing only a user turn filed nowhere and counted as a drop; said falling back to the cited assistant turn when the evidence is not in it; asked quoting the user turn before the earliest cited assistant turn, and a user turn from last_messages;
    • a rule with two entities and no relation, beside a user fact that has one, making no relation backfill call;
    • a model answer with only a rule, over a user turn whose stated preference it left out, still writing the recovered user_preference_* row.

    The erasure unit tests run the learnings user step on clickhouse-local, with both tables created from schemas/schema.sql and the step's own statements executed, and assert on the rows read back. They cover: the user step deleting only the user's supporter rows; an unsupported pending candidate losing its pending rows while an unsupported rejected one keeps a blank-rule decision written before its other rows go; a review landing between the read and the delete surviving the first run and being blanked when the step reopens; a review landing after the blank write surviving the decided delete; a late review on the older of two decided candidates surviving, since each is bounded by its own read version; and a candidate a filing backed after the read surviving the pending delete. They also cover the step traces, the census in both scopes and the classification.

  • Integration, against a private server and ClickHouse on the fake LLM with the setting turned on by a runtime override (tests/integration/test_assistant_turn_learnings.py, distinct user turns per user):

    • A rule is pending in learnings.list and absent from memory.search. Restating it in the same session changes nothing; another session and another user give 2 users and 3 sessions with the first user's quote. Promotion publishes it to knowledge. context.pack(include_knowledge) finds it for alice (read_scope=own_user) and for another user, knowledge.search with agent:support-bot finds it, and with agent:other-bot finds nothing, and context.pack for other-bot does not carry it. A supporter after the decision does not reopen it.
    • A token minted with read_user_id (no agent) is not served the bot's promoted rule that the admin token's read finds, and its memory.set_from_messages files no candidate.
    • A different rule under the same key is its own candidate, quoting its own user.
    • Erasing the first supporter removes their quote, the next supporter's quote shows, and no row holds the erased user's words. Erasing the last supporter deletes the pending candidate. Raw row counts are 0 in both tables.
    • A candidate whose insert failed after its supporter row is filed by the next user. Erasing each user in turn leaves the right quote, then nothing.
    • Erasing the only supporter of a rejected candidate, after OPTIMIZE TABLE loom.learnings_queue FINAL, keeps the rejection with the rule blanked. The same rule stated again does not come back as pending.
    • Two sessions interleaved on the real ClickHouse: alice's erasure step reads her candidate as unsupported, then bob's session files the same rule before the step's delete runs. The candidate stays pending with bob's quote, and the census is clean. With the supporter re-check removed, this test fails.
    • With coverage_sweep=true and several statements the first pass cites nowhere, the rule is filed once and none of the written memories holds it.
    • A full user erasure saga (begin_erasure + run_saga): the learnings step runs after the memory steps and before verify.residue, is checkpointed, and the census is clean. With a stray unsupported candidate left behind, a resumed run's real verify.residue fails the saga with {"loom.learnings_queue": 1}. The recovery run given that census reruns the checkpointed learnings step (other steps stay skipped) and completes clean.

    The branch is merged with the latest main, including the settings catalog and its ratchet tests. The learnings erasure step's clickhouse-local race tests are in tests/unit/test_erasure_learnings_step.py.

  • A/B on a hosted model, one run of the shipped prompt block with the setting on over the same 43 conversations: 10 of 10 bot rules filed for review and none stored as a memory; 1 false rule, an assistant's persona line in a casual chat ("Assistant is a big video game fan ...") filed as a candidate, which review rejects; no rule for the "always answer me in French" conversation, whose preference was stored as the user's memory; user-fact recall 107 of 109 (the two misses are a wedding and a city, not rules); no user fact filed as a bot rule; 578,204 input and 10,955 output tokens for the run.

  • Measured with system.query_log on ClickHouse 26.9 (one replica, one part per insert; rows, bytes and memory are the most of the runs, duration the median). Seed: org acme, namespace ns with 60,000 queue rows (50,000 candidates, a second version for every fifth one, 60% assistant_turn over 20 agents and 2,000 keys), 500,000 queue rows in another namespace, 60,000 loom.documents rows; for supporter_stats, a page of 100 candidates.

    Statement read_rows read_bytes memory duration
    rule_promoted, queue read with FINAL (before) 105,264 3.99 MiB 6.39 MiB 11 ms
    rule_promoted, queue read without FINAL (this head) 68,880 2.10 MiB 5.22 MiB 8 ms
    supporter_stats, 20,000 supporters spread over the 100 candidates 20,100 12.49 MiB 29.43 MiB 36 ms
    supporter_stats, one candidate with 20,000 supporters, 99 with 50 25,050 15.57 MiB 48.79 MiB 44 to 64 ms over three batches

    rule_promoted still reads the namespace's queue rows (60,000 rows in 5 ms for the subquery alone): agent and key are not in the queue's sort key, and a skip index on memory_key would need a migration and a mutation to cover existing parts, which this read does not justify. In supporter_stats most of the time goes to carrying each supporter's quotes through the per-session grouping; taking the quotes in a separate join on the earliest row cut the skewed case to 37 ms but not the spread one (38 ms), so the statement is unchanged. Both stay under learnings.list's 80 ms budget at 20,000 supporters on one page. The command: seed the three tables with INSERT ... SELECT FROM numbers(...), run each statement with a log_comment, SYSTEM FLUSH LOGS, then SELECT log_comment, max(read_rows), formatReadableSize(max(read_bytes)), formatReadableSize(max(memory_usage)), median(query_duration_ms) FROM system.query_log WHERE type = 'QueryFinish' AND log_comment LIKE 'measure_%' GROUP BY log_comment.

  • make format and make lint are clean on this head, and the settings, schema and reference generators report their files current. schemas/schema.sql matches a dump of a ClickHouse migrated from empty through 064 (the only difference is the dumping user's name in DEFINER), and tests/analyzer/shapes.txt gains the shapes the corpus renders: the changed loom.learnings_queue list and find statements, the erasure counts and deletes on loom.learnings_supporters, and the check for candidates whose supporters were erased; make lint checks it current. The rule-promoted check and the supporter count and existence lookups are not in the corpus; their clickhouse-local unit tests check them. The unit suite (with the settings ratchet tests and the census counts), the integration suite and the mixed-version run have no result on this head yet: CI's pytest unit + coverage, pytest integration and builds N-1 and N on one ClickHouse jobs are the record, and the checklist box below stays unchecked until they pass.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. It checked the extraction output shape, the queue's identity and dedup, where user text lives and its erasure, the N-1 shapes and the promotion identity against the code and the tests above.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py). schemas/schema.sql is the dump of a ClickHouse migrated from empty through 064 and make schema-check passes against it.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 30 commits October 20, 2026 15:58
…ndidates an admin promotes

With agent_learnings on (request flag or LOOM_MEMORY_EXTRACT_AGENT_LEARNINGS,
default off), the extraction call also returns the assistant's standing rules
about its own behaviour as about="agent" facts. They are never written as
memories: each becomes a pending assistant_turn candidate in
loom.learnings_queue for (org, namespace, agent), keyed by agent and memory
key, with the end user and session as a supporter and a rationale quoting the
assistant turn. A restatement in another session adds a supporter. Promotion
through learnings.review writes the rule as the agent's memory (user_id '-').

Migration 062 adds learnings_queue.agent and supporter_users; a user erasure
deletes every version of the candidates that user backs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A candidate is keyed by (agent, memory key, normalised rule text), so a
different rule under one key is a sibling, never a supporter of the first
wording. The candidate row is written once at filing (version 1, below any
review); each session's support is a row in the new
loom.learnings_supporters table, counted by learnings.list as distinct users
and sessions. A rule keeps the extractor's key through the settle passes.

A token bound to one end user files for its signed agent and cannot turn
filing on (the request field is dropped and named in ignored_args); the pin
rides deferred payloads. Promotion's source_trace is learning:<candidate_id>.
A user erasure deletes the user's supporter rows and every candidate the user
filed with its supporters. The dashboard shows agent, source kind, users and
sessions, and what promoting a bot rule does. The migration is renumbered to
059; the setting is documented as experimental.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The migration is renumbered to 060 after main's 059.

Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A user erasure finds the candidates a user filed from the earliest
loom.learnings_supporters row (argMin(user_id, created_at), millisecond
created_at), so it no longer depends on a queue column surviving a merge;
filed_by_user is gone. A supporter row is written once, before the
candidate row, and candidates are deleted before the supporter rows that
name their filer.

A token bound to one end user that names no agent files nothing. With the
switch off, an about="agent" fact is the user's before the settle passes.
Promotion uses the supporters' mean confidence and a single branch for its
identity and trace. The supporter list uses groupArraySorted, the queue
payload stores only the resolved learning agent, and X-Loom-Ignored-Args
carries an op's own ignored fields. The dashboard labels source kinds, says
"every agent in this namespace" for a rule with no agent, and shows the
rationale's user turn before the rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The analyzer statement corpus is regenerated over both sides.

Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The candidate row holds the rule, agent, key, status and decision and no
user's words. Each loom.learnings_supporters row (now a plain MergeTree)
stores its own user's bounded quotes, and learnings.list shows the earliest
remaining supporter's. A user erasure deletes that user's supporter rows; a
bot-rule candidate left with none is deleted when pending, and keeps its
decision with the rule blanked when decided. The filer concept is gone.

Only LOOM_MEMORY_EXTRACT_AGENT_LEARNINGS turns filing on; a request can only
turn it off, and agent_learnings=true while the setting is off is named in
ignored_args. The handler takes the learning agent as a parameter, and a
replay with none stored files nothing. supporter_users is dropped from the
wire. CI's integration job runs with the setting on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
learnings.review answers 404 for an assistant_turn candidate an erasure
blanked. The user erasure step deletes only pending rows of an unsupported
pending candidate, so a review landing in between survives and the reopened
step blanks it. Filing writes the candidate again when an erasure removed it
between the read and this session's supporter row. A quote that reads as a
credential is stored blank, and a rule that reads as one is never filed.
learning_agent is renamed rule_agent (the stored payload key stays), and the
docs say how to roll back.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The saga runs the learnings step among the user steps, checkpoints it, and
reruns it from learnings residue on resume. The test's fake access key is
built at runtime so the secrets gate does not read it as leaked, and the
learnings reference is regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…supporter

The decided delete of an unsupported bot-rule candidate is bounded to the
newest version the step read, so a review landing after the blank write
survives for the reopened step to blank. Filing writes the version-1
candidate row with every new supporter, keeping its proposal time, instead
of re-reading it. The dashboard says when a rule went with a user erasure
and its decision stands. The payload key is rule_agent, __all__ is sorted,
and the saga test resumes from a real verify.residue census.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The pending delete of an unsupported bot-rule candidate re-checks for
supporter rows when it runs, so a filing that landed after the step read the
candidate keeps it. learning_candidates counts only rules that wrote a row,
the candidate row's confidence_avg is a fixed 1.0 (the supporters' mean is
read), and the docs say how the version-1 rewrite and the org-wide count of
unsupported candidates behave. A two-session test interleaves a filing with
the erasure step on a real ClickHouse.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The candidate row stores confidence_avg 0.0 (learnings.list and promotion
read the supporters' mean); a decided rule with no supporters left lists 0
users and 0 confidence. Filing names new_supporter and judges the
supporter's confidence inline. Predicate comments and the erasure doc read
more plainly, and the test helpers pass the supported set through.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
quotes() reads the whole user turn and rule span with reads_high before
bounding them, so a key or token the cut would split still blanks the
quote. learnings.review answers 404 for a bot-rule candidate with no
supporter left, pending or blanked. Small cleanups: ignored for the dropped
request flag, assistant_rules.SOURCE_KIND without an alias, a required
result in header_value, and the docs' learning_candidates wording.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The extractor is not asked for the assistant's rules when the messages hold
no assistant turn (a user-only call, an observation replay). The dashboard
shows a bot rule's agent line and Promote warning only when the server sends
an agent. The ignored-args body and header share one merge, the setting and
predicate comments are shorter, and an integration case checks the coverage
sweep does not write the rule back as a user fact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The migration is renumbered to 061 after main's 060.

Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A memory row at user_id '-' never reaches a read bounded to one user, so
learnings.review now defaults an assistant_turn rule to the namespace's
knowledge (learning://<id>, authored by and tagged agent:<agent>), which
context.pack, knowledge.search and loom_search serve to every user;
destination is None unless given, memory stays the default for other
producers, and the SDK leaves it unset. memory.lineage names the approver
from the row's actor and counts a rule's supporters from their rows, review
reads supporter_stats directly, and the MCP descriptions name both kinds of
learning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… row

`destination` leads with its default so the generated reference keeps it,
`promoted_to_shared` and the promotion docstrings state that the row is the
agent's namespace-wide row that reads bounded to one user do not serve, and
the feature page says lineage finds a memory promotion by its candidate id.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ed as not settings

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The settings catalog from main now declares memory_extract_agent_learnings,
live, with its default pinned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e bot

A rule the assistant states about itself is filed before memory.bulk_set,
so it skipped the blacklist. Filing now refuses a rule that matches a
pattern for the org or namespace, and stores a matching quote blank, the
way a quote that reads as a credential is.

A rule promoted to knowledge no longer passes the candidate's agent as the
document's author: the review token decides the author, and only the
agent:<agent> tag names the bot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The agent '-' names no bot, so promoting its rule to knowledge no longer
tags it `agent:-`. The learnings.list description says promoting a
candidate writes it to shared, and the review's `destination` comment
reads as one sentence with its default.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… override

CI's integration job no longer sets LOOM_MEMORY_EXTRACT_AGENT_LEARNINGS for
every test. The test module installs a deployment override of
memory_extract_agent_learnings with a platform-admin token, waits until the
listing shows it in force, and clears it on teardown, so every other test runs
with the shipping default and the module no longer skips on a default server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
handle_memory_set_from_messages settles whether learnings are on from one
read of memory_extract_agent_learnings and the request, reports the ignored
argument from that read, and passes the result into the workflow state, so
an override landing mid-call cannot make ignored_args contradict what the
extraction did. The integration module builds on control_plane_url, waits for
the override with eventually and authenticates through LOOM_API_KEY, as the
settings override test does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
context.pack, memory.stack and loom_search leave out a knowledge document
promoted from an assistant_turn candidate when its agent tag names another
agent; a rule for every agent and every other document serve every agent.
The dashboard's Promote note names the agents a rule reaches. The rollback
steps delete the assistant_turn candidates an older build would promote as
the approver's memory, the reviews page names the 404 for an erased bot
rule, and the learnings.review description and destination comment name
the conversation-seeded case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…pport counts one row per session

- The decided-candidate delete in the learnings user step matches each
  candidate against the version read for it, so a late review on a candidate
  read at an older version than another survives.
- supporter_stats collapses to one row per (candidate, user, session) first:
  a session two racing filings both wrote counts once in the mean confidence
  and the supporter list, and the quotes are one row's pair with a
  deterministic tie-break.
- The Promote note names the reads that leave out another bot's rule, and
  says knowledge.search finds it for anyone who can read the namespace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 25 commits October 21, 2026 09:21
The A/B numbers predate the agreed-request prompt clause and the
user_requested filter, so the page no longer states them; the PR body
keeps the measurement.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e as user-requested

"you" before always, never or don't opens an imperative only after can, could,
would or will ("Could you always ..."); "You never told me you escalate
refunds over $500." states a fact, so the bot's rule is still filed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Main's memory webhooks take migration 063, so this branch's migration is
now 064_learnings_agent_and_supporters.sql, and its rolling-upgrade rule is
rule 15 after main's rule 14.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…the rest of the page

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r its status, and the user-request filter reads clauses, "do not" and "want you to"

rule_promoted no longer requires status 'promoted': only a knowledge
promotion writes the learning:// document and a later reject leaves it,
so the rule is still served and its restatements are not filed again.

user_requested also reads an ask word that opens a clause after a comma
or semicolon, folds "do not" into "don't" and treats "want/need you to"
as an imperative opener. Its docstring keeps only what it adds; the
opener and question rules stay in their helpers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…s", and a second review of a promoted rule is documented as rejecting it

A clause that opens with never or don't usually leaves out its subject ("I called
twice, never got my refund"), so it no longer drops the bot's rule as user_requested.
The rule_promoted comment and the review docs say that rejecting a promoted rule
leaves its document served.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…r, so "Will you always escalate refunds?" still files the bot's rule

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…stion asks for a rule only when it names the user as "me" or "us"

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ger erasure steps no longer claim to sit outside the census

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… in two lines, and the English-only request filter is documented

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ing filed

The user_requested filter and the asked quote read the user turn before the
first cited assistant turn over last_messages and messages together, so a
request sent in last_messages with the agreement in messages is dropped as
user_requested instead of filed as the bot's rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…led, and each supporter row names its writer

user_requested drops a rule only when the request asks for the behaviour to
happen or not as the rule says, with "only" and "anything but" read as
positive. A supporter row stores the writing actor, and learnings.list
reports the distinct writers beside users and sessions, so many user_ids
sent by one token read as one source. The unrelated docstring edit on
_extract_request_pins is dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…earnings

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…t and two doc pages

Split the user_requested grammar into a list and say the rollback has no
migration to undo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…a decided rule's restatement is counted as a drop

The recovery passes read the rules as the session's facts when the model
returned no user fact, so a preference, cost or age left out of the user's
turns is still written. Filing counts a restatement of a decided candidate
under reason `decided`, and the cited indices into the merged context get
their own name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ore its earliest cited turn

Citation repair scores only user turns, so on a rule it could only move the
citation onto a user turn and the rule was then dropped as citing no
assistant turn. Rules now skip it. The agreed-request check and the
supporter's user quote read the user turn before the earliest cited
assistant turn, whatever order the model cited them in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…the judge verdict is parsed once per fact

Every version of an assistant_turn candidate carries the agent, key and source
kind its id is derived from, so the IN subquery needs no FINAL. judge_verdict()
parses the verdict and judged_confidence() takes it, so source_trace and the
stored confidence read the same value. The assistant_rules docstring names the
citation helpers and the agreed-request filter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…usion no longer files an agreed rule

"Please always answer me in French, not English." read as negative because
of the later "not", so the agreed rule was filed as a bot-wide rule. The
request's polarity is now read from the ask word that opens it, and the
rule's from the word after "assistant". "replies" and "reply" now share a
stem.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e setting states its rollout constraint

The user_requested filter is removed: its English parsing had no measurement
behind it, and the prompt clause plus the reviewer cover the same case. The
memory_extract_agent_learnings description now says to turn it on only once
every replica's build erases loom.learnings_supporters.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ling, and the docs state the racing first filings

Two sessions that first file one rule at once, differing only in case or a
trailing period, may leave either wording; the docs and the comment say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ing paragraph

The supporter-first rewrite sentence now comes straight before the erasure
claim it supports, and the wording rule follows it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant