Repository navigation
refactor(toplevel): flatten five nested ternaries, drop a shadow import - #13141
Conversation
|
Intent: Make five nested-ternary sites in the top-level |
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing to check. [DESIGN-REVIEWED] e11e8c7 |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
First Principles Review (Fable 5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (10 items) — 10 justifiedIntent: make five branch-heavy sites readable and single-spelled so a debugger picks the right arm — a behavior-preserving FIX-shaped refactor, no capability change.
Checks run: [FIRST-PRINCIPLES-REVIEWED] e11e8c7 |
Opus 5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
2287c67 to
eaf43fc
Compare
|
Rebased: main moved in The rebase is what clears the previous head's Attribution of the four reds on
Gates re-run green on the rebased head ( The simplification detector re-scan after the rebase reports 0 actionable sites in |
eaf43fc to
33da02f
Compare
|
Rebased: main moved 13 commits since the last head went green ( Still no code change: Gates on Tests. The scoped runner selects 681 files here, because
The two remaining CI reds on the previous head were the same two deterministic macOS failures, reproduced identically on two independent heads, both tracked and neither reachable from this diff:
Fixing either would mean editing a file outside the |
|
Re-ran that one job only ( |
|
That explains the timeline exactly: shard 3 was green on both earlier heads ( Every red on this head is main's, and all three are tracked:
Worth recording for whoever picks this up: the previous head Not fixing any of the three here. Each lives outside the
|
33da02f to
4971a91
Compare
|
Rebased: main moved in 142 files this PR also touches since this head went green The diff is byte-identical after the rebase — one commit, same five production Why the previous head's three test reds needed no code change here. Each is a
Attribution was measured, not assumed: the eight failures in the local scoped run One site is deliberately left out of scope: |
|
That is a 1-second budget for a worker thread to be scheduled. Nothing in this diff can reach that path. Of the five production files, only Worth a tracked issue on its own merits — the budget is wall-clock rather than an |
Five chained-ternary expressions in four top-level modules read as nested expressions where the code is a plain first-match-wins chain. Each becomes an if/elif/else with the same conditions in the same order, so precedence and short-circuiting are unchanged and no call the original skipped is now evaluated. agent_discovery spelled one directory choice three ways. Both `is not None` spellings — in agent_skill_globs and agent_welcome_message — now read identically. The third, `agents_dir or _kiro_agents_dir()` in _all_agent_specs, is a FALSINESS test and is deliberately left alone: folding it in would change what a falsy non-None agents_dir resolves to. context_blocks.measure_prompt's four-deep domain ternary sits inside a dict comprehension, where a statement chain is not expressible, so it moves to a _block_domain helper beside it. measure_prompt's output is unchanged. platform_compat.file_lock computed its Windows timeout ceiling with a ternary over the same if/elif/else that chose the acquire call one line later. The two chains merge into one, so the condition is spelled once. history_projection drops an in-function `import os` that re-binds the module-scope import from line 16. The sibling `import tempfile` has no module-scope binding and stays. Flattening makes each arm its own measured statement, and five arms turned out to have had no test. Six tests cover them, so no touched file's coverage regresses: the five domains of measure_prompt, a global-scope agent reopened from an explicit agents_dir, the Windows not-waiting and explicit-timeout lock refusals, and a host with no native memory reader. No behavior change; no keystone file reshaped.
4971a91 to
e11e8c7
Compare
|
Rebased: main moved in The overlap is import-level only — none of the five production files this PR This is the second re-sync on this PR. The previous head reached |
…rt (kirodotdev#13141) Five chained-ternary expressions in four top-level modules read as nested expressions where the code is a plain first-match-wins chain. Each becomes an if/elif/else with the same conditions in the same order, so precedence and short-circuiting are unchanged and no call the original skipped is now evaluated. agent_discovery spelled one directory choice three ways. Both `is not None` spellings — in agent_skill_globs and agent_welcome_message — now read identically. The third, `agents_dir or _kiro_agents_dir()` in _all_agent_specs, is a FALSINESS test and is deliberately left alone: folding it in would change what a falsy non-None agents_dir resolves to. context_blocks.measure_prompt's four-deep domain ternary sits inside a dict comprehension, where a statement chain is not expressible, so it moves to a _block_domain helper beside it. measure_prompt's output is unchanged. platform_compat.file_lock computed its Windows timeout ceiling with a ternary over the same if/elif/else that chose the acquire call one line later. The two chains merge into one, so the condition is spelled once. history_projection drops an in-function `import os` that re-binds the module-scope import from line 16. The sibling `import tempfile` has no module-scope binding and stays. Flattening makes each arm its own measured statement, and five arms turned out to have had no test. Six tests cover them, so no touched file's coverage regresses: the five domains of measure_prompt, a global-scope agent reopened from an explicit agents_dir, the Windows not-waiting and explicit-timeout lock refusals, and a host with no native memory reader. No behavior change; no keystone file reshaped.
Problem / Motivation
Five places in the top-level
src/kiro_crew/modules pick one value out ofthree or five choices, and all five spell that as a ternary nested inside a
ternary. A reader has to hold the whole expression in their head to answer
"which arm runs when
waitis false?".context_blocks.pyis the worst: fiveoutcomes, four levels deep, indented sixteen columns inside a dict comprehension.
src/kiro_crew/agent_discovery.pyspells one decision — "reopen this agent'sspec from the project dir, the passed agents dir, or the kiro home" — three
different ways in one file.
platform_compat.file_lockasks the same question twice. It computes the Windowslock
ceilingwith a ternary, then one line later runs anif/elif/elseover the same two conditions to pick which
_win_acquire_blockingcall tomake. Two spellings of one decision can drift.
src/kiro_crew/history_projection.pyhas an in-functionimport osin a methodof a module that already imports
osat line 16.Why it matters
Every one of these sites is on a path someone debugs under pressure — a lock
that will not open, a subagent spawn refused for memory, an agent whose skills
did not load. A nested ternary is where a reader mis-reads the branch order and
fixes the wrong arm. The duplicated condition pair in
file_lockis the shapethat ships a bug later: change one chain, forget the other, and the error
message reports a ceiling the code never waited for.
A ternary also hides its arms from the coverage gate. The whole ladder is one
measured statement, so an arm no test ever runs still reads as covered.
What changed (motivation → approach → change)
src/kiro_crew/agent_discovery.pyagent_skill_globsif/elif/elsesrc/kiro_crew/agent_discovery.pyagent_welcome_messageif/elif/elsesrc/kiro_crew/context_blocks.pymeasure_prompt_block_domain(label)helper beside itsrc/kiro_crew/platform_compat.pyfile_lockceilingternary plus an identical chain below itceilingand picks the callsrc/kiro_crew/subagent.pycheck_memory_availableif/elif/elsesrc/kiro_crew/history_projection.pyimport osEvery rewrite keeps the same conditions in the same order, so first-match-wins
precedence and short-circuiting are unchanged and no call the original skipped is
now evaluated.
agent_discovery's third spelling,agents_dir or _kiro_agents_dir()in_all_agent_specs, is left exactly as it is. It is a falsiness test, not anis not Nonetest, so folding it into the other two would change what a falsynon-
Noneagents_dirresolves to. Two of three now match; the third differsfor a reason.
The
context_blockssite sits inside a dict comprehension, where a statementchain cannot be written, so it moves into a small module-level helper.
measure_promptreturns the same object it did before, key for key.The deleted
import osre-bound the module-scopeosto the samesys.modulesobject, so removing it is a no-op for behavior. It is a plainimport X, not afrom X import Y. One property does change: the method'sos.write/os.close/os.unlinknow read the module global, sosetattr(history_projection, "os", …)would reach them where before it couldnot. Nothing in the suite patches that name on this module. The sibling
import tempfilehas no module-scope binding and stays.No keystone security file is reshaped.
safety_override.pycarries threeternaries in this module and all three are deliberately left alone.
Backwards compatibility
Compatible: no signature, return type, return value, config key or error message
changes.
measure_prompt's dict andcheck_memory_available's tuple areidentical on every input, verified by running the same probe against
origin/mainand this head.Tests
Flattening turns one measured statement into three to ten, and five of the new
arms turned out to have no test at all — the ternary had been hiding that. Six
tests close the gap, each pinning a real contract rather than a line number:
TestDomainGrouping(3 tests) intest/metrics/test_context_blocks.pydomainvalues from one realistic prompt, and that a recognised block likememoryisbackgroundbecause it is none of the four named cases — not because its label went unrecognisedtest_a_global_agent_is_reopened_from_the_passed_agents_diragents_dir, not the machine's real kiro home; differential, since the spec exists only theretest_file_lock_not_waiting_reports_a_held_lock_as_blockingwait=FalseraisesBlockingIOError, not the stuck-holderOSError— only the first is safe to retrytest_file_lock_names_the_callers_own_timeout_in_the_refusaltest_startup_memory_guard_admits_a_host_with_no_native_readerMeasured per-file coverage,
origin/mainversus this head on an identical testselection, shows no file regressing and three improving:
src/kiro_crew/context_blocks.pysrc/kiro_crew/agent_discovery.pysrc/kiro_crew/platform_compat.pysrc/kiro_crew/history_projection.pycontext_blocks.pygains ten measured statements and misses none of them.The existing tests over the rewritten paths also pass unchanged:
test_context_management_doc.py,test_default_context_admission.py,test_agent_discovery.py,test_agent_discovery_pathres.py,test_history_projection_row_shape.py,test_history.py,test_platform_compat.py,test_flock_compat.py,test_host_available_memory.py,test_subagent.py,test_subagent_sizing.pyand
test_subagent_macos_probe.py— 1750 passed, 26 skipped, 0 failures.Manual verification
N/A — unit coverage sufficient: the change is behavior-preserving, and
equivalence was checked by running the same probe against a pristine
origin/mainworktree and this head and comparing output.Gates run on the Python 3.12 CI-parity venv, all exit 0:
check_black_formatting,isort --check-only,flake8,mypy(1706 files),check_subprocess_encoding,check_brand_name,check_harness_parity,check_comment_history(with a base ref),check_sync_io_in_async,docs_lint --test,docs-lint.sh,check_per_file_coverage --test,verify_vendor_manifest.Seven read-only review agents ran against the diff before this PR opened: one
each on AUTOSDE blocking rules, behavior equivalence, import semantics, the
security keystone and boot path, and comment accuracy, then two adversarial
passes told to refute the clean verdicts. Those last two found the two real
problems this diff now fixes — the newly-measurable uncovered arms, and the
agent_discoverysibling left in ternary form.Screenshots / video
Why no screenshot: backend-only diff — no file under
website/is touched,and
measure_prompt, which feeds the dashboard's context breakdown, returns thesame object it did before, so nothing rendered changes.
Related Issues
no linked issue: this is a scheduled readability sweep of one module, not a
reported defect.
Checklist
that point at this code name
measure_prompt,agent_skill_globsandproject_agents_dir, and all three keep their names and behavior