Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ CI runs `pnpm lint` and `pnpm test` on every PR.

The build emits absolute URLs (`/assets/…`, `/config.json`, `/manifest.json`), so the same `index.html` works at any client-side route the server falls back to it for, such as the journal's `/u/<name>/<n>` tracker links. To serve `webapp/` from a sub-path instead of the root, set `PUBLIC_PATH` when building, e.g. `PUBLIC_PATH=/chat/ pnpm build`.

The service worker is emitted as `<PUBLIC_PATH>sw.js` and registers only when that URL is served from the app’s own origin with a JavaScript MIME type. Serve it with a short cache policy, for example `Cache-Control: no-cache`, to avoid delaying worker updates. Web Push requires HTTPS and a VAPID key configured in the journal; see the journal README for setup.

`config.json` is copied into the build output, so edit it before `pnpm build` (or edit `webapp/config.json` after). `config.sample.json` shows the full key set; see [docs/config.md](docs/config.md) for the complete runtime configuration surface.

## License
Expand Down
2 changes: 1 addition & 1 deletion scripts/perf/budget.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"max": 304
},
"totalGzipKB": {
"max": 858
"max": 860
}
},
"cold": {
Expand Down
8 changes: 8 additions & 0 deletions src/journal/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,14 @@ export class JournalApi {
return this.json<unknown>("/notify");
}

public async webPushVapidKey(): Promise<unknown> {
return this.json<unknown>("/push/web/vapid-key");
}

public async registerWebPush(body: Record<string, unknown>): Promise<unknown> {
return this.json<unknown>("/push/web/register", { method: "POST", body });
}

/**
* PUT /notify — changes any part of the settings: `mode`, `events`, this device's
* `device_level`, or one conversation's `convo` level or mute (an explicit null clears it).
Expand Down
77 changes: 74 additions & 3 deletions src/journal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import type { Contact, ContactInvite, InviteAskAnswer, InviteAskDecision, InviteOpened } from "./contacts";
import { shareableInviteLink } from "./contact-link";
import { JournalConnection } from "./connection";
import { viewingConvoId, watchTabFocus } from "./viewing";
import { effectiveUnread, makeIdSetStore, type IdSetStore } from "./conversation-flags";
import { parseCoordinatorConsent, resetSessionStops, spawnConsentRowsIn } from "./coordinator-consent";
import { JournalDatabase } from "./database";
Expand Down Expand Up @@ -625,6 +626,9 @@ export class MatronJournalClient {
private globalSearchAbort?: AbortController;
private database?: JournalDatabase;
private connection?: JournalConnection;
private stopWatchingFocus?: () => void;
private lastViewing?: string | null;
private suspendGuard?: () => boolean;
// Read state: which timeline rows and tracker items were actually on screen, reported as
// `seen` / `item_seen` over the socket. Separate from read_marker and the unread badge.
public readonly seen = new SeenTracker({ send: (op) => this.connection?.send(op) ?? false });
Expand Down Expand Up @@ -822,6 +826,14 @@ export class MatronJournalClient {
return this.sessionGen;
}

/**
* Web push supplies a guard that returns true once a subscription is registered for the session.
* Without a guard, the socket is never closed while hidden.
*/
public setSuspendGuard(guard: (() => boolean) | undefined): void {
this.suspendGuard = guard;
}

private allStorageHealthy(): boolean {
return Object.values(this.storeHydrated).every(Boolean) && Object.values(this.storeWritable).every(Boolean);
}
Expand Down Expand Up @@ -1015,6 +1027,8 @@ export class MatronJournalClient {
this.pendingFiles.clear();
this.transientAttachmentErrors.clear();
this.seen.reset();
this.stopWatchingFocus?.();
this.stopWatchingFocus = undefined;
this.connection?.stop();
this.connection = undefined;
if (this.storageListener) {
Expand Down Expand Up @@ -1414,7 +1428,7 @@ export class MatronJournalClient {
await history;
return;
}
this.connection?.send({ op: "viewing", convo_id: conversationId });
this.sendViewing(conversationId);

const conversation = this.state.conversations.find((candidate) => candidate.id === conversationId);
if (conversation?.unread_count) this.scheduleRead(conversationId, conversation.last_seq, 0);
Expand All @@ -1426,7 +1440,7 @@ export class MatronJournalClient {
// Find in chat belongs to the conversation being left: a search still in flight must not
// reopen it, nor answer a bar opened there later.
this.endChatSearch();
this.connection?.send({ op: "viewing", convo_id: null });
this.sendViewing(null);
if (this.state.session) storeSelectedConversation(this.state.session, undefined);
this.patch({ selectedConversationId: undefined, events: [], pendingMessages: [] });
}
Expand Down Expand Up @@ -2731,6 +2745,7 @@ export class MatronJournalClient {
// user has since left.
this.focusToken += 1;
this.patch({ trackerView: next, ...(this.state.focusTarget ? { focusTarget: undefined } : {}) });
this.sendViewing();
}

// Memories view entry points: a name opens that memory's editor, "" opens the new-memory form,
Expand All @@ -2755,6 +2770,7 @@ export class MatronJournalClient {
this.trackerMissionGen += 1;
this.trackerProjectGen += 1;
this.patch({ trackerView: undefined, trackerItem: null, trackerMission: null, trackerProject: null });
this.sendViewing();
}

// Deep-link / row-tap entry points: select the row (last-tap-wins) and switch to its view.
Expand Down Expand Up @@ -5261,6 +5277,8 @@ export class MatronJournalClient {
this.dismissedAttachments.clear();
this.pendingFiles.clear();
this.transientAttachmentErrors.clear();
this.stopWatchingFocus?.();
this.stopWatchingFocus = undefined;
this.connection?.stop();
this.database?.close();
this.defaultsServer = undefined;
Expand Down Expand Up @@ -5457,7 +5475,9 @@ export class MatronJournalClient {
// journal, and an unbound revoke would sign the new account out.
const connectionGen = this.sessionGen;
const current = (): boolean => this.sessionGen === connectionGen;
this.lastViewing = undefined;
this.connection = new JournalConnection(session.serverUrl, session.token, {
canSuspend: () => this.suspendGuard?.() ?? false,
cursor: async () => (await this.database?.cursor()) ?? cursor ?? 0,
archiveSince: () => readArchiveSince(session) ?? 0,
onFrame: async (frame) => {
Expand All @@ -5478,7 +5498,11 @@ export class MatronJournalClient {
onState: (connection, error) => {
if (current()) this.patch({ connection, connectionError: error });
},
onSuspend: () => {
if (current()) this.suspendLiveStreams();
},
});
this.stopWatchingFocus = watchTabFocus(window, document, () => this.onTabViewChange());
this.connection.start();
// Purge expired live tool logs once the session is up, not before the first paint: it walks
// every cached row, and the store holds every row this device has received, so awaiting it
Expand Down Expand Up @@ -5926,6 +5950,24 @@ export class MatronJournalClient {
else if (this.state.session) storeSelectedConversation(this.state.session, undefined);
}

/** The tab was shown, hidden, focused or blurred. Only showing and hiding change what is viewed. */
private onTabViewChange(): void {
const before = this.lastViewing;
this.sendViewing();
// A hidden tab stops receiving the open chat's live updates, so what they showed can no
// longer be trusted to end: the idle frame would not arrive. Showing the tab again re-views
// the chat, and the journal replays the tool output.
if (before && this.lastViewing === null) this.suspendLiveStreams();
}

private sendViewing(selectedId = this.state.selectedConversationId ?? null): void {
// The tracker covers the open chat's transcript: it is not being viewed.
const convoId = viewingConvoId(this.state.trackerView?.open ? null : selectedId);
if (convoId !== this.lastViewing && this.connection?.send({ op: "viewing", convo_id: convoId })) {
this.lastViewing = convoId;
}
}

private async handleReady(hello?: JournalControlFrame): Promise<void> {
// hello_ok carries the pin list on a journal with pins; its absence means an older journal.
if (hello !== undefined) this.applyJournalPins(pinsFromContainer(hello));
Expand Down Expand Up @@ -5983,9 +6025,19 @@ export class MatronJournalClient {
await this.refreshSelectedConversation(message.convoId, db, gen);
if (!ownsReplay()) return;
}
// Rows another tab stored while this one was suspended or offline are not replayed here: the
// cursor is the shared one. Reload what the views show from the store, not from the cursor.
await this.refreshConversations();
if (!ownsReplay()) return;
const viewedId = this.state.selectedConversationId;
if (viewedId) {
await this.refreshSelectedConversation(viewedId, db, gen);
if (!ownsReplay()) return;
}
for (const message of kept) this.sendPendingMessage(message, connection);
this.lastViewing = undefined;
this.sendViewing();
if (this.state.selectedConversationId) {
connection.send({ op: "viewing", convo_id: this.state.selectedConversationId });
const conversation = this.selectedConversation();
if (conversation?.unread_count) this.scheduleRead(conversation.id, conversation.last_seq, 0);
}
Expand Down Expand Up @@ -6358,6 +6410,8 @@ export class MatronJournalClient {
offset: payload.offset ?? 0,
headTruncated: false,
};
// An offset-less chunk would continue across whatever the closed socket missed.
if (current.stale && payload.offset === undefined) return;
const offset = payload.offset ?? current.offset;
if (offset <= current.offset) {
const addition = trimUtf8Prefix(chunk, current.offset - offset);
Expand All @@ -6371,6 +6425,23 @@ export class MatronJournalClient {
this.toolStreams.set(frame.convo_id, streams);
}

/**
* The tab's socket was closed while hidden, which ends the journal's live tool output for
* this device. Keep the text shown, mark it as no longer live, and let the snapshot the next
* connection's `viewing` draws replace it; never append to it.
*/
private suspendLiveStreams(): void {
for (const streams of this.toolStreams.values()) {
// New objects, so a view memoised on the stream re-renders.
for (const [ref, stream] of Object.entries(streams)) streams[ref] = { ...stream, stale: true };
}
// An activity indicator is cleared only by the frame that ends it, which a closed socket
// cannot deliver; drop it rather than leave "thinking" shown after the agent went idle.
this.activities.clear();
const selectedId = this.state.selectedConversationId;
if (selectedId !== undefined) this.refreshEphemeralState(selectedId);
}

private refreshEphemeralState(conversationId: string): void {
this.patch({
activity: this.activities.get(conversationId),
Expand Down
1 change: 1 addition & 0 deletions src/journal/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5895,6 +5895,7 @@ export function ToolStream({ stream }: { stream: ToolStreamState }): React.React
>
{nodes}
</pre>
{stream.stale && <div className="mj_LiveTool_stale">live output no longer available</div>}
</div>
</div>
</div>
Expand Down
94 changes: 88 additions & 6 deletions src/journal/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ interface JournalConnectionCallbacks {
onSnapshotRequired(): Promise<void>;
onRevoked(): void;
onState(state: ConnectionState, error?: string): void;
/** Whether an active push subscription permits closing the hidden tab's socket. Defaults to false. */
canSuspend?(): boolean;
/** The socket was closed because the tab stayed hidden: views fed by the live socket (tool
* output) no longer update, and the next reconnect's snapshot replaces what they show. */
onSuspend?(): void;
}

// A hidden tab checks at this interval whether push permits closing its socket.
// Becoming visible again reconnects from the stored cursor.
export const HIDDEN_CLOSE_MS = 60_000;
const RETRY_DELAYS_MS = [0, 1_000, 2_000, 4_000, 8_000, 15_000, 30_000];
// Upper bound on one coalesced journal run: keeps a single IndexedDB transaction and the render
// after it bounded, and lets the UI paint between runs during a long replay.
Expand All @@ -43,6 +51,9 @@ export const MAX_JOURNAL_BATCH = 500;
export class JournalConnection {
private socket?: WebSocket;
private retryTimer?: number;
private hiddenTimer?: number;
// Closed on purpose because the tab was hidden: no retries until it is visible again.
private suspended = false;
private retryAttempt = 0;
private stopped = true;
private welcomed = false;
Expand Down Expand Up @@ -80,6 +91,9 @@ export class JournalConnection {
this.stopped = false;
window.addEventListener("online", this.onOnline);
window.addEventListener("offline", this.onOffline);
document.addEventListener("visibilitychange", this.onVisibility);
this.suspended = false;
if (document.visibilityState === "hidden") this.armHiddenTimer();
this.open();
}

Expand All @@ -91,6 +105,11 @@ export class JournalConnection {
this.retryTimer = undefined;
window.removeEventListener("online", this.onOnline);
window.removeEventListener("offline", this.onOffline);
document.removeEventListener("visibilitychange", this.onVisibility);
this.clearHiddenTimer();
this.suspended = false;
// Its remaining frames must not apply on a connection started later.
if (this.socket) this.abandonedSockets.add(this.socket);
this.socket?.close(1000, "client stopped");
this.socket = undefined;
for (const [requestId, pending] of this.pendingRpc) {
Expand Down Expand Up @@ -151,9 +170,59 @@ export class JournalConnection {
}

private readonly onOnline = (): void => {
if (!this.stopped && !this.socket) this.scheduleReconnect(0);
if (!this.stopped && !this.suspended && !this.socket) this.scheduleReconnect(0);
};

private readonly onVisibility = (): void => {
if (this.stopped) return;
if (document.visibilityState === "hidden") {
this.armHiddenTimer();
return;
}
this.clearHiddenTimer();
if (!this.suspended) return;
this.suspended = false;
this.retryAttempt = 0;
// A snapshot reload in flight reopens the socket itself once the new cursor is stored.
if (this.replacingSnapshot) return;
// At once, not on a timer: the user is looking at the tab. Offline, the online event retries.
this.open();
};

private armHiddenTimer(): void {
this.clearHiddenTimer();
this.hiddenTimer = window.setTimeout(() => {
this.hiddenTimer = undefined;
if (this.stopped || this.suspended || document.visibilityState !== "hidden") return;
if (this.callbacks.canSuspend?.()) this.suspendForHidden();
else this.armHiddenTimer();
}, HIDDEN_CLOSE_MS);
}

private clearHiddenTimer(): void {
if (this.hiddenTimer !== undefined) window.clearTimeout(this.hiddenTimer);
this.hiddenTimer = undefined;
}

// Close the socket and stop retrying until the tab is visible again.
private suspendForHidden(): void {
if (this.stopped || this.suspended) return;
this.suspended = true;
if (this.retryTimer !== undefined) window.clearTimeout(this.retryTimer);
this.retryTimer = undefined;
const socket = this.socket;
this.socket = undefined;
this.welcomed = false;
// Frames this socket already delivered but the queue has not applied must not run now:
// the cursor replays them on the next connection.
if (socket) this.abandonedSockets.add(socket);
for (const [requestId, pending] of this.pendingRpc) {
this.resolveRpc(requestId, pending, { ok: false, origin: "relay", code: "not_connected" });
}
socket?.close(1000, "tab hidden");
this.callbacks.onSuspend?.();
}

private readonly onOffline = (): void => {
this.callbacks.onState("offline", "No network connection");
this.socket?.close();
Expand Down Expand Up @@ -200,9 +269,12 @@ export class JournalConnection {
};

socket.onclose = (event) => {
if (this.socket === socket) this.socket = undefined;
// A socket that was replaced (a suspended one whose close handshake finishes after the tab
// opened its successor) must not touch the current connection's state or retry.
if (this.socket !== socket) return;
this.socket = undefined;
this.welcomed = false;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (this.stopped || this.replacingSnapshot) return;
if (this.stopped || this.suspended || this.replacingSnapshot) return;
const reason = event.code === 1000 ? undefined : event.reason || "Connection interrupted";
this.callbacks.onState(navigator.onLine ? "connecting" : "offline", reason);
this.scheduleReconnect();
Expand Down Expand Up @@ -332,12 +404,19 @@ export class JournalConnection {
this.abandonedSockets.add(socket);
this.replacingSnapshot = true;
socket.close(1000, "replacing snapshot");
let failure: { error: unknown } | undefined;
try {
await this.callbacks.onSnapshotRequired();
} catch (error) {
failure = { error };
} finally {
this.replacingSnapshot = false;
}
this.scheduleReconnect(0);
// The socket's close event returned early while the snapshot was being replaced, so this is
// the only retry: it must happen on failure too, and also when the tab was shown meanwhile
// (the visibility handler left the reopening to this path).
this.scheduleReconnect(failure ? undefined : 0);
if (failure) throw failure.error;
}

private async handleJournalBatch(frames: JournalEvent[], socket: WebSocket): Promise<void> {
Expand Down Expand Up @@ -372,7 +451,10 @@ export class JournalConnection {
// Runs already queued on this socket must not apply and move the cursor past the rows that
// just failed: those rows would never be replayed.
this.abandonedSockets.add(socket);
this.callbacks.onState("offline", error instanceof Error ? error.message : "Sync failed");
// A socket that is no longer current reports nothing: its successor owns the state.
if (socket === this.socket) {
this.callbacks.onState("offline", error instanceof Error ? error.message : "Sync failed");
}
socket.close();
}

Expand Down Expand Up @@ -427,7 +509,7 @@ export class JournalConnection {
}

private scheduleReconnect(delayOverride?: number): void {
if (this.stopped || this.retryTimer !== undefined || !navigator.onLine) return;
if (this.stopped || this.suspended || this.retryTimer !== undefined || !navigator.onLine) return;
const baseDelay = delayOverride ?? RETRY_DELAYS_MS[Math.min(this.retryAttempt++, RETRY_DELAYS_MS.length - 1)];
const delay = delayOverride === undefined ? Math.round(baseDelay * (0.8 + Math.random() * 0.4)) : baseDelay;
this.retryTimer = window.setTimeout(() => {
Expand Down
Loading
Loading