refactor(state): dedupe session-checkpoint helper boilerplate#825
Closed
kelsonpw wants to merge 1 commit into
Closed
refactor(state): dedupe session-checkpoint helper boilerplate#825kelsonpw wants to merge 1 commit into
kelsonpw wants to merge 1 commit into
Conversation
Two small, behavior-preserving cleanups inside session-checkpoint.ts:
1. loadCheckpoint() return — collapse the inline `s && s.trim().length > 0`
guard for `selectedProjectId` into the existing `isNonEmpty()` helper.
Identical semantics; one fewer place that has to know the empty/whitespace
rule.
2. hasProjectBinding() — replace two identical `try { existsSync(x) } catch
{ return true }` blocks with a single loop over a candidates array.
The EACCES/EIO → "present" invariant is preserved (and now documented
inline rather than duplicated).
No checkpoint-load invalidation logic is altered. The #778 companion-file
check (`meaningful selection && !hasProjectBinding → invalidate`) keeps the
same predicates with the same fall-through behavior. Schema, on-disk
format, and 0o600 perm all untouched.
discovered-facts/ and agent-state-hydrate.ts mentioned in the brief don't
exist on main, so they were out of reach this pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
Closing as part of pat-leave handoff to reduce open-PR load on the team. Branch preserved on origin — reopen if specific changes are wanted. The highest-impact wins from this refactor round already merged. |
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.
Summary
Two small, behavior-preserving cleanups inside
src/lib/session-checkpoint.ts. Scope was originallydiscovered-facts/+agent-state*+session-checkpoint, butsrc/lib/discovered-facts/and a standaloneagent-state-hydrate.tsdon't exist onmain(they live onfeat/discovered-facts-vertical-and-app-type), andagent-state.tshad no safe dedup left after a careful read — so this PR is intentionally narrow.What landed
Net lines: +17 / -16 (all in
session-checkpoint.ts). No test changes, no schema changes, no on-disk format changes.Lines touched in
session-checkpoint.tsloadCheckpoint) — collapsed the inlinecheckpoint.selectedProjectId && checkpoint.selectedProjectId.trim().length > 0 ? ... : nullternary intoisNonEmpty(checkpoint.selectedProjectId) ? ... : null.isNonEmptywas already defined at line 348 and used bycheckpointHasMeaningfulSelection. Safe becauseisNonEmpty(value)returnstypeof value === 'string' && value.trim().length > 0— semantically identical for thestring | null | undefinedinput type.hasProjectBinding) — replaced two near-identicaltry { if (existsSync(x)) return true } catch { return true }blocks with a single loop over a 2-element candidates array. The EACCES/EIO → "treat as present" invariant is preserved (and now documented inline rather than duplicated above the function). Result for every input is identical: returnstrueiff either path exists ORexistsSyncitself throws on either path.session-checkpoint.ts safety analysis
Every condition added by the recent fix history is preserved:
checkpointHasMeaningfulSelection(checkpoint) && !hasProjectBinding(installDir)gate at lines 210–224 is byte-identical to before. ThehasProjectBindingrefactor changes shape, not semantics — same two candidate paths checked in the same order, same try/catch → return true behavior.needsEnvPickStillRequired): lives in auth/org-picker code, not in this file. Not touched.pendingOrgsreconciliation, not in this file. Not touched.Checkpoint on-disk format (
version, field names, schema transform legacy reads) and 0o600 mode are all unchanged.loadCheckpoint's null-return predicates (file missing, parse failure, schema failure, >24h age, installDir mismatch, companion-file missing) remain identical in count and order.Patterns deferred
checkpointHasMeaningfulSelection/isNonEmptybetweensession-checkpoint.tsandself-heal.ts— would require touchingself-heal.ts, which is out of scope here.withCacheRoot()test helper foragent-state.test.ts+agent-state-hydrate.test.tsboilerplate — would require a brand-new_helperstest file, which CLAUDE.md guidance discourages without explicit need.buildRetryHint/buildRecoveryNotesection-builder dedup inagent-state.ts— the wording diverges enough that any shared helper would obscure intent. Left alone.discovered-facts/classifier dedup — directory doesn't exist onmain; lives on a feature branch.Tests
session-checkpoint.test.ts+ 9 insession-checkpoint-events.test.ts)Test plan
pnpm tsc --noEmitgreenpnpm lintgreenpnpm testgreen (4473/4473)pnpm exec vitest run --pool=forks --maxWorkers=1 src/lib/__tests__/session-checkpoint*.test.ts src/lib/__tests__/agent-state*.test.tsgreenwizard-abort.tsuntouchedwizard-session.tsuntouchedloadCheckpoint()invalidation conditions preserved🤖 Generated with Claude Code
Note
Low Risk
Low risk: behavior-preserving refactors limited to checkpoint loading and file-existence checks, with no schema, persistence format, or control-flow changes intended.
Overview
Refactors
loadCheckpoint()to normalizeselectedProjectIdvia the sharedisNonEmpty()helper instead of an inline trim/ternary check.Refactors
hasProjectBinding()to iterate over the canonical and legacy binding file paths in a single loop, preserving the existing "existsSync errors count as present" behavior while removing duplicated try/catch blocks.Reviewed by Cursor Bugbot for commit f4090a0. Bugbot is set up for automated code reviews on this repo. Configure here.