FE-1226: Split the optimizations provider into policy, errors, and storage modules - #9151
Draft
kube wants to merge 1 commit into
Draft
FE-1226: Split the optimizations provider into policy, errors, and storage modules#9151kube wants to merge 1 commit into
kube wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
Base automatically changed from
cf/fe-1226-petrinaut-optimization-reconnect
to
main
August 4, 2026 07:16
…orage modules The detached-run provider had grown to 874 lines, half of them framework-free logic sitting next to the React state it happens to be used by, and a 258-line attach loop whose failure handling interleaved policy with effects. - `reconnect-policy.ts`: the backoff constants and delay, plus a pure `decideAttachFailure` that maps a failure to one of cancelled / settled / expired / reconnect / giveUp. The loop's 75-line `catch` cascade becomes a switch over that decision, and the policy is now unit-testable directly. - `transport-errors.ts`: duck-typed error classification and message building, plus a single `isAbortFailure` predicate replacing the three-part abort check that run creation and the attach loop each had their own copy of. - `active-run-storage.ts`: the sessionStorage bookkeeping for runs a reload may re-attach to. Its key is now used by the tests instead of a literal repeated eight times. Pure refactor: the 17 existing provider tests pass unchanged, and 10 new tests cover the extracted decision function's ordering and cap. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
kube
force-pushed
the
cf/fe-1226-provider-simplification
branch
from
August 4, 2026 08:23
e84d4e9 to
ffd2115
Compare
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 is the purpose of this PR?
Make the detached-run optimization provider readable. #9066 left it at 874 lines: about half was framework-free logic that happened to live next to the React state using it, and its attach loop was a single 258-line callback whose failure handling interleaved reconnect policy with state effects.
This is a pure refactor — no behavioural change, no public API change. The 17 provider tests from #9066 pass untouched, which is the guarantee that nothing moved semantically.
🔗 Related links
🚫 Blocked by
🔍 What does this change?
Three modules extracted from
provider.tsx, all internal (nothing is added to the@hashintel/petrinaut/reactentry point):reconnect-policy.ts— the backoff constants,reconnectDelayMs,abortableDelay, and a new puredecideAttachFailurethat maps one failure tocancelled/settled/expired/reconnect/giveUp. The attach loop's 75-linecatchcascade — six sequentialifs, each ending in areturn, with the reconnect predicate computed inline in the middle — becomes aswitchover that decision. The policy's ordering constraints are now stated where the policy lives, and testable without driving a provider through fake streams.transport-errors.ts— the duck-typedclassifyError,buildErrorMessage, andisAbortError, plus one newisAbortFailurepredicate. Run creation and the attach loop each carried their own copy of the same three-part abort check (signal.aborted || isAbortError(error) || classified?.category === "aborted"); they now share one, so they cannot drift.active-run-storage.ts— the sessionStorage bookkeeping for runs a reload may re-attach to.ACTIVE_RUNS_STORAGE_KEYis now imported by the tests instead of the string literal being repeated eight times.Net effect:
provider.tsx874 → 656 lines, andrunAttachLoop258 → 163.Also corrected in passing: the comment on
computeRunningBestclaimed the service "no longer knows the objective direction after the creating request ends". That was true of the old connection-scoped design; with detached runs the Optuna study — and its direction — lives for the run's lifetime. The reason the provider computes the running best is that cursors make any single attachment partial, not that the service has forgotten anything.Pre-Merge Checklist 🚀
🚢 Has this modified a publishable library?
This PR:
📜 Does this require a change to the docs?
The changes in this PR:
🕸️ Does this require a change to the Turbo Graph?
The changes in this PR:
One item from the review that produced this PR is deliberately not done:
createOptimizationand the reload re-attach effect duplicate ~20 lines of lifecycle (mint id →AbortController→ register in two refs → prepend record → run the loop → clear both refs). Merging them needs a nullable-runIdbranch, because creation only learns its run id after an await while re-attach knows it upfront — the resulting helper reads worse than the duplication. Left as is.🛡 What tests cover this?
reconnect-policy.test.tspin the decision function: the backoff sequence and its cap, which categories reconnect, that a definitivehttpstatus never does, that cancellation outranks every other outcome, that a failure after a terminal event issettled, and that a 404 only meansexpiredon a stored run's first attachment.26 test files / 197 tests green in
@hashintel/petrinaut; eslint and oxfmt clean.❓ How to test this?
cd libs/@hashintel/petrinaut && yarn vitest --run src/react/optimizationsgit diffonprovider.tsxis only deletions plus the rewired imports and theswitch.🤖 Generated with Claude Code