Skip to content

The pre-edit block keeps its token budget and its decisions' full source - #175

Merged
zlareb1 merged 21 commits into
mainfrom
fix/for-file-budget-and-source
Nov 2, 2026
Merged

zlareb1 merged 21 commits into
mainfrom
fix/for-file-budget-and-source

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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):

Whenever its header, its removal footer and its … more line fit the budget, the pre-edit block from memory.for_file now stays within its token_budget when it leaves rows out, and a row too long for the budget no longer hides the rows after it. The block also prints a decision's source whole, so a knowledge: citation keeps its #section and still resolves through knowledge.get.

Why

Two defects in render_block (server/memory/app/for_file.py):

  1. When rows did not fit, the closing - … <n> more; memory.for_file(path=…) lists them. line was appended after the budget check, with no room kept for it, so a truncated block ran past its budget (a 300-token block could render as 323). A block whose rows all fit is printed whole, as before, with no room reserved. Otherwise each row has to fit together with a … more line counting every row that may still be left out (the ones already skipped and the ones after it).

    A row that does not fit is skipped rather than ending the block: the rows after it still get their turn, and the … more count includes it. Before, the first row that did not fit ended the block, which matters more once sources are unbounded: one decision with a very long source would hide every row below it. A row placed below a newer row that supersedes it is left out with that row, in any namespace, so an outdated rule never prints without the row that replaces it. For that to hold, _supersede_order now places the rows newer than a row first, recursively, so a chain of superseding rows always reads newest first (before, a chain whose oldest row sorted first came out with the newest row last); the newer row records the memory_id of every older row it is linked to (internal, not on the wire), so a row linked to two older rows takes both out with it, and only those exact rows, never another writer's row under the same key; and two linked rows with the same updated_at link one way only, the larger memory_id being the newer (before, each superseded the other). supersedes_key and the author note still name the first older row.

  2. A decision's — source: … was clipped at 120 characters. A long knowledge:<ns>/<uri>#<section> lost its #section, or part of its URI, and the clipped text read as a different citation: an agent that copies it calls knowledge.get on the wrong document or section. The source is now printed whole and counts against the budget like the rest of the line. Of the two options (print it whole, or elide the middle of the URI behind a marker), printing it whole is the simpler one and the only one that keeps the citation an agent copies resolvable; an elided source would have to be looked up again through memory.for_file hits. A row whose source makes its line too long for the budget is left out whole, the later rows still render, and the … more line points to the op whose hits list it with its source_resource_id.

The _SOURCE_CHARS cap is removed. Only line breaks in a source are replaced by a space, so the block stays one line per row; other whitespace stays as written. Section titles are already whitespace-normalised when a document is chunked (_clean_title in server/knowledge/domain/chunking.py), but the document URI part of a citation is not, so collapsing all whitespace could change a citation.

Scope and risk

The rendering of the pre-edit block, and the order of a chain of superseding rows. The query and the truncated flag's meaning are unchanged. A chain of superseding rows is now listed newest first in hits as well as in rendered: with a sorted first and b superseding a, c superseding b, the order was [b, a, c] and is now [c, b, a]. Every other row keeps its place. A block may now hold one row fewer than before at the same budget, because the … more line is paid for, and it may hold a later, shorter row it used to drop. The header, the removal footer and the … more line are always printed, so a budget smaller than those can be exceeded; docs/concepts/memory.md, the token_budget field comment and the render_block docstring now say so.

Compatibility, rollout and rollback

  • Wire: no op or field added, changed or removed; the reference page carries the reworded token_budget and hits descriptions. rendered is shorter when truncated, decision sources are no longer clipped, and a chain of superseding rows is listed newest first in hits (was [b, a, c] for a chain whose oldest row sorted first, now [c, b, a]).
  • Operator steps: none.
  • Rollback: a revert restores the old rendering; nothing is stored.

Tests

  • tests/unit/test_memory_for_file.py: a sweep over budgets 50 to 399 with twelve rows asserts the rendered text and the reported tokens never exceed the budget, and that truncated matches the … more line; a boundary test fills the 320 characters of an 80-token budget exactly with two rows (both print, not truncated), then adds one character (the second row is left out and the … more line fits); a 253-character knowledge: source keeps its #section; a long superseding row above a short superseded one leaves both out (… 2 more); in the caller's own namespace, another writer's row under the superseded key still prints; a row linked to two older rows leaves all three out (… 3 more); two linked rows with equal updated_at link one way, in the same order whichever comes first; a three-row chain linked through _supersede_order, oldest row pinned and sorted first, renders newest first and, at a small budget, leaves all three out (… 3 more); a mixed-length sweep over budgets 50 to 599 stays within budget; the handler test at a 90-token budget now stays within 90 and prints its third row; a block whose two rows (100 and 30 characters) fit in 193 of 200 characters prints both with truncated=False; a source with a line break stays on its row with its double space and tab kept; a decision with a 1500-character source ahead of a short row leaves only itself out (… 1 more). The sweep and the long-source tests fail on main.
  • tests/unit/test_file_rules_shared.py, through handle_memory_for_file: two linked team rows at a truncating budget both leave the block (… 2 more); the merged-rows test at a 150-token budget stays within 150.
  • make format lint typecheck: pass. tests/unit/test_public_copy.py, tests/unit/test_doc_links.py: pass.
  • Integration, against a private ClickHouse and a server from source with auth off: tests/integration/test_memory_for_file_decisions.py and tests/integration/test_memory_code_links.py: 197 passed.

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; the budget arithmetic and the tests were checked against the old and new code.

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

Checklist

  • make format-check, make lint and make typecheck pass, and the unit test files of the touched module; the full unit suite runs in CI. The integration files for this op pass.
  • Generated files are regenerated in this PR, not by hand: docs/reference/operations/memory.md (python scripts/codegen/docs_reference.py), for the token_budget and hits field comments; the SDK generators change nothing.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (No migration.)
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. (No plugin file changed.)
  • The frontend builds, if frontend/ changed. (Not changed.)
  • Docs under docs/ describe the system as it now is (docs/concepts/memory.md).
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 15 commits November 2, 2026 10:31
render_block appended the "… more" line after the budget check without
room for it, so a truncated block ran past its token budget. A row that
leaves others out after it now has to fit together with that line.

A decision's source was clipped at 120 characters, so a long
knowledge:<ns>/<uri>#<section> lost its section and read as a different
citation. The source is now printed whole and counts against the budget
like the rest of the line.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A row that does not fit the budget is now skipped instead of ending the
block, so one decision with a very long source cannot hide every row
below it; the "… more" line counts it, and each row reserves room for
that line at the largest count it could reach.

Only line breaks in a source become spaces: a document URI may hold
repeated spaces, and collapsing them would cite another document.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The reservation for the "… more" line was taken even when every row
fit, so a block that fit whole could drop a higher-ranked row for a
lower-ranked one. The rows are now built first and printed whole when
they fit; the reserving pass runs only when some row must be left out.

The docs, the token_budget comment and the render_block docstring say
that the header, the removal footer and the "… more" line always print.

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

When the budget left out a newer team row, the older row it supersedes,
placed just below it, could still print and read as current. A row left
out now takes the rows it supersedes with it, counted in "… more".

The handler test asserts the block stays within its budget, the
reservation drops a branch that could not be taken, and the docs and
the op docstring say the header, footer and "… more" line always print.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The rule that leaves a superseded row out with its superseder applies
to every linked pair, own rows included; the docs now say so, and tests
cover an own-namespace pair and a three-row chain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
_supersede_order placed each newer row above its older one, but not the
row newer than that, so a chain sorted with its oldest row first came
out [b, a, c] and render_block could print b after leaving c out. Each
row now places the rows newer than it first, recursively, so a chain is
always newest first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The shared file-rules test no longer allows a "… more" overrun, and the
two supersede tests share one row helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
A block whose rows all fit prints no "… more" line and can stay within
a budget smaller than the header, footer and "… more" line together.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The header and the removal footer always print; the "… more" line only
when the budget leaves a row out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Keys are unique per writer, so hiding by (namespace, key) also hid
another writer's row under the same key. _supersede_order now records
the superseded row's memory_id, and render_block hides by that id,
down a chain as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
handle_memory_for_file rendered from the wire hits, which carry neither
memory_id nor supersedes_id, so render_block never left a superseded row
out with its superseder. It now renders each hit with the ids of the row
it was built from. The boundary test states its arithmetic directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Two writers' rows under one key can both reach the block from the
caller's own namespace, so the test builds them there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the fix/for-file-budget-and-source branch from 07a440e to 062ebf4 Compare November 2, 2026 10:39
zlareb1 and others added 6 commits November 2, 2026 10:49
_supersede_order recorded only the first older row a newer row is linked
to, so a second one printed as current when the newer row was left out.
The newer row now records every older row's memory_id, and render_block
hides all of them, down chains as before. Two linked rows with the same
updated_at no longer supersede each other: the memory_id breaks the tie.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
render_block counted a newline after the last line too, so a block one
character short of its budget was treated as full. It now counts only
the newlines between lines. The token_budget comment's first sentence,
which the reference page shows, says the header, footer and "… more"
line can exceed it, and the hits comment says two typed decisions keep
their order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 68256aa into main Nov 2, 2026
20 of 22 checks passed
@zlareb1
zlareb1 deleted the fix/for-file-budget-and-source branch November 2, 2026 11:24
@pufit pufit mentioned this pull request Nov 2, 2026
9 tasks done
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