Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Seanaye/feat/replay missed socket messages (#4500)

Sean Aye7e90b931dc81
diff --git a/js/app/package.json b/js/app/package.json
index ea7eae1256..03cd9c0e48 100644
--- a/js/app/package.json
+++ b/js/app/package.json
@@ -79,7 +79,6 @@
"@tauri-apps/plugin-http": "~2.5.8",
"@tauri-apps/plugin-notification": "~2",
"@tauri-apps/plugin-os": "~2",
- "@tauri-apps/plugin-websocket": "~2",
"@thisbeyond/solid-dnd": "^0.7.5",
"@urql/core": "^6.0.3",
"@vitest/ui": "^4.0.17",
diff --git a/js/app/packages/websocket/platform/tauri-websocket.ts b/js/app/packages/websocket/platform/tauri-websocket.ts
index f66ac9a773..989a220af9 100644
--- a/js/app/packages/websocket/platform/tauri-websocket.ts
+++ b/js/app/packages/websocket/platform/tauri-websocket.ts
@@ -1,16 +1,60 @@
import { isTauri } from '@core/util/platform';
import { Channel, invoke } from '@tauri-apps/api/core';
-import TauriWebsocket, {
- type Message as TauriMessage,
-} from '@tauri-apps/plugin-websocket';
import { match } from 'ts-pattern';
import type { MinimalWebSocket, WebSocketFactory } from './minimal-websocket';
+// This wrapper talks to the tauri websocket plugin's IPC surface directly
+// instead of going through @tauri-apps/plugin-websocket's JS glue:
+// - connect: we must pre-register our message handler BEFORE the connection
+// is made, which the stock wrapper does not allow
+// (https://github.com/tauri-apps/plugins-workspace/issues/3152)
+// - recover: only exists in our fork
+// The types below mirror the crate pinned in tauri/src-tauri/Cargo.toml
+// (github.com/seanaye/plugins-workspace, branch seanaye/feat/ws-replay-buffer,
+// plugins/websocket/src/lib.rs) and must be kept in sync with it.
+
+interface TauriMessageKind<T, D> {
+ type: T;
+ data: D;
+}
+
+interface TauriCloseFrame {
+ code: number;
+ reason: string;
+}
+
+type TauriMessage =
+ | TauriMessageKind<'Text', string>
+ | TauriMessageKind<'Binary', number[]>
+ | TauriMessageKind<'Ping', number[]>
+ | TauriMessageKind<'Pong', number[]>
+ | TauriMessageKind<'Close', TauriCloseFrame | null>;
+
+/**
+ * Envelope emitted by our fork of the websocket plugin
+ * (seanaye/feat/ws-replay-buffer). Every incoming message carries a
+ * per-connection sequence number so missed messages can be recovered after
+ * the webview was suspended.
+ */
+interface ReplayEnvelope {
+ seq: number;
+ message: TauriMessage;
+}
+
+interface RecoverResponse {
+ messages: ReplayEnvelope[];
+ /**
+ * True when messages newer than `after` were already evicted from the
+ * Rust-side replay buffer; the connection must be resynced.
+ */
+ gap: boolean;
+}
+
/**
* Tauri WebSocket wrapper that implements MinimalWebSocket interface
*/
class TauriWebSocketWrapper implements MinimalWebSocket {
- private ws: TauriWebsocket; // Tauri WebSocket instance
+ private socketId: number | null = null; // plugin connection id
private _readyState: number = 0; // CONNECTING
private _url: string;
private _protocol: string = '';
@@ -27,6 +71,13 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
private eventListeners: Map<string, Set<EventListener>> = new Map();
private removeListener?: () => void;
+ // Replay/recovery state (see seanaye/feat/ws-replay-buffer)
+ private lastSeq = 0;
+ private recovering = false;
+ private queued: ReplayEnvelope[] = [];
+ private deliver?: (envelope: ReplayEnvelope) => void;
+ private visibilityHandler?: () => void;
+
// WebSocket state constants
readonly CONNECTING: 0 = 0;
readonly OPEN: 1 = 1;
@@ -48,11 +99,9 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
// forward `protocols` to the plugin connect call.
void protocols;
- // Workaround for https://github.com/tauri-apps/plugins-workspace/issues/3152:
- // TauriWebsocket.connect() only exposes the listeners Set after it resolves, so
- // messages sent by the server immediately after the handshake can arrive via IPC
- // before addListener() is called and get dropped. Instead, we call invoke()
- // directly and pre-register our handler BEFORE the connection is made.
+ // We call invoke() directly and pre-register our handler BEFORE the
+ // connection is made, so messages sent by the server immediately after
+ // the handshake cannot be dropped (see module comment above).
const listeners = new Set<(message: TauriMessage) => void>();
const handleMessage = (message: TauriMessage) => {
@@ -90,32 +139,58 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
// is dispatched to our handler rather than dropped into an empty Set.
listeners.add(handleMessage);
- const onMessage = new Channel<TauriMessage>();
- onMessage.onmessage = (message: TauriMessage) => {
+ // Unwrap the { seq, message } replay envelope, deduplicating messages
+ // that are delivered both live and via recover().
+ //
+ // Note: the live path can never present a forward gap (seq > lastSeq + 1
+ // with the hole unfilled). Tauri's Channel delivers messages strictly
+ // in-order (out-of-order arrivals are buffered internally), and
+ // recover() verifies contiguity Rust-side via the `gap` flag. Lost
+ // channel messages stall inside the Channel instead, which is exactly
+ // what recover() compensates for.
+ const deliver = (envelope: ReplayEnvelope) => {
+ if (envelope.seq > 0) {
+ if (envelope.seq <= this.lastSeq) {
+ return;
+ }
+ this.lastSeq = envelope.seq;
+ }
listeners.forEach((l) => {
- l(message);
+ l(envelope.message);
});
};
+ this.deliver = deliver;
+
+ const onMessage = new Channel<ReplayEnvelope>();
+ onMessage.onmessage = (envelope: ReplayEnvelope) => {
+ if (this.recovering) {
+ this.queued.push(envelope);
+ } else {
+ deliver(envelope);
+ }
+ };
const id = await invoke<number>('plugin:websocket|connect', {
url,
onMessage,
});
-
- // Reconstruct a TauriWebsocket instance from the connection id and our
- // pre-populated listeners Set, matching the internal shape of the plugin class.
- this.ws = new (
- TauriWebsocket as unknown as new (
- id: number,
- listeners: Set<(arg: TauriMessage) => void>
- ) => TauriWebsocket
- )(id, listeners);
+ this.socketId = id;
this.removeListener = () => {
listeners.delete(handleMessage);
};
this._readyState = this.OPEN;
+
+ // Recover messages received by the Rust side while the webview was
+ // suspended (e.g. iOS backgrounding) whenever we become visible again.
+ this.visibilityHandler = () => {
+ if (document.visibilityState === 'visible') {
+ void this.recover();
+ }
+ };
+ document.addEventListener('visibilitychange', this.visibilityHandler);
+
console.log(`initialized tauri websocket for ${url}`);
// Trigger open event
const openEvent = new Event('open');
@@ -152,12 +227,82 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
}
private handleClose(event: CloseEvent) {
+ this.teardownVisibilityListener();
if (this.onclose) {
this.onclose.call(this as any, event);
}
this.dispatchToEventListeners('close', event);
}
+ private teardownVisibilityListener() {
+ if (this.visibilityHandler) {
+ document.removeEventListener('visibilitychange', this.visibilityHandler);
+ this.visibilityHandler = undefined;
+ }
+ }
+
+ /**
+ * Requests redelivery of messages that were received by the Rust side but
+ * did not reach this webview. If the replay buffer no longer covers the
+ * missed range (gap), the connection is force-closed so the upper layer
+ * reconnects and resyncs.
+ */
+ private async recover(): Promise<void> {
+ if (
+ this.socketId === null ||
+ this._readyState !== this.OPEN ||
+ this.recovering
+ ) {
+ return;
+ }
+ this.recovering = true;
+ // Set when missed messages cannot be recovered: either the replay buffer
+ // no longer covers them (gap) or the recover call itself failed. In both
+ // cases we must not continue with a hole in the stream.
+ let needsResync = false;
+ try {
+ const res = await invoke<RecoverResponse>('plugin:websocket|recover', {
+ id: this.socketId,
+ after: this.lastSeq,
+ });
+ for (const envelope of res.messages) {
+ this.deliver?.(envelope);
+ }
+ needsResync = res.gap;
+ } catch (error) {
+ console.error(`Tauri WebSocket recover failed for ${this._url}:`, error);
+ needsResync = true;
+ } finally {
+ const queued = this.queued;
+ this.queued = [];
+ this.recovering = false;
+ if (!needsResync) {
+ // flush messages that arrived live while we were recovering
+ for (const envelope of queued) {
+ this.deliver?.(envelope);
+ }
+ }
+ // when resyncing, the queued envelopes are dropped: the forced
+ // reconnect below triggers a full resync that covers them
+ }
+ if (needsResync) {
+ console.warn(
+ `Tauri WebSocket ${this._url}: unrecoverable missed messages, forcing reconnect`
+ );
+ this.disconnectSocket().catch(() => {});
+ this._readyState = this.CLOSED;
+ this.removeListener?.();
+ this.handleError(new Event('error'));
+ this.handleClose(
+ new CloseEvent('close', {
+ code: 1006,
+ reason: 'Replay gap detected',
+ wasClean: false,
+ })
+ );
+ }
+ }
+
private handleError(event: Event) {
if (this.onerror) {
this.onerror.call(this as any, event);
@@ -236,13 +381,31 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
return true;
}
+ /** Sends a message over the plugin connection. */
+ private async sendMessage(message: TauriMessage): Promise<void> {
+ await invoke('plugin:websocket|send', {
+ id: this.socketId,
+ message,
+ });
+ }
+
+ /** Closes the plugin connection with a normal-closure frame. */
+ private async disconnectSocket(): Promise<void> {
+ await this.sendMessage({
+ type: 'Close',
+ data: {
+ code: 1000,
+ reason: 'Disconnected by client',
+ },
+ });
+ }
+
close(_code?: number, _reason?: string): void {
- if (this.ws && this._readyState === this.OPEN) {
+ if (this.socketId !== null && this._readyState === this.OPEN) {
this._readyState = this.CLOSING;
// Tauri WebSocket disconnect doesn't take parameters, so we just disconnect
- this.ws
- .disconnect()
+ this.disconnectSocket()
.catch((error: any) => {
console.error('Error closing Tauri WebSocket:', error);
})
@@ -272,7 +435,7 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
}
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
- if (!this.ws || this._readyState !== this.OPEN) {
+ if (this.socketId === null || this._readyState !== this.OPEN) {
throw new Error(
`Websocket to: ${this.url} is in state ${this._readyState}`
);
@@ -280,9 +443,9 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
if (typeof data === 'string') {
// Send as text message
- this.ws
- .send({ type: 'Text', data })
- .catch((e: unknown) => this.handleSendRejection(e));
+ this.sendMessage({ type: 'Text', data }).catch((e: unknown) =>
+ this.handleSendRejection(e)
+ );
} else {
// Convert binary data to number array for Tauri
let uint8Array: Uint8Array;
@@ -313,9 +476,9 @@ class TauriWebSocketWrapper implements MinimalWebSocket {
}
// Send as binary message with number array
- this.ws
- .send({ type: 'Binary', data: Array.from(uint8Array) })
- .catch((e: unknown) => this.handleSendRejection(e));
+ this.sendMessage({ type: 'Binary', data: Array.from(uint8Array) }).catch(
+ (e: unknown) => this.handleSendRejection(e)
+ );
}
}
}
diff --git a/js/app/tauri/Cargo.lock b/js/app/tauri/Cargo.lock
index 82beeb87d2..0e9699efb2 100644
--- a/js/app/tauri/Cargo.lock
+++ b/js/app/tauri/Cargo.lock
@@ -2259,7 +2259,7 @@ dependencies = [
"js-sys",
"log",
"wasm-bindgen",
- "windows-core 0.61.2",
+ "windows-core 0.58.0",
]
[[package]]
@@ -5474,8 +5474,8 @@ dependencies = [
[[package]]
name = "tauri-plugin-fs"
-version = "2.5.0"
-source = "git+https://github.com/seanaye/plugins-workspace?branch=seanaye%2Ffeat%2Fwebsocket-cookies#c23e1d7b24391a79b5bcfc3df535452c17f1f01c"
+version = "2.5.1"
+source = "git+https://github.com/seanaye/plugins-workspace?rev=65794e6229a3e109c14d81d678d86c7802ada304#65794e6229a3e109c14d81d678d86c7802ada304"
dependencies = [
"anyhow",
"dunce",
@@ -5491,7 +5491,7 @@ dependencies = [
"tauri-plugin",
"tauri-utils",
"thiserror 2.0.18",
- "toml 0.9.12+spec-1.1.0",
+ "toml 1.1.2+spec-1.1.0",
"url",
]
@@ -5511,8 +5511,8 @@ dependencies = [
[[package]]
name = "tauri-plugin-http"
-version = "2.5.8"
-source = "git+https://github.com/seanaye/plugins-workspace?branch=seanaye%2Ffeat%2Fwebsocket-cookies#c23e1d7b24391a79b5bcfc3df535452c17f1f01c"
+version = "2.5.9"
+source = "git+https://github.com/seanaye/plugins-workspace?rev=65794e6229a3e109c14d81d678d86c7802ada304#65794e6229a3e109c14d81d678d86c7802ada304"
dependencies = [
"bytes",
"cookie_store",
@@ -5695,7 +5695,7 @@ dependencies = [
[[package]]
name = "tauri-plugin-websocket"
version = "2.4.2"
-source = "git+https://github.com/seanaye/plugins-workspace?branch=seanaye%2Ffeat%2Fwebsocket-cookies#c23e1d7b24391a79b5bcfc3df535452c17f1f01c"
+source = "git+https://github.com/seanaye/plugins-workspace?rev=65794e6229a3e109c14d81d678d86c7802ada304#65794e6229a3e109c14d81d678d86c7802ada304"
dependencies = [
"futures-util",
"http",
@@ -6012,9 +6012,9 @@ dependencies = [
[[package]]
name = "tokio-tungstenite"
-version = "0.28.0"
+version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
+checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c"
dependencies = [
"futures-util",
"log",
@@ -6313,9 +6313,9 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "tungstenite"
-version = "0.28.0"
+version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442"
+checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8"
dependencies = [
"bytes",
"data-encoding",
@@ -6327,7 +6327,6 @@ dependencies = [
"rustls-pki-types",
"sha1",
"thiserror 2.0.18",
- "utf-8",
]
[[package]]
diff --git a/js/app/tauri/src-tauri/Cargo.toml b/js/app/tauri/src-tauri/Cargo.toml
index bd21cb4278..e705722aa2 100644
--- a/js/app/tauri/src-tauri/Cargo.toml
+++ b/js/app/tauri/src-tauri/Cargo.toml
@@ -10,7 +10,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
-crate-type = ["staticlib", "rlib"]
+crate-type = ["rlib", "staticlib"]
name = "app_lib"
[build-dependencies]
@@ -37,17 +37,20 @@ tracing-oslog = { version = "0.3" }
nom = "8.0.0"
reqwest = { workspace = true, features = ["cookies", "stream"] }
-tokio-util = { version = "0.7", features = ["io"] }
serde_qs = "0.15.0"
tauri = { workspace = true, features = ["devtools", "protocol-asset"] }
tauri-plugin-deep-link = "=2.4.5"
-tauri-plugin-http = { git = "https://github.com/seanaye/plugins-workspace", branch = "seanaye/feat/websocket-cookies", features = [
+tokio-util = { version = "0.7", features = ["io"] }
+# seanaye/feat/ws-replay-buffer; keep both plugin revs identical, and keep the
+# TauriMessage/ReplayEnvelope types in packages/websocket/platform/tauri-websocket.ts
+# in sync when bumping
+tauri-plugin-http = { git = "https://github.com/macro-inc/plugins-workspace", rev = "a1351a7c8c9f4dfe9699badd940cfc5afdb68e36", features = [
"unsafe-headers",
] }
tauri-plugin-log = { version = "2", features = ["tracing"] }
tauri-plugin-opener = "2"
tauri-plugin-safe-area-insets = "0.1.0"
-tauri-plugin-websocket = { git = "https://github.com/seanaye/plugins-workspace", branch = "seanaye/feat/websocket-cookies" }
+tauri-plugin-websocket = { git = "https://github.com/macro-inc/plugins-workspace", rev = "a1351a7c8c9f4dfe9699badd940cfc5afdb68e36" }
thiserror = "2.0.14"
tokio = { version = "1.47.1", features = ["full"] }
tracing = { workspace = true }
diff --git a/js/bun.lock b/js/bun.lock
index 26f0f49a46..251ebb2759 100644
--- a/js/bun.lock
+++ b/js/bun.lock
@@ -83,7 +83,6 @@
"@tauri-apps/plugin-http": "~2.5.8",
"@tauri-apps/plugin-notification": "~2",
"@tauri-apps/plugin-os": "~2",
- "@tauri-apps/plugin-websocket": "~2",
"@thisbeyond/solid-dnd": "^0.7.5",
"@urql/core": "^6.0.3",
"@vitest/ui": "^4.0.17",
@@ -1641,8 +1640,6 @@
"@tauri-apps/plugin-os": ["@tauri-apps/[email protected]", "", { "dependencies": { "@tauri-apps/api": "^2.8.0" } }, "sha512-n+nXWeuSeF9wcEsSPmRnBEGrRgOy6jjkSU+UVCOV8YUGKb2erhDOxis7IqRXiRVHhY8XMKks00BJ0OAdkpf6+A=="],
- "@tauri-apps/plugin-websocket": ["@tauri-apps/[email protected]", "", { "dependencies": { "@tauri-apps/api": "^2.8.0" } }, "sha512-B1/csKPwJH9kLduNoqWFUdCsTgCvLM84T5ZZYK35jeBc9m2cm2m2G/hbSr28ZpVOZrl0x24fCWFe/9rpp7WETw=="],
-
"@testing-library/dom": ["@testing-library/[email protected]", "", { "dependencies": { "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", "@types/aria-query": "^5.0.1", "aria-query": "5.3.0", "dom-accessibility-api": "^0.5.9", "lz-string": "^1.5.0", "picocolors": "1.1.1", "pretty-format": "^27.0.2" } }, "sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg=="],
"@testing-library/jest-dom": ["@testing-library/[email protected]", "", { "dependencies": { "@adobe/css-tools": "^4.4.0", "aria-query": "^5.0.0", "css.escape": "^1.5.1", "dom-accessibility-api": "^0.6.3", "picocolors": "^1.1.1", "redent": "^3.0.0" } }, "sha512-zIcONa+hVtVSSep9UT3jZ5rizo2BsxgyDYU7WFD5eICBE7no3881HGeb/QkGfsJs6JTkY1aQhT7rIPC7e+0nnA=="],
diff --git a/nix-support/node_modules-hashes.json b/nix-support/node_modules-hashes.json
index 7dc12a8a34..e86945987a 100644
--- a/nix-support/node_modules-hashes.json
+++ b/nix-support/node_modules-hashes.json
@@ -1,6 +1,6 @@
{
"nodeModules": {
"aarch64-darwin": "sha256-HdpGcJzJIaFGDS6sN4Lm4vOWolNcnVroOaJK5iSwraY=",
- "x86_64-linux": "sha256-IMzszY6DOIoX8sJOmRZSdjxGVOsVY6fQGBMID0lymyk="
+ "x86_64-linux": "sha256-SB8/f3ioTZuhizRPPeVHlNOq+hs54YqvwZHLwxDF1fU="
}
-}
+}
\ No newline at end of file
diff --git a/rust/graphql-cache/.gitignore b/rust/graphql-cache/.gitignore
new file mode 100644
index 0000000000..ff85f54087
--- /dev/null
+++ b/rust/graphql-cache/.gitignore
@@ -0,0 +1,2 @@
+# Ignore build output
+/target/