Skip to content

Commit d04128d

Browse files
Synchronize mobile threads with authoritative shell snapshots
- Add completion markers and buffered shell/thread replay - Refresh snapshots on app activation and show sync status
1 parent 271454c commit d04128d

13 files changed

Lines changed: 681 additions & 158 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,20 @@ export function HomeScreen(props: HomeScreenProps) {
399399
onAction={!props.catalogState.hasReadyEnvironment ? props.onAddConnection : undefined}
400400
variant="plain"
401401
/>
402-
{emptyState.loading ? (
402+
{emptyState.loading && !shouldShowConnectionStatus ? (
403403
<View className="mt-4 items-center">
404404
<ActivityIndicator color={accentColor} />
405405
</View>
406406
) : null}
407+
{shouldShowConnectionStatus && Platform.OS === "ios" ? (
408+
<View className="mt-4">
409+
<WorkspaceConnectionStatus
410+
state={props.catalogState}
411+
onPress={props.onOpenEnvironments}
412+
variant="sidebar"
413+
/>
414+
</View>
415+
) : null}
407416
</View>
408417
{connectionStatus}
409418
</View>

‎apps/mobile/src/features/home/WorkspaceConnectionStatus.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,21 @@ describe("workspace connection status", () => {
6767
expect(shouldShowWorkspaceConnectionStatus(state)).toBe(true);
6868
expect(workspaceConnectionStatusLabel(state)).toBe("Could not reach Julius’s Mac mini");
6969
});
70+
71+
it("shows shell catch-up while cached threads remain visible", () => {
72+
const state = workspaceState({ hasPendingShellSnapshot: true });
73+
74+
expect(shouldShowWorkspaceConnectionStatus(state)).toBe(true);
75+
expect(workspaceConnectionStatusLabel(state)).toBe("Syncing threads...");
76+
});
77+
78+
it("distinguishes initial shell loading from cached catch-up", () => {
79+
const state = workspaceState({
80+
hasLoadedShellSnapshot: false,
81+
hasPendingShellSnapshot: true,
82+
});
83+
84+
expect(shouldShowWorkspaceConnectionStatus(state)).toBe(true);
85+
expect(workspaceConnectionStatusLabel(state)).toBe("Loading threads...");
86+
});
7087
});

‎apps/mobile/src/features/home/WorkspaceConnectionStatus.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export function WorkspaceConnectionStatus(props: {
1212
readonly variant?: "floating" | "sidebar";
1313
}) {
1414
const iconColor = useThemeColor("--color-icon-muted");
15-
const isReconnecting = props.state.connectingEnvironments.length > 0;
15+
const isSynchronizing =
16+
props.state.networkStatus !== "offline" &&
17+
props.state.connectionError === null &&
18+
(props.state.connectingEnvironments.length > 0 || props.state.hasPendingShellSnapshot);
1619
const variant = props.variant ?? "floating";
1720

1821
return (
@@ -37,7 +40,7 @@ export function WorkspaceConnectionStatus(props: {
3740
: undefined
3841
}
3942
>
40-
{isReconnecting ? (
43+
{isSynchronizing ? (
4144
<ActivityIndicator color={iconColor} size="small" />
4245
) : (
4346
<SymbolView name="wifi.slash" size={15} tintColor={iconColor} type="monochrome" />

‎apps/mobile/src/features/home/workspace-connection-status.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export function shouldShowWorkspaceConnectionStatus(state: WorkspaceState): bool
55
state.networkStatus === "offline" ||
66
state.connectionError !== null ||
77
state.hasConnectingEnvironment ||
8+
state.hasPendingShellSnapshot ||
89
(state.hasLoadedShellSnapshot && !state.hasReadyEnvironment)
910
);
1011
}
@@ -18,5 +19,8 @@ export function workspaceConnectionStatusLabel(state: WorkspaceState): string {
1819
return `Reconnecting ${state.connectingEnvironments.length} environments`;
1920
}
2021
if (state.connectionError !== null) return state.connectionError;
22+
if (state.hasPendingShellSnapshot) {
23+
return state.hasLoadedShellSnapshot ? "Syncing threads..." : "Loading threads...";
24+
}
2125
return "Not connected";
2226
}

‎apps/server/src/server.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3717,6 +3717,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
37173717

37183718
assert.equal(response.environment.environmentId, testEnvironmentDescriptor.environmentId);
37193719
assert.equal(response.auth.policy, "desktop-managed-local");
3720+
assert.equal(response.shellResumeCompletionMarker, true);
3721+
assert.equal(response.threadResumeCompletionMarker, true);
37203722
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
37213723
);
37223724

@@ -5607,6 +5609,113 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
56075609
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
56085610
);
56095611

5612+
it.effect("marks an empty shell catch-up replay as synchronized when requested", () =>
5613+
Effect.gen(function* () {
5614+
yield* buildAppUnderTest({
5615+
layers: {
5616+
orchestrationEngine: {
5617+
readEvents: () => Stream.empty,
5618+
},
5619+
},
5620+
});
5621+
5622+
const wsUrl = yield* getWsServerUrl("/ws");
5623+
const firstItem = yield* Effect.scoped(
5624+
withWsRpcClient(wsUrl, (client) =>
5625+
client[ORCHESTRATION_WS_METHODS.subscribeShell]({
5626+
afterSequence: 0,
5627+
requestCompletionMarker: true,
5628+
}).pipe(Stream.runHead),
5629+
),
5630+
);
5631+
5632+
assert.deepEqual(Option.getOrThrow(firstItem), { kind: "synchronized" });
5633+
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
5634+
);
5635+
5636+
it.effect("marks a socket thread snapshot as synchronized when requested", () =>
5637+
Effect.gen(function* () {
5638+
const thread = makeDefaultOrchestrationReadModel().threads[0]!;
5639+
yield* buildAppUnderTest({
5640+
layers: {
5641+
projectionSnapshotQuery: {
5642+
getThreadDetailSnapshot: () =>
5643+
Effect.succeed(Option.some({ snapshotSequence: 1, thread })),
5644+
},
5645+
},
5646+
});
5647+
5648+
const wsUrl = yield* getWsServerUrl("/ws");
5649+
const items = yield* Effect.scoped(
5650+
withWsRpcClient(wsUrl, (client) =>
5651+
client[ORCHESTRATION_WS_METHODS.subscribeThread]({
5652+
threadId: defaultThreadId,
5653+
requestCompletionMarker: true,
5654+
}).pipe(Stream.take(2), Stream.runCollect),
5655+
),
5656+
);
5657+
5658+
assert.equal(items[0]?.kind, "snapshot");
5659+
assert.deepEqual(items[1], { kind: "synchronized" });
5660+
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
5661+
);
5662+
5663+
it.effect("buffers shell events published while the fallback snapshot loads", () =>
5664+
Effect.gen(function* () {
5665+
const liveEvents = yield* PubSub.unbounded<OrchestrationEvent>();
5666+
const deletedEvent = {
5667+
sequence: 2,
5668+
eventId: EventId.make("event-shell-thread-deleted"),
5669+
aggregateKind: "thread",
5670+
aggregateId: defaultThreadId,
5671+
occurredAt: "2026-01-01T00:00:01.000Z",
5672+
commandId: null,
5673+
causationEventId: null,
5674+
correlationId: null,
5675+
metadata: {},
5676+
type: "thread.deleted",
5677+
payload: {
5678+
threadId: defaultThreadId,
5679+
deletedAt: "2026-01-01T00:00:01.000Z",
5680+
},
5681+
} satisfies Extract<OrchestrationEvent, { type: "thread.deleted" }>;
5682+
5683+
yield* buildAppUnderTest({
5684+
layers: {
5685+
orchestrationEngine: {
5686+
streamDomainEvents: Stream.fromPubSub(liveEvents),
5687+
},
5688+
projectionSnapshotQuery: {
5689+
getShellSnapshot: () =>
5690+
Effect.gen(function* () {
5691+
yield* Effect.sleep("25 millis");
5692+
yield* PubSub.publish(liveEvents, deletedEvent);
5693+
return {
5694+
snapshotSequence: 1,
5695+
projects: [],
5696+
threads: [makeDefaultOrchestrationThreadShell()],
5697+
updatedAt: "2026-01-01T00:00:00.000Z",
5698+
};
5699+
}),
5700+
},
5701+
},
5702+
});
5703+
5704+
const wsUrl = yield* getWsServerUrl("/ws");
5705+
const items = yield* Effect.scoped(
5706+
withWsRpcClient(wsUrl, (client) =>
5707+
client[ORCHESTRATION_WS_METHODS.subscribeShell]({
5708+
requestCompletionMarker: true,
5709+
}).pipe(Stream.take(3), Stream.runCollect),
5710+
),
5711+
).pipe(Effect.timeout("2 seconds"));
5712+
5713+
assert.equal(items[0]?.kind, "snapshot");
5714+
assert.equal(items[1]?.kind, "thread-removed");
5715+
assert.deepEqual(items[2], { kind: "synchronized" });
5716+
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
5717+
);
5718+
56105719
it.effect("buffers thread events published while the initial snapshot loads", () =>
56115720
Effect.gen(function* () {
56125721
const thread = makeDefaultOrchestrationReadModel().threads[0]!;

‎apps/server/src/ws.ts‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,8 @@ const makeWsRpcLayer = (
935935
otlpMetricsEnabled: config.otlpMetricsUrl !== undefined,
936936
},
937937
settings,
938+
shellResumeCompletionMarker: true,
939+
threadResumeCompletionMarker: true,
938940
};
939941
});
940942

@@ -1104,11 +1106,28 @@ const makeWsRpcLayer = (
11041106
}),
11051107
),
11061108
);
1107-
return Stream.concat(catchUpStream, Stream.fromQueue(liveBuffer));
1109+
const afterCatchUp =
1110+
input.requestCompletionMarker === true
1111+
? Stream.concat(
1112+
Stream.fromEffect(
1113+
Queue.offer(liveBuffer, { kind: "synchronized" as const }),
1114+
).pipe(Stream.drain),
1115+
Stream.fromQueue(liveBuffer),
1116+
)
1117+
: Stream.fromQueue(liveBuffer);
1118+
return Stream.concat(catchUpStream, afterCatchUp);
11081119
}),
11091120
);
11101121
}
11111122

1123+
// The full-snapshot fallback needs the same replay-window safety
1124+
// as the resume path: subscribe before loading the projection so
1125+
// events published while the snapshot is read are buffered.
1126+
const liveBuffer = yield* Queue.unbounded<OrchestrationShellStreamItem>();
1127+
yield* Effect.forkScoped(
1128+
liveStream.pipe(Stream.runForEach((item) => Queue.offer(liveBuffer, item))),
1129+
);
1130+
const bufferedLiveStream = Stream.fromQueue(liveBuffer);
11121131
const snapshot = yield* projectionSnapshotQuery.getShellSnapshot().pipe(
11131132
Effect.tapError((cause) =>
11141133
Effect.logError("orchestration shell snapshot load failed", { cause }),
@@ -1122,12 +1141,21 @@ const makeWsRpcLayer = (
11221141
),
11231142
);
11241143

1144+
const afterSnapshot =
1145+
input.requestCompletionMarker === true
1146+
? Stream.concat(
1147+
Stream.fromEffect(
1148+
Queue.offer(liveBuffer, { kind: "synchronized" as const }),
1149+
).pipe(Stream.drain),
1150+
bufferedLiveStream,
1151+
)
1152+
: bufferedLiveStream;
11251153
return Stream.concat(
11261154
Stream.make({
11271155
kind: "snapshot" as const,
11281156
snapshot,
11291157
}),
1130-
liveStream,
1158+
afterSnapshot,
11311159
);
11321160
}),
11331161
{ "rpc.aggregate": "orchestration" },
@@ -1206,7 +1234,16 @@ const makeWsRpcLayer = (
12061234
}),
12071235
),
12081236
);
1209-
return Stream.concat(catchUpStream, bufferedLiveStream);
1237+
const afterCatchUp =
1238+
input.requestCompletionMarker === true
1239+
? Stream.concat(
1240+
Stream.fromEffect(
1241+
Queue.offer(liveBuffer, { kind: "synchronized" as const }),
1242+
).pipe(Stream.drain),
1243+
bufferedLiveStream,
1244+
)
1245+
: bufferedLiveStream;
1246+
return Stream.concat(catchUpStream, afterCatchUp);
12101247
}
12111248

12121249
const snapshot = yield* projectionSnapshotQuery
@@ -1228,12 +1265,21 @@ const makeWsRpcLayer = (
12281265
});
12291266
}
12301267

1268+
const afterSnapshot =
1269+
input.requestCompletionMarker === true
1270+
? Stream.concat(
1271+
Stream.fromEffect(
1272+
Queue.offer(liveBuffer, { kind: "synchronized" as const }),
1273+
).pipe(Stream.drain),
1274+
bufferedLiveStream,
1275+
)
1276+
: bufferedLiveStream;
12311277
return Stream.concat(
12321278
Stream.make({
12331279
kind: "snapshot" as const,
12341280
snapshot: snapshot.value,
12351281
}),
1236-
bufferedLiveStream,
1282+
afterSnapshot,
12371283
);
12381284
}),
12391285
{ "rpc.aggregate": "orchestration" },

0 commit comments

Comments
 (0)