Skip to content

Fix the inline-function ratchet double-count#2429

Merged
joshalbrecht merged 2 commits into
mainfrom
mngr/inline-fn-ratchet-doublecount
Jul 14, 2026
Merged

Fix the inline-function ratchet double-count#2429
joshalbrecht merged 2 commits into
mainfrom
mngr/inline-fn-ratchet-doublecount

Conversation

@weishi-imbue

@weishi-imbue weishi-imbue commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

What

find_inline_functions in libs/imbue_common double-counts a function nested two or more levels deep. It iterated every FunctionDef in a file (get_ast_nodes_of_type returns nested ones too), then ast.walked under each. A function with two enclosing functions is a descendant of both, so it was emitted once per ancestor. snapshot(N) never meant "N inline functions" for any file with 2-deep nesting.

Nested defs are now collected keyed by (lineno, col_offset) and counted once. Two distinct defs cannot share both coordinates, so the dedup is exact.

Blast radius: one project

Measured across all 44 projects that pin this ratchet, using each test's real _DIR: exactly one recorded count changes — apps/minds, 9 -> 7. Every other project has zero 2-deep nesting (imbue_common's own count, 3, is unchanged). The two counts removed from minds are the same doubly-nested function counted twice.

Why this exists

main briefly went red on apps/minds test_prevent_inline_functions after a merge race (PR #2409 tightened the snapshot 9->8 against a base that had 8; PR #2413 legitimately added a 9th violation when main still pinned 9; the merge combined them). Main self-healed by bumping the snapshot back to 9. This PR is the honest follow-up: fix the checker so the recorded count is the true count, rather than leaving an inflated snapshot in place.

Not in scope (deliberately)

An earlier revision of this PR also hoisted the backup-detail endpoint's one genuine inline closure (_run_check_into_results) to a module-level helper, which would have taken minds to 6. Reverted: that closure captures five locals and is used once as a thread target, so the inline form reads better than a five-argument module-level function. It stays, and it is honestly counted in the 7.

Verification

  • find_inline_functions recount: minds 7, imbue_common 3 (unchanged).
  • Blast-radius sweep over all 44 pinning projects with real _DIR: only minds changes.
  • apps/minds + imbue_common inline-function ratchets: pass at 7 and 3.
  • Ratchet checker unit tests core_test.py: 25 passed.
  • ty check clean on the changed source.

🤖 Generated with Claude Code

…e helper

find_inline_functions walked every FunctionDef in a file (nested ones
included) and descended into each one's full subtree, so a function nested
two or more levels deep was emitted once per enclosing function. The
recorded snapshot counts therefore overstated the real number of inline
functions. Nested defs are now collected keyed by source position and
counted once. Across the monorepo the only project whose recorded count
changes is apps/minds (9 -> 7 with no code change).

Separately, hoist the backup-detail endpoint's one remaining inline helper
to a module-level _append_backup_service_check invoked via
start_new_thread(args=...). This is behavior-preserving and keeps the
ratchet honest, rather than trading the inline-function violation for a
functools.partial one (that ratchet is at 0 for minds). With both changes
the minds inline-function count is 6.
Revert the api_v1 hoist: _run_check_into_results closes over five locals
and is used once as a thread target, so the inline closure reads better
than a module-level five-argument helper. It stays.

The shared-checker double-count fix stands on its own. With the closure
kept, the minds inline-function count settles at 7 (down from the buggy 9),
not 6.
@weishi-imbue weishi-imbue changed the title Fix the inline-function ratchet double-count (and remove a minds inline helper) Fix the inline-function ratchet double-count Jul 10, 2026
@weishi-imbue weishi-imbue marked this pull request as ready for review July 10, 2026 23:05
@joshalbrecht

Copy link
Copy Markdown
Contributor

lgtm, thanks for the fix!

@joshalbrecht joshalbrecht merged commit 029e64d into main Jul 14, 2026
14 checks passed
@joshalbrecht joshalbrecht deleted the mngr/inline-fn-ratchet-doublecount branch July 14, 2026 17:17
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.

2 participants