enhance(#3267): reduce handleEvaluate complexity below refactor-trigger's own threshold (#3978)

* fix(#3267): reduce handleEvaluate complexity below refactor-trigger's own threshold

handleEvaluate scored 26 (then 21 after later #3261 commits) against the
complexity-triggered-refactor feature's own default threshold of 15. Extracts
the read-and-analyze loop (analyzeTouchedFiles) and the artifact/baseline/
ledger write path (finalizeEvaluation) into named helpers, per the issue's
suggested direction. Behavior-preserving: every existing test in
tests/refactor-trigger-cli.test.cjs is unchanged, and every degrade-path
reason code (REFACTOR_INVALID_PHASE, REFACTOR_GIT_UNAVAILABLE,
REFACTOR_NO_TOUCHED_FILES, REFACTOR_FILE_UNREADABLE,
REFACTOR_ANALYZER_UNSUPPORTED, REFACTOR_ANALYZER_UNPARSEABLE,
REFACTOR_BASELINE_WRITE_FAILED, REFACTOR_STRICT_NOT_ENFORCING) keeps its
current value and emission path.

The four complexity-trigger.cts lexer functions (scanFunctions,
stripLiterals, skipTypeExpr, skipGenericParamList) are deliberately left
untouched, per ADR-1953 D5 — they are a hand-rolled lexer state machine,
densely branchy by construction, and refactoring them to lower the metric
would be exactly the "split a coherent function to satisfy a metric"
behavior D5 exists to prevent.

Closes #3267

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* docs: backfill changeset PR number for #3978

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-08-28 08:12:27 -04:00
committed by GitHub
parent d24e22b156
commit 4f32209f78
3 changed files with 130 additions and 55 deletions

View File

@@ -41,6 +41,8 @@ const {
VERDICT,
REASON,
PROPOSAL_SUFFIX,
DEFAULTS,
analyzeSource,
} = require('../gsd-core/bin/lib/complexity-trigger.cjs');
const gitBaseBranch = require('../gsd-core/bin/lib/git-base-branch.cjs');
const windowsModule = require('../gsd-core/bin/lib/broken-windows.cjs');
@@ -979,3 +981,20 @@ describe('refactor-trigger: loop wiring', () => {
}
});
});
// ─── Regression: handleEvaluate self-complexity (#3267) ────────────────────
describe('refactor-trigger-command-router: self-complexity', () => {
test('handleEvaluateStaysAtOrBelowThreshold', () => {
const routerPath = path.join(__dirname, '..', 'src', 'refactor-trigger-command-router.cts');
const source = fs.readFileSync(routerPath, 'utf8');
const result = analyzeSource(source);
assert.strictEqual(result.ok, true, 'router source must analyze cleanly');
const handleEvaluateFn = result.functions.find((f) => f.name === 'handleEvaluate');
assert.ok(handleEvaluateFn, 'handleEvaluate must still be found by the analyzer');
assert.ok(
handleEvaluateFn.score <= DEFAULTS.threshold,
`handleEvaluate scores ${handleEvaluateFn.score}, must be <= ${DEFAULTS.threshold} (refactor.complexity_threshold)`,
);
});
});