Skip to content

Commit 6b06a81

Browse files
fix(mobile): a new thread stays Unsent until the outbox can send it
Connected was not enough: the outbox also waits for a live shell and the operate scope. Hiding the creation on connection alone dropped it from the lists and showed a Working row that never progressed. Both now use the drain's own send gate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent edba83f commit 6b06a81

3 files changed

Lines changed: 84 additions & 18 deletions

File tree

‎apps/mobile/src/state/pending-new-tasks-model.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,26 @@ function queuedCreation(environmentId: string): QueuedThreadMessage {
104104
}
105105

106106
describe("buildPendingNewTasks", () => {
107-
it("lists only creations waiting for their environment to reconnect", () => {
107+
it("lists only creations the outbox is not sending yet", () => {
108+
const sending = EnvironmentId.make("sending");
109+
const syncing = EnvironmentId.make("syncing");
110+
const readOnly = EnvironmentId.make("read-only");
108111
const tasks = buildPendingNewTasks({
109-
queuedMessages: [queuedCreation("online"), queuedCreation("offline")],
112+
queuedMessages: ["sending", "syncing", "read-only", "offline"].map(queuedCreation),
110113
drafts: {},
111-
connectedEnvironmentIds: new Set([EnvironmentId.make("online")]),
114+
sendGate: {
115+
connectedEnvironmentIds: new Set([sending, syncing, readOnly]),
116+
operableEnvironmentIds: new Set([sending, syncing]),
117+
shellStatuses: new Map([
118+
[sending, "live"],
119+
[syncing, "synchronizing"],
120+
[readOnly, "live"],
121+
]),
122+
},
112123
});
113124

114-
expect(tasks.map((task) => task.environmentId)).toEqual(["offline"]);
125+
expect(new Set(tasks.map((task) => task.environmentId))).toEqual(
126+
new Set(["syncing", "read-only", "offline"]),
127+
);
115128
});
116129
});

‎apps/mobile/src/state/pending-new-tasks-model.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
12
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
23

34
import { deriveThreadTitleFromPrompt } from "../lib/projectThreadStartTurn";
4-
import type { QueuedThreadCreation, QueuedThreadMessage } from "./thread-outbox-model";
5+
import {
6+
resolveThreadOutboxDeliveryAction,
7+
type QueuedThreadCreation,
8+
type QueuedThreadMessage,
9+
} from "./thread-outbox-model";
510
import { isNewTaskDraftKey } from "./new-task-draft-key";
611
import type { ComposerDraft } from "./use-composer-drafts";
712
import { resolveNewTaskDraftWorkspace } from "../features/threads/new-task-context-presentation";
@@ -85,15 +90,38 @@ export function resolvePendingDraftBranch(input: {
8590
}).branch;
8691
}
8792

93+
export interface ThreadOutboxSendGate {
94+
readonly connectedEnvironmentIds: ReadonlySet<EnvironmentId>;
95+
readonly operableEnvironmentIds: ReadonlySet<EnvironmentId>;
96+
readonly shellStatuses: ReadonlyMap<EnvironmentId, EnvironmentShellStatus>;
97+
}
98+
99+
/** The outbox drain's gate for a creation: connected, shell live, and operate granted. */
100+
export function isQueuedCreationSending(
101+
message: QueuedThreadMessage,
102+
gate: ThreadOutboxSendGate,
103+
): boolean {
104+
const environmentId = message.environmentId;
105+
return (
106+
gate.operableEnvironmentIds.has(environmentId) &&
107+
resolveThreadOutboxDeliveryAction({
108+
isCreation: true,
109+
threadExists: false,
110+
shellStatus: gate.shellStatuses.get(environmentId) ?? "empty",
111+
environmentConnected: gate.connectedEnvironmentIds.has(environmentId),
112+
}) === "send"
113+
);
114+
}
115+
88116
export function buildPendingNewTasks(input: {
89117
readonly queuedMessages: ReadonlyArray<QueuedThreadMessage>;
90118
readonly drafts: Readonly<Record<string, ComposerDraft>>;
91-
readonly connectedEnvironmentIds: ReadonlySet<EnvironmentId>;
119+
readonly sendGate: ThreadOutboxSendGate;
92120
}): ReadonlyArray<PendingNewTask> {
93121
const tasks: PendingNewTask[] = [];
94122
for (const message of input.queuedMessages) {
95-
// A connected environment is delivering it now; listing it would flash an "Unsent" row.
96-
if (!message.creation || input.connectedEnvironmentIds.has(message.environmentId)) {
123+
// Listing a creation already being sent would flash an "Unsent" row.
124+
if (!message.creation || isQueuedCreationSending(message, input.sendGate)) {
97125
continue;
98126
}
99127
tasks.push({
Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,80 @@
11
import { useAtomValue } from "@effect/atom-react";
2+
import { AuthOrchestrationOperateScope } from "@t3tools/contracts";
3+
import { Atom } from "effect/reactivity";
24
import { useMemo } from "react";
35
import { threadKey } from "@t3tools/client-runtime/state/entities";
46
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
5-
import { buildPendingNewTasks, type PendingNewTask } from "./pending-new-tasks-model";
7+
import {
8+
buildPendingNewTasks,
9+
isQueuedCreationSending,
10+
type PendingNewTask,
11+
type ThreadOutboxSendGate,
12+
} from "./pending-new-tasks-model";
613
import { pendingThreadCreationOutcomesAtom, withLaunchingThreads } from "./pending-thread-creation";
7-
import { connectedEnvironmentIdsAtom } from "./presentation";
814
import { flattenQueuedThreadMessages } from "./thread-outbox-model";
915
import { environmentThreadShells } from "./threads";
1016
import { newTaskComposerDraftsAtom } from "./use-composer-drafts";
11-
import { useThreadOutboxMessages } from "./use-thread-outbox";
17+
import { threadOutboxShellStatusesAtom, useThreadOutboxMessages } from "./use-thread-outbox";
18+
import { environmentSession, sessionHasScope } from "./session";
19+
import { workspaceConnections } from "./workspace";
1220

1321
export type {
1422
PendingDraftTask,
1523
PendingNewTask,
1624
PendingQueuedTask,
1725
} from "./pending-new-tasks-model";
1826

27+
const threadOutboxSendGateAtom = Atom.make((get): ThreadOutboxSendGate => {
28+
const connected = get(workspaceConnections.environmentsAtom).flatMap((environment) =>
29+
environment.connectionState === "connected" ? [environment.environmentId] : [],
30+
);
31+
return {
32+
connectedEnvironmentIds: new Set(connected),
33+
operableEnvironmentIds: new Set(
34+
connected.filter((environmentId) =>
35+
sessionHasScope(
36+
get(environmentSession.sessionStateAtom(environmentId)),
37+
AuthOrchestrationOperateScope,
38+
),
39+
),
40+
),
41+
shellStatuses: get(threadOutboxShellStatusesAtom),
42+
};
43+
}).pipe(Atom.withLabel("mobile:pending-new-tasks:send-gate"));
44+
1945
export function usePendingNewTasks(): ReadonlyArray<PendingNewTask> {
2046
const queuedMessagesByThreadKey = useThreadOutboxMessages();
2147
const drafts = useAtomValue(newTaskComposerDraftsAtom);
2248
const hiddenKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom);
23-
const connectedEnvironmentIds = useAtomValue(connectedEnvironmentIdsAtom);
49+
const sendGate = useAtomValue(threadOutboxSendGateAtom);
2450
// A delivered creation can stay queued after its thread is live and hidden.
2551
const queuedMessages = flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter(
2652
(message) => !hiddenKeys.has(threadKey(message)),
2753
);
2854
return buildPendingNewTasks({
2955
queuedMessages,
3056
drafts,
31-
connectedEnvironmentIds: new Set(connectedEnvironmentIds),
57+
sendGate,
3258
});
3359
}
3460

35-
/** The list's threads, with creations a connected environment is delivering placed as working. */
61+
/** The list's threads, with creations the outbox is sending placed as working. */
3662
export function useLaunchingThreads(
3763
threads: ReadonlyArray<EnvironmentThreadShell>,
3864
): ReadonlyArray<EnvironmentThreadShell> {
3965
const queuedMessagesByThreadKey = useThreadOutboxMessages();
4066
const outcomes = useAtomValue(pendingThreadCreationOutcomesAtom);
4167
const hiddenThreadKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom);
42-
const connectedEnvironmentIds = useAtomValue(connectedEnvironmentIdsAtom);
68+
const sendGate = useAtomValue(threadOutboxSendGateAtom);
4369
return useMemo(() => {
44-
const connected = new Set(connectedEnvironmentIds);
4570
const launches = [
4671
...flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter((message) =>
47-
connected.has(message.environmentId),
72+
isQueuedCreationSending(message, sendGate),
4873
),
4974
...Object.values(outcomes).flatMap((outcome) =>
5075
outcome.kind === "delivered" ? [outcome.message] : [],
5176
),
5277
];
5378
return withLaunchingThreads({ threads, launches, hiddenThreadKeys });
54-
}, [threads, queuedMessagesByThreadKey, outcomes, hiddenThreadKeys, connectedEnvironmentIds]);
79+
}, [threads, queuedMessagesByThreadKey, outcomes, hiddenThreadKeys, sendGate]);
5580
}

0 commit comments

Comments
 (0)