Copy rules
A copy rule is a namespace's saved criteria for copying memories into it from another namespace of the same org.
A copy rule is a namespace’s saved criteria for copying memories into it from another namespace of the same org. It names the source namespace, typed conditions, the person who owns it and whether it is enabled. This page describes what a rule stores, who may save, list, delete, preview and run one, how a rule is evaluated, what a run writes, the table, erasure and the rolling-upgrade gate.
A stored rule copies nothing: it is configuration. One operation evaluates a rule, memory.copy_rule.run. By default it answers what the rule would propose without writing a memory. With dry_run: false the rule’s owner runs it, and each new match becomes a proposal in the destination that another administrator decides.
Where a rule lives
A rule belongs to the namespace it writes into, the destination. The destination’s role checks govern it: the transport authorizes (org, namespace) for each operation, a viewer of the destination reads what feeds it, and its administrators delete a rule they do not want.
The source is named by from_namespace and is in the same org: the operations take one org field. A rule keeps its source for its whole life. A save that names another source is refused with 409 PRECONDITION_FAILED, and the owner deletes the rule and saves another.
Neither end is a collection, a personal collection, or the namespace of a document mirror or a code store, and a rule never reads the namespace it writes into. Each of those is a 422 VALIDATION_FAILED.
Conditions
Every condition a rule states must hold for a memory to be selected.
| Condition | Selects |
|---|---|
memory_type |
Memories of that type. profile and definition are refused. |
key_prefix |
Memories whose key starts with the prefix, at most 256 characters. |
tags_all |
Memories carrying every tag listed, at most 50. |
tags_any |
Memories carrying at least one tag listed, at most 50. |
pinned |
Pinned memories. Only true is accepted. |
verified |
Verified memories. Only true is accepted. |
updated_within_days |
Memories updated within that many days, 1 to 3650. It narrows a rule and does not make one by itself. |
agent |
Memories one agent label of the source wrote. It narrows a rule and does not make one by itself. |
A rule states at least one of the first six, so it never selects a whole namespace or everything one agent wrote. memory_type, key_prefix, tags_all and tags_any have the meaning of the same fields of a memory.apply filter.
An unknown condition is refused. The conditions are stored as JSON with only the stated fields, the tags sorted and without repeats, and the keys in order, so two bodies that select the same memories store the same text.
A rule whose stored conditions this build cannot parse, such as one that a later build saved with a condition this build does not know, is listed with readable: false and its conditions as stored. This build treats it as selecting nothing: reading the conditions it does know would select more than the owner saved.
Operations
| Operation | Role | Does |
|---|---|---|
memory.copy_rule.set |
writer | Stores a rule, or replaces the caller’s own. The whole rule is sent: enabled left out is false. |
memory.copy_rule.list |
viewer | Lists the namespace’s rules by rule_id, or the one named. An unknown id answers an empty list. |
memory.copy_rule.delete |
writer | Deletes a rule. No memory is deleted. |
memory.copy_rule.run |
writer | Previews what a rule would propose, memory by memory (see evaluation), and writes no memory. With dry_run: false, runs the rule. |
All four are experimental. Over MCP, memory.copy_rule.list and memory.copy_rule.run are called by name through loom_op; set and delete answer OP_NOT_ON_MCP, because a person saves and deletes a rule. The dashboard’s Copy rules tab calls all four: a writer previews a rule there and its owner runs the previewed revision (Copy rules). A token whose read scope is narrowed below the namespace is refused all four.
Who may save
memory.copy_rule.set checks, in this order:
- The body:
rule_idis 1 to 64 characters of lowercase letters, digits,_and-, starting with a letter or a digit; the conditions and the two namespace names pass the rules above. Otherwise 422VALIDATION_FAILED. - The caller is a signed-in person: a token with a subject that is not a service account. Otherwise 403
AUTH_INSUFFICIENT. A deployment with sign-in off has no person, so it stores no rule. - Every live replica runs a build that knows the table (see rolling upgrades). Otherwise 503
OVERLOADEDwith a 30-second retry. - The caller can read the source namespace. Otherwise 403
AUTH_INSUFFICIENT, with the same answer whether the source exists or not.
The rest runs under one deployment-wide claim for the destination namespace, which delete takes too, so two saves of one id are ordered and the second reads the first’s row. A claim that is not free within five seconds answers 503 OVERLOADED.
| Stored row for the id | Answer |
|---|---|
| None | Stored with revision 1, owned by the caller. A namespace that already holds 100 live rules answers 409 PRECONDITION_FAILED. |
| Live, owned by someone else | 403 AUTH_INSUFFICIENT. |
| Live, owned by the caller | The revision rises by one when the conditions differ, and stays when only enabled changes. An identical body writes nothing. Another source is refused, 409. |
| Deleted | Stored anew above the deleted rule’s revision, owned by the caller. |
A revision number names one set of conditions for the life of the id. The answer is the stored rule and revised, which is true when the call stored a new revision.
The owner is the token’s subject, never a field of the body.
Who may list
Every viewer of the destination reads each rule’s source name, conditions and owner. The owner, who reads both namespaces, chose to name the source in the destination.
Who may delete
The rule’s owner, or an administrator of the destination. An administrator may be a service account: stopping what feeds a namespace needs no person. Another caller is refused with 403 AUTH_INSUFFICIENT, an id the namespace never held answers 404 RESOURCE_NOT_FOUND, and a rule already deleted answers deleted: false.
Evaluation
memory.copy_rule.run evaluates a rule and answers what a run would propose, memory by memory. A preview, the default (dry_run: true), writes no memory, refusal or event: the one row it leaves is its audit row. A paused rule previews as an enabled one. A run (dry_run: false) makes the same evaluation and then writes.
The call checks, in this order:
- The rule exists and is not deleted. Otherwise 404
RESOURCE_NOT_FOUND. - This build can parse the stored conditions. Otherwise 422
VALIDATION_FAILED, and no memory is read. - The caller can read the source namespace. This is checked on every call: the grant that let the owner save the rule may be gone, and the caller need not be the owner. Otherwise 403
AUTH_INSUFFICIENT, with no count. A call admitted to a restricted source leaves the restricted-access audit row.
Then it reads memory three times: the candidates, the destination’s rows at their keys, and the copies the rule’s runs wrote. Under the Postgres memory authority the reads start once ClickHouse holds every write committed in the two namespaces, or the call answers 503. A read that fails fails the call: no answer is built from a part of them.
Candidates
One statement reads the source: the live memories of from_namespace that meet every condition, are filed under no user and lie inside the caller’s read scope, ordered by key, agent and session. At most 1,000 are evaluated. When more match, the answer says truncated: true and covers the first 1,000. Under the Postgres memory authority each row read is replaced by the authority’s head, and a head that no longer meets the conditions is dropped.
A memory filed under a user (user_id other than -) is never a candidate: its copy would be filed under no user, outside that user’s erasure.
Each condition has one definition, evaluated in ClickHouse for the read and in Python for a head the authority returns:
| Condition | A memory meets it when |
|---|---|
memory_type |
Its type equals the value. |
key_prefix |
Its key starts with the value. |
tags_all |
It carries every tag listed. |
tags_any |
It carries at least one tag listed. |
pinned |
It is pinned. |
verified |
Its attestation binds its current content hash, the attesting actor is a person (human:), and that actor is not the actor who wrote the memory. |
updated_within_days |
Its updated_at is at most that many days before the call. |
agent |
Its agent label equals the value. |
A match selects a memory for a proposal. It approves nothing and grants nothing.
Exclusions
A candidate that a rule never copies is reported refused, with the first code that applies:
| Code | The source memory |
|---|---|
pending_review |
Is a proposal, or a rejected one. |
not_served |
Is superseded, conflicted, a duplicate, expired, past its valid_to, or in a status other than active. |
sensitive |
Is graded high. Its content_sha256 is blank in the answer. |
redacted |
Is redacted. |
type_not_copied |
Is a profile or a definition. |
is_a_copy |
Is a copy from another namespace: a copy of a copy would name the copy as its source, and the first source’s edits, deletion and erasure would not reach it. |
Outcome
Every other candidate is judged against the destination. One statement reads what the destination holds at the candidates’ keys, deleted rows included, with no value. It answers at most two rows a key: the namespace’s own row first, which is where a copy is filed, then rows of other identities, live ones before deleted ones. Those two rows decide every outcome below, so the answer does not grow with the agents, users and sessions that hold a key. It reads ClickHouse under both storage authorities.
A copy is filed under the identity (org, namespace, '-', '-', '-', source key). This source’s copy is the row at that identity whose cross_promoted_from_memory_id and cross_promoted_from_namespace name the source memory, whoever made it: a copy made with memory.copy_to counts.
| At the key in the destination | Same content_sha256 as the source |
Another hash |
|---|---|---|
| Nothing, or only deleted rows that are not this source’s copy | would_apply |
would_apply |
| A live row that is not this source’s copy, under any identity | refused, collision |
refused, collision |
| This source’s copy, deleted | refused, removed |
would_apply |
This source’s copy, rejected |
refused, rejected |
would_apply |
This source’s copy, proposed |
noop, pending |
would_apply: it replaces the undecided proposal |
| This source’s copy, any other status | Counted in already_copied |
noop, diverged: the copy is kept |
A source memory stored without a content hash is not known to hold another text than its copy. While the destination holds its deleted, rejected or undecided copy, it is refused with removed, refused with rejected or noop with pending, whatever hash the copy carries, and its served copy is noop with diverged.
When the destination holds this source’s copy and another live row at the key, the copy answers first where a run would write nothing, and a run that would write is refused with collision. When two candidates share a key, the first in candidate order that a run would write keeps the key, and every later one is refused with collision.
A preview judges exclusions and outcomes from what the statements read, and probes no row again.
The rule’s copies
One aggregate statement reads the copies the rule’s runs wrote: the live rows of the destination at the copy identity whose source_trace starts with copy_rule:<rule_id>@ and whose cross_promoted_from_namespace is the rule’s source. It answers two counts and one bounded list:
pending: the copies stillproposed. A rule holds at most 25 of them. An evaluation that findsnundecided reports the first25 - nnew matches, in candidate order, aswould_applyand every later one asnoopwithdeferred. The bound is a constant, since a setting would let one namespace queue more than its reviewers read.- The accepted copies whose source memory is not among the candidates: the source was deleted, edited out of the conditions, or moved out of the caller’s read scope. Each is counted in
noopand inreasonsasstopped_matching, and the first 100 are listed after the candidates. Such a row names the copy intarget_memory_idand the source memory by id alone, with a blank agent, session and hash, since the source was not read. The copy is kept: a rule proposes copies and never deletes one. A truncated evaluation leaves these out, because a source past the first 1,000 is not known to have stopped matching.
Answer
The answer carries the rule’s revision, from_namespace and conditions_sha256 (the sha256 of the stored conditions), dry_run, the counts, truncated, reasons and rows:
matchedis the number of candidates evaluated.would_apply,applied,failed,noop,refusedandalready_copiedadd up tomatchedplusreasons.stopped_matching.pendingis the number of the rule’s undecided proposals before the call wrote.reasonscounts thenoop,refusedandfailedrows by code.rowslists the candidates that are not already copied, in candidate order, then the copies whose source stopped matching, at mostlimit(1 to 1,000, default 100). A run lists everyappliedrow, pastlimittoo. A row has the source memory’s key, agent, session, id and content hash, itsstatusandreason, andtarget_memory_idwhen the destination holds this source’s copy, a deleted one included.
No memory text is in the answer. The counts cover only memories the caller reads, and the keys, ids and hashes are of source memories the caller reads.
The audit row records rule_id, revision, conditions_sha256, from_namespace, dry_run, the counts, pending, reasons and truncated. A preview’s row holds no key, id or hash of a memory. A run’s row lists each proposal written in applied_rows, by the source’s key, the copy’s id and the content hash copied.
Run
memory.copy_rule.run with dry_run: false proposes each new match. After checks 1 and 2 above and before any memory is read, it checks, in this order:
- The caller is a signed-in person. Otherwise 403
AUTH_INSUFFICIENT. - The caller is the rule’s
owner. Otherwise 403AUTH_INSUFFICIENT: an administrator who does not own the rule previews it and decides what it proposed. - The body names
revision. Otherwise 422VALIDATION_FAILED. revisionis the rule’s stored revision. Otherwise 409PRECONDITION_FAILED, naming the revision to preview: the owner runs the conditions they looked at.- The rule is enabled. Otherwise 409
PRECONDITION_FAILED.
The evaluation is the preview’s. A rule that selects more than 1,000 memories is refused with 422 VALIDATION_FAILED and reason: rule_too_wide before the destination is read: a run never covers a part of what a rule selects.
What a run writes
Each would_apply candidate is written by itself, in candidate order, through the function memory.copy_to builds its row with. Before the write:
- The source memory is read again in the caller’s read scope. A source that is gone, holds another text, is now excluded or no longer meets the conditions is
refusedwithchanged. - The head at the copy’s identity is read with sequential consistency and judged again by the outcome table. An outcome that writes nothing is the row’s outcome.
- The destination’s blacklist is applied to the value.
- A lock on a live row the copy would replace refuses the write.
The copy is one row at (org, namespace, '-', '-', '-', source key) with status = 'proposed' and requires_review = 1. Its actor is the owner (human:<subject>), its source_trace is copy_rule:<rule_id>@<revision>, and cross_promoted_from_memory_id and cross_promoted_from_namespace name the source. It keeps the source’s value, type, tags, sensitivity, validity anchors, grounding and attestation, and resets what memory.copy_to resets. A copy over this source’s undecided or rejected copy keeps that copy’s memory_id; a copy over a deleted one takes a new id.
Runs of one rule write one at a time. A run holds a claim on the rule from before the evaluation until its last write, so it counts the undecided proposals after every earlier run wrote, and the bound of 25 holds when two runs start together. A run that finds the claim held does not wait: it answers 503 OVERLOADED and writes nothing. The claim is not renewed. A run that has not ended 48 seconds after it took the claim is stopped with 503 BACKEND_UNAVAILABLE; the proposals it wrote stay and the next run counts them. The proposal being written when the run is stopped can be left without its memory.copy_to audit row.
The insert holds the key to the head it read, under the fences every memory insert takes. Runs of two rules over one source at once write one copy of it: the second finds the head moved and its row is failed, or reads the first’s proposal and answers noop, pending.
Each written copy leaves a memory.copy_to audit row with the source and copy ids, proposed: true and copy_rule: <rule_id>@<revision>. The run’s own audit row lists the same copies.
Write checks
A run’s write is a copy, not a memory.set. It runs these checks of the write path:
- The destination’s blacklist.
- The refusal of a deleted value: a text deleted from the destination is not written again (
refused,removed). - The hold of an unfinished erasure of the destination, and the review fence of the insert.
- The lock on a row the write would replace (
refused,held).
It does not run conflict surfacing, duplicate detection or the pinned-peer check: the reviewer who accepts the proposal decides whether it belongs beside what the destination holds.
Refusals and failures
One row’s refusal never fails the run. The row’s status and reason say what happened:
| Error of the row’s write | status |
reason |
|---|---|---|
| 403, 409, 422 or 423 | refused |
The error code, lower case. |
422 with reason: forgotten |
refused |
removed |
| 423 | refused |
held |
| Anything else, such as a 503 on a head that moved | failed |
The error code, lower case. |
A failed row wrote nothing, and the next run tries it again. The server log names the key, the code and the request id.
Who decides
A proposal a run wrote is decided with memory.proposal_decide, by the content hash the reviewer read. The independence check compares the reviewer with the row’s stored actor, which is the rule’s owner, so the owner cannot accept what their rule proposed, and neither can a service account. Another administrator of the destination accepts or rejects it. Until then the copy is a proposal like any other: search and recall do not serve it, a viewer of the destination reads it by key or in the proposals listing with status: proposed, and memory.set, memory.update and memory.promote at its key do not change it.
An accepted copy keeps its source_trace, so the copies of one revision are found by it: a memory.apply filter with source_trace: "copy_rule:<rule_id>@<revision>" deletes them. A request cannot write the marker: source_trace values starting with copy_rule: are refused on every write, and a copy of a rule-made copy into another namespace drops it.
What a run does not propose again
| The destination holds | The next run |
|---|---|
| The undecided proposal | noop, pending |
| The accepted copy, source unchanged | Counts it in already_copied |
| The copy, deleted, source unchanged | refused, removed |
| The copy, rejected, source unchanged | refused, rejected |
| A deleted or rejected copy, source edited | Proposes the new text, unless that text was deleted from the destination |
| The accepted copy, source edited | noop, diverged: the copy is kept |
Removing a copy does not undo what was read while it was served. An erasure of the source namespace deletes the rule and does not reach the copies in the destination: a copy is the destination’s row, and the destination’s own erasure covers it.
Storage
loom.memory_copy_rules is a ReplacingMergeTree(version) keyed by (org, namespace, rule_id), with no partition and no TTL. It is in ClickHouse under both storage authorities. A row holds the revision, the owner, enabled, the source namespace, the conditions, when the rule was saved first and when it last changed, and a tombstone time for a deleted rule.
Every read is a FINAL read on the full sort-key prefix with sequential consistency, and no read is cached, so a pause or a delete holds at the next call on every replica. A namespace holds at most 100 live rules. A save is three reads and one insert, four reads for a new rule, which also counts the namespace’s live rules. A list is one read. A preview is one read of the rule and three reads of loom.memory, and no insert. A run adds a claim on the rule and, for each proposal it writes, two point reads, one insert and one audit row, for at most 25 proposals a call. The capacity reading of loom doctor --capacity counts the operation’s requests with the bulk writes, since one request writes many rows. The candidate read and the read of the rule’s copies each cover the whole (org, namespace) range of their namespace, less what a sort-key column or a skip index on a condition leaves out, so their cost follows the size of that namespace. With lazy materialization, which ClickHouse applies by default on local disks and, from 26.8, on object storage, the source read takes the sort and filter columns of every row in the range and whole rows only for the 1,001 candidates it returns. Without it, the source read takes whole rows for the range.
The table is not counted when a namespace’s name is checked for reuse. A rule a later owner of the name finds copies nothing, and any administrator of the namespace deletes it.
Erasure
A namespace erasure deletes every rule stored in an erased namespace and every rule that reads from one, tombstones included, in the saga step copy_rules.delete. The step is the last of the namespace steps, so a rule saved while the saga ran is deleted by it. A save is not fenced against an erasure of its namespace: a rule stored after the step is configuration with no memory content, which the next erasure or an administrator’s delete removes.
A row holds namespace names, typed conditions and a principal’s subject, and no memory value, so the residue census does not count the table. A user erasure leaves owner, a principal’s identifier, in place.
Rolling upgrades
The migration that creates loom.memory_copy_rules is additive. A replica of the previous build answers the four operations as unknown and has no erasure step for the table, so memory.copy_rule.set stores nothing until every live replica, in every deployment on the store, runs that migration: no rule exists while an older build can run an erasure. list, delete and run are not gated: none stores a rule, and run answers 422 for a rule whose conditions this build cannot parse.
A replica of a build that only previews ignores dry_run and revision: it answers a preview, names both fields in ignored_args and writes nothing, so a run sent to it proposes nothing and the caller sees why. That build does not refuse a source_trace starting with copy_rule:. A row a request wrote with that trace is not counted as a rule’s copy unless its lineage also names the rule’s source, and it does not change what a run proposes.
A rollback leaves the stored rules and the proposals in place. A proposal is an ordinary proposed row, which the previous build’s memory.proposal_decide decides. See the copy rules rule in rolling upgrades.
Source entry points
server/memory/api/ops_copy_rules.py,server/memory/api/models_copy_rules.py: the operations and their wire models.server/memory/app/copy_rules.py: the handlers, the fleet gate, the run’s checks and the bound.server/memory/app/copy_ops.py:copy_one, the write of one proposal.server/memory/domain/copy_rules.py: the conditions, the stored form and the bounds.server/memory/domain/copy_outcome.py: the exclusions and the outcome of one memory.server/memory/domain/delete_marks.py: thecopy_rule:trace and the refusal of it on a request.server/memory/store/copy_rules.py: every statement on the table, the candidate read and the read of a rule’s copies.sdk/python/loom/cli_copy_rule.py:loom copy-rule run.server/memory/store/reads.py:rows_at_keys, the destination read.server/erasure/store/steps_ledgers.py: the erasure step.