The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, it } from "@effect/vitest"; |
| 2 | import { |
| 3 | CommandId, |
| 4 | EventId, |
| 5 | MessageId, |
| 6 | NodeId, |
| 7 | ProjectId, |
| 8 | ProviderDriverKind, |
| 9 | ProviderInstanceId, |
| 10 | ProviderSessionId, |
| 11 | ProviderThreadId, |
| 12 | ProviderTurnId, |
| 13 | RunAttemptId, |
| 14 | RunId, |
| 15 | ThreadId, |
| 16 | TurnItemId, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import * as DateTime from "effect/DateTime"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as Layer from "effect/Layer"; |
| 21 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 22 | import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; |
| 23 | import * as EffectOutbox from "./EffectOutbox.ts"; |
| 24 | import * as Orchestrator from "./Orchestrator.ts"; |
| 25 | import * as ProjectionStore from "./ProjectionStore.ts"; |
| 26 | import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; |
| 27 | import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; |
| 28 | import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 29 | |
| 30 | const instanceId = ProviderInstanceId.make("codex"); |
| 31 | const modelSelection = { instanceId, model: "gpt-5.1-codex" }; |
| 32 | const adapter = { |
| 33 | instanceId, |
| 34 | driver: ProviderDriverKind.make("codex"), |
| 35 | getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2), |
| 36 | planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }), |
| 37 | openSession: () => Effect.die("No provider process needed for record controls"), |
| 38 | } as ProviderAdapter.ProviderAdapterV2["Service"]; |
| 39 | const layerDatabase = SqlitePersistence.layerMemory; |
| 40 | const layerTest = Layer.mergeAll( |
| 41 | layerDatabase, |
| 42 | ProjectionStore.layer.pipe(Layer.provide(layerDatabase)), |
| 43 | EffectOutbox.layer.pipe(Layer.provide(layerDatabase)), |
| 44 | ProviderReplayHarness.layerWithRegistry( |
| 45 | { name: "secret-request-record" }, |
| 46 | ProviderAdapterRegistry.layerFromAdapters([adapter]), |
| 47 | { databaseLayer: layerDatabase, runEffectWorker: false }, |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | const projectId = ProjectId.make("project:secret-request-record"); |
| 52 | const threadId = ThreadId.make("thread:secret-request-record"); |
| 53 | const sessionId = ProviderSessionId.make("session:secret-request-record"); |
| 54 | const providerThreadId = ProviderThreadId.make("provider-thread:secret-request-record"); |
| 55 | const runId = RunId.make("run:secret-request-record"); |
| 56 | const firstAttemptId = RunAttemptId.make("attempt:secret-request-record:1"); |
| 57 | const steeredAttemptId = RunAttemptId.make("attempt:secret-request-record:2"); |
| 58 | const originalRoot = NodeId.make("node:secret-request-record:original"); |
| 59 | const steeredRoot = NodeId.make("node:secret-request-record:steered"); |
| 60 | const originalTurnId = ProviderTurnId.make("provider-turn:secret-request-record:original"); |
| 61 | const messageId = MessageId.make("message:secret-request-record"); |
| 62 | const cardId = TurnItemId.make("turn-item:secret-request:secret-request-record"); |
| 63 | const staleCardId = TurnItemId.make("turn-item:secret-request:secret-request-record:stale"); |
| 64 | const now = DateTime.makeUnsafe("2026-11-11T20:00:00.000Z"); |
| 65 | |
| 66 | /** |
| 67 | * A pending secret card must stay closable after a steering restart replaces the |
| 68 | * run's root node: the call that opened it only ever carries the card's own node, |
| 69 | * so rejecting that node wedges the card open forever. |
| 70 | */ |
| 71 | it.effect("closes a secret card recorded under an earlier root after a steering restart", () => |
| 72 | Effect.gen(function* () { |
| 73 | const orchestrator = yield* Orchestrator.OrchestratorV2; |
| 74 | const projections = yield* ProjectionStore.ProjectionStoreV2; |
| 75 | yield* orchestrator.dispatch({ |
| 76 | type: "thread.create", |
| 77 | commandId: CommandId.make("create-secret-request-record"), |
| 78 | threadId, |
| 79 | projectId, |
| 80 | title: "Secret request record", |
| 81 | modelSelection, |
| 82 | runtimeMode: "full-access", |
| 83 | interactionMode: "default", |
| 84 | branch: null, |
| 85 | worktreePath: null, |
| 86 | createdBy: "user", |
| 87 | creationSource: "web", |
| 88 | }); |
| 89 | yield* projections.apply({ |
| 90 | id: EventId.make("secret-request-record:session"), |
| 91 | type: "provider-session.attached", |
| 92 | threadId, |
| 93 | occurredAt: now, |
| 94 | payload: { |
| 95 | id: sessionId, |
| 96 | driver: adapter.driver, |
| 97 | providerInstanceId: instanceId, |
| 98 | status: "ready", |
| 99 | cwd: "/repo", |
| 100 | model: modelSelection.model, |
| 101 | capabilities: CodexProviderCapabilitiesV2, |
| 102 | createdAt: now, |
| 103 | updatedAt: now, |
| 104 | lastError: null, |
| 105 | }, |
| 106 | }); |
| 107 | yield* projections.apply({ |
| 108 | id: EventId.make("secret-request-record:provider-thread"), |
| 109 | type: "provider-thread.updated", |
| 110 | threadId, |
| 111 | occurredAt: now, |
| 112 | payload: { |
| 113 | id: providerThreadId, |
| 114 | driver: adapter.driver, |
| 115 | providerInstanceId: instanceId, |
| 116 | providerSessionId: sessionId, |
| 117 | appThreadId: threadId, |
| 118 | ownerNodeId: null, |
| 119 | nativeThreadRef: { driver: adapter.driver, nativeId: "native-thread", strength: "strong" }, |
| 120 | nativeConversationHeadRef: null, |
| 121 | status: "idle", |
| 122 | firstRunOrdinal: 1, |
| 123 | lastRunOrdinal: 1, |
| 124 | handoffIds: [], |
| 125 | forkedFrom: null, |
| 126 | createdAt: now, |
| 127 | updatedAt: now, |
| 128 | }, |
| 129 | }); |
| 130 | yield* projections.apply({ |
| 131 | id: EventId.make("secret-request-record:run"), |
| 132 | type: "run.created", |
| 133 | threadId, |
| 134 | occurredAt: now, |
| 135 | payload: { |
| 136 | id: runId, |
| 137 | threadId, |
| 138 | ordinal: 1, |
| 139 | providerInstanceId: instanceId, |
| 140 | modelSelection, |
| 141 | providerThreadId, |
| 142 | userMessageId: messageId, |
| 143 | rootNodeId: originalRoot, |
| 144 | activeAttemptId: firstAttemptId, |
| 145 | status: "running", |
| 146 | requestedAt: now, |
| 147 | startedAt: now, |
| 148 | completedAt: null, |
| 149 | checkpointId: null, |
| 150 | contextHandoffId: null, |
| 151 | }, |
| 152 | }); |
| 153 | yield* projections.apply({ |
| 154 | id: EventId.make("secret-request-record:attempt-1"), |
| 155 | type: "run-attempt.created", |
| 156 | threadId, |
| 157 | occurredAt: now, |
| 158 | payload: { |
| 159 | id: firstAttemptId, |
| 160 | runId, |
| 161 | attemptOrdinal: 1, |
| 162 | rootNodeId: originalRoot, |
| 163 | providerInstanceId: instanceId, |
| 164 | providerThreadId, |
| 165 | providerTurnId: null, |
| 166 | reason: "initial", |
| 167 | status: "running", |
| 168 | startedAt: now, |
| 169 | completedAt: null, |
| 170 | }, |
| 171 | }); |
| 172 | |
| 173 | // The first attempt reached the provider; the card is asked on that turn. |
| 174 | yield* projections.apply({ |
| 175 | id: EventId.make("secret-request-record:turn"), |
| 176 | type: "provider-turn.updated", |
| 177 | threadId, |
| 178 | occurredAt: now, |
| 179 | payload: { |
| 180 | id: originalTurnId, |
| 181 | providerThreadId, |
| 182 | nodeId: originalRoot, |
| 183 | runAttemptId: firstAttemptId, |
| 184 | nativeTurnRef: null, |
| 185 | ordinal: 1, |
| 186 | status: "running", |
| 187 | startedAt: now, |
| 188 | completedAt: null, |
| 189 | }, |
| 190 | }); |
| 191 | |
| 192 | const record = (commandId: string, nodeId: NodeId, turnItemId: TurnItemId) => |
| 193 | orchestrator.dispatch({ |
| 194 | type: "secret_request.record", |
| 195 | commandId: CommandId.make(commandId), |
| 196 | threadId, |
| 197 | runId, |
| 198 | nodeId, |
| 199 | turnItemId, |
| 200 | label: "Booking details", |
| 201 | reason: "To check the passenger in.", |
| 202 | secretStatus: "pending" as const, |
| 203 | }); |
| 204 | const readCard = (id: TurnItemId) => |
| 205 | projections |
| 206 | .getThreadRecords(threadId, ["turnItems"], { |
| 207 | turnItemTypes: ["secret_request"], |
| 208 | messageRoles: [], |
| 209 | }) |
| 210 | .pipe( |
| 211 | Effect.map((records) => { |
| 212 | const item = records.turnItems.find((candidate) => candidate.id === id); |
| 213 | return item?.type === "secret_request" ? item : undefined; |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | yield* record("secret-request-record:open", originalRoot, cardId); |
| 218 | const opened = yield* readCard(cardId); |
| 219 | assert.equal(opened?.secretStatus, "pending"); |
| 220 | assert.equal(opened?.providerTurnId, originalTurnId); |
| 221 | |
| 222 | // The user steers while the card waits: the run's root moves to a new attempt. |
| 223 | yield* projections.apply({ |
| 224 | id: EventId.make("secret-request-record:run-steered"), |
| 225 | type: "run.updated", |
| 226 | threadId, |
| 227 | occurredAt: now, |
| 228 | payload: { |
| 229 | id: runId, |
| 230 | threadId, |
| 231 | ordinal: 1, |
| 232 | providerInstanceId: instanceId, |
| 233 | modelSelection, |
| 234 | providerThreadId, |
| 235 | userMessageId: messageId, |
| 236 | rootNodeId: steeredRoot, |
| 237 | activeAttemptId: steeredAttemptId, |
| 238 | status: "starting", |
| 239 | requestedAt: now, |
| 240 | startedAt: now, |
| 241 | completedAt: null, |
| 242 | checkpointId: null, |
| 243 | contextHandoffId: null, |
| 244 | }, |
| 245 | }); |
| 246 | yield* projections.apply({ |
| 247 | id: EventId.make("secret-request-record:attempt-2"), |
| 248 | type: "run-attempt.created", |
| 249 | threadId, |
| 250 | occurredAt: now, |
| 251 | payload: { |
| 252 | id: steeredAttemptId, |
| 253 | runId, |
| 254 | attemptOrdinal: 2, |
| 255 | rootNodeId: steeredRoot, |
| 256 | providerInstanceId: instanceId, |
| 257 | providerThreadId, |
| 258 | providerTurnId: null, |
| 259 | reason: "steering_restart", |
| 260 | status: "pending", |
| 261 | startedAt: null, |
| 262 | completedAt: null, |
| 263 | }, |
| 264 | }); |
| 265 | |
| 266 | // The waiting call only carries the card's own node; closing must still work. |
| 267 | yield* orchestrator.dispatch({ |
| 268 | type: "secret_request.record", |
| 269 | commandId: CommandId.make("secret-request-record:close"), |
| 270 | threadId, |
| 271 | runId, |
| 272 | nodeId: originalRoot, |
| 273 | turnItemId: cardId, |
| 274 | label: "Booking details", |
| 275 | reason: "To check the passenger in.", |
| 276 | secretStatus: "cancelled" as const, |
| 277 | }); |
| 278 | const cancelled = yield* readCard(cardId); |
| 279 | assert.equal(cancelled?.secretStatus, "cancelled"); |
| 280 | assert.equal(cancelled?.status, "cancelled"); |
| 281 | // Closing through the card's own node keeps the identity it was asked with, |
| 282 | // even though the run's active attempt has no provider turn of its own. |
| 283 | assert.equal(cancelled?.nodeId, originalRoot); |
| 284 | assert.equal(cancelled?.providerTurnId, originalTurnId); |
| 285 | |
| 286 | // A new card may not be opened under the stale root. |
| 287 | const stale = yield* Effect.flip( |
| 288 | record("secret-request-record:stale-open", originalRoot, staleCardId), |
| 289 | ); |
| 290 | assert.match(String((stale as { cause?: unknown }).cause ?? ""), /is not the root of run/); |
| 291 | }).pipe(Effect.provide(layerTest)), |
| 292 | ); |