Repository navigation
refactor(knowledge): delete the dead and repeated code in seven over-gate functions - #2056
Merged
Merged
Conversation
_adopt_replayed_prior re-implemented, by hand, the member resolution
PatchKB.prior_file already owns: the files-root symlink check, the
per-part symlink walk, the resolve/relative_to escape check, the is_file
check, a read_bytes probe, validate_relative_path and a seen-set. Every
ref in warm_replay_outcome["replayed_patch_refs"] is a ref PRELUDE
resolved through that same PatchKB.prior_file before it applied it
(prelude.py:1001-1004, refs recorded at :2328-2346 and :2449-2450), so
CLOSE now asks the owner again and raises the existing "is missing"
error when it no longer resolves. An unreadable member fails at
adoption ("cannot adopt ...") instead of at the deleted read probe.
Also deleted:
- the knowledge.value fallback when reading the warm-start recipe.json:
its only writer, RemoteRecipeClient.read, flattens knowledge into the
top level (client.py:88-104, :350, :418), and PRELUDE reads overlays
only from the top-level value;
- the function-local `import json` (the module imports it) and the
getattr/Path() re-wrap of KnowledgeSections.warm_start_dir, which is
always a Path or None;
- str(... or "") on warm_replay_outcome["status"], which every writer
sets to a string;
- the per-candidate rewrite of value["patch"]["patches"]: the adopted
refs are merged once after the loop, with the same result.
The prior apply-root lookup and the provenance re-home move into
_prior_apply_roots and _provenance_with_apply_roots unchanged.
Co-authored-by: Cursor <cursoragent@cursor.com>
…alue
merge_staged_sections unioned every flat ref list it found already on
`value` with the staged column. Its only production caller,
build_remote_knowledge, has always passed a freshly built
{name: {} for name in RECIPE_SECTIONS}, and the prior record is adopted
only after the merge, so there was never anything to union. Delete the
union and _is_ref_list, and return {column: knowledge} in draft order
instead of writing into an argument and returning the names beside it.
`only` and `required` lose their None defaults: production always
passes both, and the one caller without them was the test of the union.
The "required " qualifier is computed once per column instead of in
each of its three raise sites, and the existing handler re-raises
RemoteRecipeValidationError through its own clause instead of an
isinstance check.
build_remote_knowledge:
- the outbox triage moves into _dropped_staged_sections, which no
longer re-filters the pending rows: once no row blocks, every pending
row is a dropped one;
- the try/except around int(row["stack_index"]) goes: the only producer
writes int(stack_index) (loop/writeback.py:618) and the drain reads it
with a bare int() (writeback.py:678, :696);
- the synthetic "__stack_index" key copied into every stack entry goes:
the required-owner check enumerates the mapping stack, the same
position _adopt_replayed_prior already uses for the replay index.
Deleted test: test_merge_staged_sections_unions_and_dedups_prior_refs,
which only exercised the union with a prefilled value that no caller
passes.
Co-authored-by: Cursor <cursoragent@cursor.com>
build_kernel_rewrite_value read
`kernel_opt_task_attempts or kernel_opt_attempts`. The only production
state that reaches it is the coordinator's SharedState
(recipe_journal.py:1383 -> remote_recipe/__init__.py:190 -> :88 ->
values.py:1085 -> agent_kb.py:425), where kernel_opt_attempts is a
property that re-indexes kernel_opt_task_attempts
(state/shared_state.py:1730-1735). That re-index returns {} for an
empty ledger (kernel/_kernel_decisions.py:741-753), so whenever the
first term is empty the second is too. Read the one owner.
The experience document printed `patch or 'unavailable'`, but `patch`
is the ref files.add returned, and the loop raises a few lines earlier
when that ref is empty. The document is now built from one f-string
with the same bytes, instead of joining a tuple of lines.
A differential run of the builder on base and head, over rooted,
rootless and padded-root attempts, gives byte-identical rows, staged
members and warnings.
Co-authored-by: Cursor <cursoragent@cursor.com>
…tion
knowledge_to_warm_recipe picked the validated gain with
`interactivity_gain if "interactivity_gain_pct" in knowledge else ...`
after computing interactivity_gain up front. `knowledge` is always a
plain dict here, so `knowledge.get("interactivity_gain_pct", fallback)`
selects the same branch for every record, including one that stores
the key with a null value.
`bool(x) if isinstance(x, bool) else True` for view.replayable is
`x is not False` for every input: only the False singleton disables
replay, exactly as before.
Co-authored-by: Cursor <cursoragent@cursor.com>
RemoteWarmRecipeAdapter.search carried two flags, pages_scanned and has_more, only to decide whether to warn that the scan stopped at its page budget. The warning fired exactly when every page iteration ran to completion, which is what a for loop's else clause states, so both flags go. The while condition also re-checked the candidate cap before every page, but each iteration that continues has just broken out if the cap was reached, so the check only matters on entry and is now made once before the scan. The label alias table mapped "hardware" to "hardware", which aliases.get(key, key) already does; that entry goes. The per-item loop moves unchanged into _page_rows, which projects one page of identity hits onto warm rows and caches each new candidate's view until the scan cap. A differential run on base and head over nine scenarios (page budget exhausted, exactly ten pages, candidate cap mid-page and at a page boundary followed by a second search, cached rows, rejected and missing views, non-advancing and non-integer next_offset, non-list items, an empty first page) gives byte-identical rows, store calls, view fetches and log records. The one input that now behaves differently is search_page_cap <= 0, which logs "stopped after 0 pages" where it used to stay silent. It is a class constant of 10 and nothing in the repository assigns it. Co-authored-by: Cursor <cursoragent@cursor.com>
_sanitize_value handed extra_server_args to sanitize_publish_server_args as str(value or ""), and that function's first statement tokenizes str(value or "") again. Converting twice is the same as converting once, so the caller's copy goes. Co-authored-by: Cursor <cursoragent@cursor.com>
Remove the ten function entries the remote_recipe rework brought within the gate (_adopt_replayed_prior cognitive, cyclomatic and length; merge_staged_sections, knowledge_to_warm_recipe and _sanitize_value cognitive; RemoteWarmRecipeAdapter.search cognitive and length; build_kernel_rewrite_value and build_remote_knowledge length) and lower the values.py module length from 1284 to 1224 lines. Co-authored-by: Cursor <cursoragent@cursor.com>
Code metrics gate: PASSEDJudged: the 5 files this change touches. Files it does not touch never fail it; anything found there is listed under Outside this change for information.
116 findings in files this change does not touchOutside this change (informational): 116
...and 91 more; the job log lists every row.
Thresholds and sources: |
fengshaoyi-amd
marked this pull request as ready for review
October 20, 2026 11:11
Contributor
Author
|
/review |
|
Automated review started for head 54c1cc2. Progress: https://github.com/AMD-AGI/Hyperloom/actions/runs/39413552911 |
|
Automated review: no verdict applied -- the review job ended with result 'failure'. Comment |
…ote-recipe Co-authored-by: Cursor <cursoragent@cursor.com>
chennyiiis
pushed a commit
that referenced
this pull request
Oct 21, 2026
Resolves the code_metrics_baseline.txt ratchet conflict with resolve_baseline.py. main's #1782 r9700/gfx1201 board test and #2056's deletion of test_merge_staged_sections_unions_and_dedups_prior_refs both survive; our restructuring of recipe_kb_t0/_warm_start_match is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
What this does
Seven functions in the remote Recipe knowledge path (
orchestrator/knowledge/remote_recipe/) were over the code-metrics gate. This PR brings each one under every limit it broke, mostly by deleting what it re-did: a hand-written copy of the member resolutionPatchKBalready owns, a union into an argument its only caller passes empty, a fallback for a recipe layout no writer produces, a synthetic index key, a fallback to a derived attempts view, two flags that restate afor … else, two ternaries that a.getdefault andis not Falsealready express, and a conversion the callee repeats anyway.Production code: +160 / −227 (net −67) across 3 files. The code-metrics baseline loses 10 entries and gains none; the
values.pymodule-length entry drops from 1,284 to 1,224.This continues the complexity cleanup of #2024, #2026 and #2052. The seven commits read one at a time, and each commit message lists what it deleted and why that is safe.
remote_recipe/values.pyremote_recipe/values.pyremote_recipe/values.pyremote_recipe/values.pyremote_recipe/__init__.pyremote_recipe/sanitize.pyscripts/code_metrics_baseline.txtremote_recipe/is undersrc/hyperloom/orchestrator/knowledge/. Every other path cited below is undersrc/hyperloom/orchestrator/, and every line number is on this PR's head.Gate numbers per function
Measured with the gate's own tools (ruff C901, complexipy, function lines), base → head. Limits are 20, 30 and 80, and a value fails only above its limit; bold values were over the limit at base.
_adopt_replayed_priormerge_staged_sectionsbuild_remote_knowledgebuild_kernel_rewrite_valueknowledge_to_warm_recipeRemoteWarmRecipeAdapter.search_sanitize_valueAcross the 3 touched production files: C901 total 297 → 288, cognitive total 617 → 556, decision points 551 → 524, functions 66 → 69 (four added,
_is_ref_listdeleted). Functions over a gate limit: cyclomatic 1 → 0, cognitive 5 → 0, length 4 → 0.New names
Numbers are cyclomatic / cognitive / lines.
_prior_apply_roots(6 / 12 / 14): the prior record's apply roots keyed by prior overlay ref, lifted unchanged out of_adopt_replayed_prior._provenance_with_apply_roots(2 / 8 / 22): merges the re-homed apply roots into the provenance row of the replay stack index, creating that row when absent; lifted unchanged._dropped_staged_sections(2 / 6 / 16): fails while section staging is still owed, otherwise returns the stagings dropped for missing patch sources; the outbox triage that openedbuild_remote_knowledge.RemoteWarmRecipeAdapter._page_rows(10 / 20 / 46): projects one page of identity hits onto warm rows, fetching and caching each new candidate's view until the scan cap; the per-item loop ofsearch, unchanged._is_ref_list, which only the deleted union called.Observable effect
Nothing an operator sees changes. Published Recipe records, the warm rows T0 ranks, staged columns, adopted overlay files and log lines are the same for every input the repo's own callers produce.
knowledge_to_warm_recipeand_sanitize_valuereturn the same result for every input; for the second,sanitize_publish_server_argsapplies the samestr(value or "")itself (remote_recipe/sanitize.py:173).The differences below are reachable only by input that no caller in the repo produces:
_adopt_replayed_prior). Each replayed ref now resolves throughPatchKB.prior_file(knowledge/agent_kb.py:48-73).PatchKB.prior_filedoes not resolve fails with the existing "successfully replayed prior overlay is missing: ''". Base had a message per cause: "replayed prior files root must not be a symlink", "successfully replayed prior overlay resolves through a symlink: …", "replayed prior overlay escapes files root: …", and the path-validation error for a ref containing... A ref outsidepatch/, which base adopted, now fails the same way./or surrounding whitespace now resolves the way PRELUDE resolved it. Base rejected the first as an invalid artifact path and reported the second missing.PatchKB.prior_file, and gave up the replay when one did not resolve (phases/prelude.py:1001-1004). The ref travels as the patch entry'spatch_file(:1006), which the baseline executor echoes back for each applied overlay (actions/executors/baseline.py:1263,:1455-1460), intowarm_replay_outcome["replayed_patch_refs"](phases/prelude.py:2328-2346,:2449-2450). Only a warm-start tree changed between PRELUDE and CLOSE reaches these paths.recipe.jsonthat carries its overlays only underknowledge.value. Base fell back toknowledge.value. Head reads only the top-levelvalue, so such a file fails with "successfully replayed prior overlays are absent from prior knowledge". The only writer,RemoteRecipeClient.read, flattens knowledge into the top level (remote_recipe/client.py:88-105,:350,:418), and PRELUDE reads the overlays from that top-levelvaluetoo (PatchKB.read_patchesatphases/prelude.py:995reads throughKnowledgeSections.read,remote_recipe/_vendor/kb_store_client.py:697-705).warm_replay_outcome["status"]that is not a string but whosestr()is "reproduced" no longer counts as reproduced. Every writer assigns a string literal: PRELUDE's replay paths (for examplephases/prelude.py:1496and:2447) andloop/writeback.py:4086-4088.sectionsobject whosewarm_start_diris a string, or that has nowarm_start_dirat all, raisesTypeErrororAttributeError. Base wrapped the string in aPath, and treated a missing attribute as "no warm-start artifact root".KnowledgeSectionsstoreswarm_start_diras aPathorNone(remote_recipe/_vendor/kb_store_client.py:573-576), and production passesKnowledgeSections.from_env()(remote_recipe/__init__.py:91).stack_indexis not an integer. Base skipped the row when matching dropped stagings to stack entries; head raises theValueErrororTypeErrorfromint(). The only producer writesint(stack_index)(loop/writeback.py:618), and the drain that moves rows into the dead letter already reads it with a bareint()(loop/writeback.py:678,:696).optimization_stack. Base matched a dropped staging to a stack entry by the entry's position in the raw list. Head uses its position among the mapping entries, the position the replay index and_worked_from_stackalready used. The field is typedlist[dict[str, Any]](state/shared_state.py:515), and every reader in this module skips non-mapping entries.merge_staged_sections's interface. It returns{column: knowledge}instead of writing into avalueargument and returning the names, it no longer unions ref lists already present on that argument, andonlyandrequiredare now required keywords. It is listed invalues.__all__, but theremote_recipepackage does not re-export it and nothing else in the repository imports it. Its one caller,build_remote_knowledge(remote_recipe/values.py:1087), always passed a freshly built emptyvalue.build_kernel_rewrite_valuereads onlykernel_opt_task_attempts. A state object that carries attempts only underkernel_opt_attemptsno longer publishes them. The only production state that reaches the builder is the coordinator'sSharedState(knowledge/recipe_journal.py:1382-1386→remote_recipe/__init__.py:190→:88→remote_recipe/values.py:1085→knowledge/agent_kb.py:425). There,kernel_opt_attemptsis a property that re-indexeskernel_opt_task_attempts(state/shared_state.py:1722-1727), and the re-index of an empty ledger is empty (kernel/_kernel_decisions.py:741-753).RemoteWarmRecipeAdapter.search_page_capof 0 or less now logs "remote Recipe candidate search stopped after N pages", N being the cap, where base stayed silent. It is a class constant of 10 (remote_recipe/__init__.py:205), and nothing in the repository assigns it; tests assign onlysearch_candidate_cap.Checklist
test_merge_staged_sections_unions_and_dedups_prior_refsexercised only the union into a prefilledvalue, which no caller passes. No test is added.test_column_kb,test_enablement_recipe_steps,test_promote_shared_state_lockandtest_remote_recipe_v2) give 347 passed and 15 failed. Each of the 15 also fails on this host without this change, rerun on a main checkout (b4e296a) whoseremote_recipemodules are identical to this PR's base. Linux CI on this PR is the authority.test_rewrite_writer_accepts_multi_file_patchandtest_integrate_stack_is_authoritative_for_rewrite_patch), and no test reaches the page budget or the pagination errors ofsearch. Both changes were therefore also run differentially against the base modules, which main has not changed since, with no difference:build_kernel_rewrite_value: rooted, rootless and padded-root attempts. Rows, staged members and the logged warning are byte-identical.RemoteWarmRecipeAdapter.search: nine scenarios, covering the page budget exhausted and exactly met, the candidate cap mid-page and at a page boundary followed by a second search, cached rows, rejected and missing views, non-advancing and non-integernext_offset, non-list items, and an empty first page. Rows, store calls, view fetches and log records are byte-identical.[tool.coverage.run]omits__init__.pyfiles, as in CI). The two uncovered lines are thecontinues that skip non-mapping provenance rows in_prior_apply_roots.scripts/pr_hygiene.py size), under the 400-line warning; net −67._page_rows, is at cyclomatic 10 and 46 lines._adopt_replayed_prior(139 → 75),build_remote_knowledge(107 → 76) andbuild_kernel_rewrite_value(88 → 78). Four stay over the cyclomatic trigger of 10 without gaining a branch:_sanitize_value(15),_adopt_replayed_prior(14),knowledge_to_warm_recipe(11) andsearch(11)._adopt_replayed_prior,knowledge_to_warm_recipeand_sanitize_valuesit at cognitive 30, which the gate allows; each got there by deletion alone.values.pystays over the 1,200-line module limit, but gets shorter: 1,291 → 1,224 lines, and its baseline entry drops from 1,284 to 1,224._adopt_replayed_prior's hand-written copy ofPatchKB's member resolution: the files-root and per-part symlink checks, the escape check, the read probe, the path validation and the seen-set.knowledge.valuefallback for the warm-startrecipe.json.import json, thegetattr/Path()re-wrap ofwarm_start_dir, and thestr(… or "")on the replay status.merge_staged_sectionstogether with_is_ref_list. Its write-into-valueoutput channel became a return value, and its three copies of the qualifier became one.build_remote_knowledge: the synthetic__stack_indexkey, thetry/exceptaroundint(stack_index), and the second filter over pending rows, which every row passes once the blocking check has not raised.build_kernel_rewrite_value: thekernel_opt_attemptsfallback, andpatch or 'unavailable', whosepatchis never empty past the raise atremote_recipe/values.py:675-679.knowledge_to_warm_recipe.search: the identity alias"hardware": "hardware", thepages_scannedandhas_moreflags, and the per-page re-check of the candidate cap._sanitize_value: the duplicatedstr(value or "").merge_staged_sectionschanges its interface (see above), and its only caller moves with it.Known merge interactions
remote_recipe/values.py, on one line: theattempts = …line ofbuild_kernel_rewrite_value, which this PR shortens and [fix] close the defects behind the MuseSpark run shortfall and delete state nothing reads #2055 deletes, because it moves the builder ontopending_kernel_integrationsand deletesSharedState.kernel_opt_task_attemptsandkernel_opt_attempts. Whichever lands second takes [fix] close the defects behind the MuseSpark run shortfall and delete state nothing reads #2055's side of that line; the rest of both changes merges cleanly. Once [fix] close the defects behind the MuseSpark run shortfall and delete state nothing reads #2055 is in, thekernel_opt_attemptsdifference listed above goes away with the field. The two also conflict inscripts/code_metrics_baseline.txt: both remove thebuild_kernel_rewrite_valuelength entry and lower thevalues.pymodule-length entry, so the second one to land re-measures that entry. [fix] close the defects behind the MuseSpark run shortfall and delete state nothing reads #2055 merges cleanly with main on its own.test_remote_recipe_v2.py, which merges cleanly. In the files this PR changes, they conflict only in the baseline file, where they already conflict with main.scripts/code_metrics_baseline.txt.Gate
scripts/code_metrics.py --base-ref origin/main(5fbbcf3) passes on the 5 touched files: 0 new, 0 worse, 0 out-of-date entries, and every design check (comments, English only, production importing test code, repeated literals, A1, A8–A12) has 0 findings. Baseline: −10 entries, +0, with one module-length entry lowered.ruff checkandruff format --checkare clean on the touched files.