-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(mobile): Recover thread state reliably after reconnects #4034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
juliusmarminge:codex/pr3910-reconnect-recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+396
−38
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { | ||
| EMPTY_ENVIRONMENT_THREAD_STATE, | ||
| type EnvironmentThreadState, | ||
| } from "@t3tools/client-runtime/state/threads"; | ||
| import { causeFailureMessage } from "@t3tools/client-runtime/errors"; | ||
| import type * as Cause from "effect/Cause"; | ||
| import * as Option from "effect/Option"; | ||
| import { AsyncResult } from "effect/unstable/reactivity"; | ||
|
|
||
| function formatThreadStateFailure(cause: Cause.Cause<unknown>): string { | ||
| return causeFailureMessage(cause, "Could not load conversation."); | ||
| } | ||
|
|
||
| export function environmentThreadStateFromAsyncResult( | ||
| result: AsyncResult.AsyncResult<EnvironmentThreadState, unknown>, | ||
| ): EnvironmentThreadState { | ||
| const value = Option.getOrNull(AsyncResult.value(result)); | ||
| if (AsyncResult.isFailure(result)) { | ||
| return { | ||
| ...(value ?? EMPTY_ENVIRONMENT_THREAD_STATE), | ||
| error: Option.some(formatThreadStateFailure(result.cause)), | ||
| }; | ||
| } | ||
|
|
||
| return value ?? EMPTY_ENVIRONMENT_THREAD_STATE; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads"; | ||
| import * as Cause from "effect/Cause"; | ||
| import * as Option from "effect/Option"; | ||
| import { AsyncResult } from "effect/unstable/reactivity"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { environmentThreadStateFromAsyncResult } from "./threadQueryState"; | ||
|
|
||
| describe("environmentThreadStateFromAsyncResult", () => { | ||
| it("returns the empty state while the first value is loading", () => { | ||
| expect(environmentThreadStateFromAsyncResult(AsyncResult.initial(true))).toEqual( | ||
| EMPTY_ENVIRONMENT_THREAD_STATE, | ||
| ); | ||
| }); | ||
|
|
||
| it("surfaces a failure when no previous value exists", () => { | ||
| const state = environmentThreadStateFromAsyncResult( | ||
| AsyncResult.failure(Cause.fail("thread sync failed")), | ||
| ); | ||
|
|
||
| expect(Option.getOrNull(state.data)).toBeNull(); | ||
| expect(Option.getOrNull(state.error)).toBe("thread sync failed"); | ||
| }); | ||
|
|
||
| it("preserves previous thread data while surfacing a refresh failure", () => { | ||
| const previousState = { | ||
| ...EMPTY_ENVIRONMENT_THREAD_STATE, | ||
| status: "live" as const, | ||
| }; | ||
| const previousSuccess = AsyncResult.success(previousState); | ||
| const state = environmentThreadStateFromAsyncResult( | ||
| AsyncResult.failure(Cause.fail(new Error("refresh failed")), { | ||
| previousSuccess: Option.some(previousSuccess), | ||
| }), | ||
| ); | ||
|
|
||
| expect(state.status).toBe("live"); | ||
| expect(Option.getOrNull(state.error)).toBe("refresh failed"); | ||
| }); | ||
| }); |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import * as Cause from "effect/Cause"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { causeFailureMessage } from "./causeMessage.ts"; | ||
|
|
||
| describe("causeFailureMessage", () => { | ||
| it("returns the message of an Error failure", () => { | ||
| expect(causeFailureMessage(Cause.fail(new Error("boom")), "fallback")).toBe("boom"); | ||
| }); | ||
|
|
||
| it("returns a string failure verbatim", () => { | ||
| expect(causeFailureMessage(Cause.fail("nope"), "fallback")).toBe("nope"); | ||
| }); | ||
|
|
||
| it("returns the message of a tagged error object", () => { | ||
| expect( | ||
| causeFailureMessage( | ||
| Cause.fail({ _tag: "RelayInternalError", message: "relay down" }), | ||
| "fallback", | ||
| ), | ||
| ).toBe("relay down"); | ||
| }); | ||
|
|
||
| it("falls back for empty Error messages", () => { | ||
| expect(causeFailureMessage(Cause.fail(new Error(" ")), "fallback")).toBe("fallback"); | ||
| }); | ||
|
|
||
| it("falls back for empty string failures", () => { | ||
| expect(causeFailureMessage(Cause.fail(""), "fallback")).toBe("fallback"); | ||
| }); | ||
|
|
||
| it("falls back for objects without a string message", () => { | ||
| expect(causeFailureMessage(Cause.fail({ code: 500 }), "fallback")).toBe("fallback"); | ||
| expect(causeFailureMessage(Cause.fail({ message: 42 }), "fallback")).toBe("fallback"); | ||
| }); | ||
|
|
||
| it("falls back for other primitive failures", () => { | ||
| expect(causeFailureMessage(Cause.fail(null), "fallback")).toBe("fallback"); | ||
| expect(causeFailureMessage(Cause.fail(42), "fallback")).toBe("fallback"); | ||
| }); | ||
|
|
||
| it("returns the defect message for die causes", () => { | ||
| expect(causeFailureMessage(Cause.die(new Error("defect")), "fallback")).toBe("defect"); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import * as Cause from "effect/Cause"; | ||
|
|
||
| export function causeFailureMessage(cause: Cause.Cause<unknown>, fallback: string): string { | ||
| const message = failureMessage(Cause.squash(cause)); | ||
| return message !== null && message.trim().length > 0 ? message : fallback; | ||
| } | ||
|
|
||
| export function failureMessage(error: unknown): string | null { | ||
| if (error instanceof Error) { | ||
| return error.message; | ||
| } | ||
| if (typeof error === "string") { | ||
| return error; | ||
| } | ||
| if (typeof error === "object" && error !== null && "message" in error) { | ||
| return typeof error.message === "string" ? error.message : null; | ||
| } | ||
| return null; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from "./causeMessage.ts"; | ||
| export * from "./errorTrace.ts"; | ||
| export * from "./safeLog.ts"; | ||
| export * from "./transport.ts"; |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.