|
| 1 | +import { |
| 2 | + EMPTY_ENVIRONMENT_THREAD_STATE, |
| 3 | + type EnvironmentThreadState, |
| 4 | +} from "@t3tools/client-runtime/state/threads"; |
| 5 | +import { |
| 6 | + EnvironmentId, |
| 7 | + ProjectId, |
| 8 | + ProviderInstanceId, |
| 9 | + ThreadId, |
| 10 | + type OrchestrationSessionStatus, |
| 11 | + type OrchestrationThread, |
| 12 | + type OrchestrationThreadShell, |
| 13 | +} from "@t3tools/contracts"; |
| 14 | +import * as Option from "effect/Option"; |
| 15 | +import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity"; |
| 16 | +import { describe, expect, it } from "vite-plus/test"; |
| 17 | + |
| 18 | +import { createRunningThreadKeepAliveAtom } from "./threads"; |
| 19 | + |
| 20 | +const LOCAL = EnvironmentId.make("local"); |
| 21 | +const REMOTE = EnvironmentId.make("remote"); |
| 22 | + |
| 23 | +function session(threadId: ThreadId, status: OrchestrationSessionStatus) { |
| 24 | + return { |
| 25 | + threadId, |
| 26 | + status, |
| 27 | + providerName: "codex", |
| 28 | + runtimeMode: "full-access", |
| 29 | + activeTurnId: null, |
| 30 | + lastError: null, |
| 31 | + updatedAt: "2026-09-24T00:00:00.000Z", |
| 32 | + } satisfies OrchestrationThread["session"]; |
| 33 | +} |
| 34 | + |
| 35 | +function shell(id: string, status: OrchestrationSessionStatus | null) { |
| 36 | + const threadId = ThreadId.make(id); |
| 37 | + return { |
| 38 | + id: threadId, |
| 39 | + session: status === null ? null : session(threadId, status), |
| 40 | + } satisfies Pick<OrchestrationThreadShell, "id" | "session">; |
| 41 | +} |
| 42 | + |
| 43 | +function detail( |
| 44 | + id: string, |
| 45 | + status: OrchestrationSessionStatus, |
| 46 | + overrides: Partial<EnvironmentThreadState> = {}, |
| 47 | +) { |
| 48 | + const threadId = ThreadId.make(id); |
| 49 | + const thread: OrchestrationThread = { |
| 50 | + id: threadId, |
| 51 | + projectId: ProjectId.make("project"), |
| 52 | + title: id, |
| 53 | + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 54 | + runtimeMode: "full-access", |
| 55 | + interactionMode: "default", |
| 56 | + branch: null, |
| 57 | + worktreePath: null, |
| 58 | + latestTurn: null, |
| 59 | + createdAt: "2026-09-24T00:00:00.000Z", |
| 60 | + updatedAt: "2026-09-24T00:00:00.000Z", |
| 61 | + archivedAt: null, |
| 62 | + settledOverride: null, |
| 63 | + settledAt: null, |
| 64 | + pullRequests: [], |
| 65 | + deletedAt: null, |
| 66 | + messages: [], |
| 67 | + proposedPlans: [], |
| 68 | + activities: [], |
| 69 | + checkpoints: [], |
| 70 | + session: session(threadId, status), |
| 71 | + }; |
| 72 | + return AsyncResult.success<EnvironmentThreadState>({ |
| 73 | + ...EMPTY_ENVIRONMENT_THREAD_STATE, |
| 74 | + status: "live", |
| 75 | + data: Option.some(thread), |
| 76 | + ...overrides, |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +function makeHarness() { |
| 81 | + // Registry cleanup runs only on `flush`, like the real deferred task. |
| 82 | + const tasks: Array<() => void> = []; |
| 83 | + const registry = AtomRegistry.make({ |
| 84 | + scheduleTask: (task) => { |
| 85 | + tasks.push(task); |
| 86 | + return () => {}; |
| 87 | + }, |
| 88 | + }); |
| 89 | + const flush = () => { |
| 90 | + for (let task = tasks.shift(); task !== undefined; task = tasks.shift()) task(); |
| 91 | + }; |
| 92 | + const environmentIds = Atom.make<ReadonlyArray<EnvironmentId>>([LOCAL, REMOTE]).pipe( |
| 93 | + Atom.keepAlive, |
| 94 | + ); |
| 95 | + const threads = Atom.family((_environmentId: EnvironmentId) => |
| 96 | + Atom.make<ReadonlyArray<ReturnType<typeof shell>>>([]).pipe(Atom.keepAlive), |
| 97 | + ); |
| 98 | + // Stand-ins for the thread state atoms. Each one lives only while mounted, |
| 99 | + // as the real stream does. |
| 100 | + const keys = new Set<string>(); |
| 101 | + const states = Atom.family((_key: string) => |
| 102 | + Atom.make<AsyncResult.AsyncResult<EnvironmentThreadState>>( |
| 103 | + AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE), |
| 104 | + ), |
| 105 | + ); |
| 106 | + const stateAtom = (environmentId: EnvironmentId, threadId: string) => { |
| 107 | + const key = `${environmentId}:${threadId}`; |
| 108 | + keys.add(key); |
| 109 | + return states(key); |
| 110 | + }; |
| 111 | + const keepAlive = createRunningThreadKeepAliveAtom({ |
| 112 | + environmentIdsAtom: environmentIds, |
| 113 | + threadsAtom: threads, |
| 114 | + stateAtom, |
| 115 | + }); |
| 116 | + registry.mount(keepAlive); |
| 117 | + return { |
| 118 | + registry, |
| 119 | + environmentIds, |
| 120 | + threads, |
| 121 | + stateAtom, |
| 122 | + keepAlive, |
| 123 | + openStreams: () => { |
| 124 | + flush(); |
| 125 | + return [...keys].filter((key) => registry.getNodes().has(states(key))).toSorted(); |
| 126 | + }, |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +describe("createRunningThreadKeepAliveAtom", () => { |
| 131 | + it("keeps running threads open across shell updates and thread view visits", () => { |
| 132 | + const h = makeHarness(); |
| 133 | + h.registry.set(h.threads(LOCAL), [ |
| 134 | + shell("a", "running"), |
| 135 | + shell("b", "ready"), |
| 136 | + shell("c", null), |
| 137 | + ]); |
| 138 | + h.registry.set(h.threads(REMOTE), [shell("d", "starting")]); |
| 139 | + expect(h.openStreams()).toEqual(["local:a", "remote:d"]); |
| 140 | + |
| 141 | + // A thread view that comes and goes shares the kept stream. |
| 142 | + const live = detail("a", "running"); |
| 143 | + h.registry.set(h.stateAtom(LOCAL, "a"), live); |
| 144 | + h.registry.mount(h.stateAtom(LOCAL, "a"))(); |
| 145 | + |
| 146 | + // A shell update that starts or stops nothing does not rebuild the set. |
| 147 | + const kept = h.registry.get(h.keepAlive); |
| 148 | + h.registry.set(h.threads(LOCAL), [shell("a", "running"), shell("b", "ready")]); |
| 149 | + expect(h.registry.get(h.keepAlive)).toBe(kept); |
| 150 | + expect(h.openStreams()).toEqual(["local:a", "remote:d"]); |
| 151 | + expect(h.registry.get(h.stateAtom(LOCAL, "a"))).toBe(live); |
| 152 | + }); |
| 153 | + |
| 154 | + it("holds a stopped thread until its own stream is live and shows the stop", () => { |
| 155 | + const h = makeHarness(); |
| 156 | + h.registry.set(h.threads(LOCAL), [ |
| 157 | + shell("a", "running"), |
| 158 | + shell("b", "running"), |
| 159 | + shell("c", "running"), |
| 160 | + ]); |
| 161 | + // "b" has not loaded yet. "c" hit a stream error. |
| 162 | + h.registry.set(h.stateAtom(LOCAL, "a"), detail("a", "running")); |
| 163 | + h.registry.set( |
| 164 | + h.stateAtom(LOCAL, "c"), |
| 165 | + detail("c", "running", { status: "cached", error: Option.some("Could not sync.") }), |
| 166 | + ); |
| 167 | + |
| 168 | + // The shell reports the stops first. A failed stream cannot deliver its |
| 169 | + // stop, so only it is released now. |
| 170 | + h.registry.set(h.threads(LOCAL), [ |
| 171 | + shell("a", "ready"), |
| 172 | + shell("b", "ready"), |
| 173 | + shell("c", "ready"), |
| 174 | + ]); |
| 175 | + expect(h.openStreams()).toEqual(["local:a", "local:b"]); |
| 176 | + |
| 177 | + h.registry.set(h.stateAtom(LOCAL, "a"), detail("a", "ready")); |
| 178 | + h.registry.set(h.stateAtom(LOCAL, "b"), detail("b", "ready", { status: "synchronizing" })); |
| 179 | + expect(h.openStreams()).toEqual(["local:b"]); |
| 180 | + h.registry.set(h.stateAtom(LOCAL, "b"), detail("b", "ready")); |
| 181 | + expect(h.openStreams()).toEqual([]); |
| 182 | + }); |
| 183 | + |
| 184 | + it("follows environments that connect and go away", () => { |
| 185 | + const h = makeHarness(); |
| 186 | + h.registry.set(h.environmentIds, [LOCAL]); |
| 187 | + h.registry.set(h.threads(REMOTE), [shell("d", "running")]); |
| 188 | + expect(h.openStreams()).toEqual([]); |
| 189 | + |
| 190 | + h.registry.set(h.environmentIds, [LOCAL, REMOTE]); |
| 191 | + expect(h.openStreams()).toEqual(["remote:d"]); |
| 192 | + |
| 193 | + // Removal drops every mount, including one still waiting for its stop. |
| 194 | + h.registry.set(h.stateAtom(REMOTE, "d"), detail("d", "running")); |
| 195 | + h.registry.set(h.threads(REMOTE), [shell("d", "ready")]); |
| 196 | + expect(h.openStreams()).toEqual(["remote:d"]); |
| 197 | + h.registry.set(h.environmentIds, [LOCAL]); |
| 198 | + expect(h.openStreams()).toEqual([]); |
| 199 | + }); |
| 200 | +}); |
0 commit comments