Repository navigation
The pre-edit block keeps its token budget and its decisions' full source - #175
Merged
Merged
Conversation
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]>
…test Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
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]>
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
force-pushed
the
fix/for-file-budget-and-source
branch
from
November 2, 2026 10:39
07a440e to
062ebf4
Compare
_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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
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]>
8 of 9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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
… moreline fit the budget, the pre-edit block frommemory.for_filenow stays within itstoken_budgetwhen 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 aknowledge:citation keeps its#sectionand still resolves throughknowledge.get.Why
Two defects in
render_block(server/memory/app/for_file.py):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… moreline 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
… morecount 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_ordernow 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 thememory_idof 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 sameupdated_atlink one way only, the largermemory_idbeing the newer (before, each superseded the other).supersedes_keyand the author note still name the first older row.A decision's
— source: …was clipped at 120 characters. A longknowledge:<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 callsknowledge.geton 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 throughmemory.for_filehits. A row whose source makes its line too long for the budget is left out whole, the later rows still render, and the… moreline points to the op whosehitslist it with itssource_resource_id.The
_SOURCE_CHARScap 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_titleinserver/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
truncatedflag's meaning are unchanged. A chain of superseding rows is now listed newest first inhitsas well as inrendered: withasorted first andbsupersedinga,csupersedingb, 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… moreline is paid for, and it may hold a later, shorter row it used to drop. The header, the removal footer and the… moreline are always printed, so a budget smaller than those can be exceeded;docs/concepts/memory.md, thetoken_budgetfield comment and therender_blockdocstring now say so.Compatibility, rollout and rollback
token_budgetandhitsdescriptions.renderedis shorter when truncated, decision sources are no longer clipped, and a chain of superseding rows is listed newest first inhits(was[b, a, c]for a chain whose oldest row sorted first, now[c, b, a]).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 thattruncatedmatches the… moreline; 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… moreline fits); a 253-characterknowledge: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 equalupdated_atlink 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 withtruncated=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 onmain.tests/unit/test_file_rules_shared.py, throughhandle_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.tests/integration/test_memory_for_file_decisions.pyandtests/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 lintandmake typecheckpass, and the unit test files of the touched module; the full unit suite runs in CI. The integration files for this op pass.docs/reference/operations/memory.md(python scripts/codegen/docs_reference.py), for thetoken_budgetandhitsfield comments; the SDK generators change nothing.main; no merged migration is edited. (No migration.)frontend/changed. (Not changed.)docs/describe the system as it now is (docs/concepts/memory.md).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code