The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 3 | import { |
| 4 | EnvironmentId, |
| 5 | EventId, |
| 6 | MessageId, |
| 7 | NodeId, |
| 8 | type OrchestrationV2DomainEvent, |
| 9 | type OrchestrationV2ThreadShell, |
| 10 | type OrchestrationV2TurnItem, |
| 11 | ProjectId, |
| 12 | ProviderInstanceId, |
| 13 | RunId, |
| 14 | RuntimeRequestId, |
| 15 | ThreadId, |
| 16 | TurnItemId, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import { RelayAgentActivityState } from "@t3tools/contracts/relay"; |
| 19 | import * as DateTime from "effect/DateTime"; |
| 20 | import * as Deferred from "effect/Deferred"; |
| 21 | import * as Effect from "effect/Effect"; |
| 22 | import * as Fiber from "effect/Fiber"; |
| 23 | import * as Layer from "effect/Layer"; |
| 24 | import * as Option from "effect/Option"; |
| 25 | import * as Queue from "effect/Queue"; |
| 26 | import * as Ref from "effect/Ref"; |
| 27 | import * as Schema from "effect/Schema"; |
| 28 | import * as Stream from "effect/Stream"; |
| 29 | import * as TestClock from "effect/testing/TestClock"; |
| 30 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 31 | |
| 32 | import * as EnvironmentKeys from "../cloud/environmentKeys.ts"; |
| 33 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 34 | import { |
| 35 | PUBLISH_AGENT_ACTIVITY_SECRET, |
| 36 | RELAY_ENVIRONMENT_CREDENTIAL_SECRET, |
| 37 | RELAY_ISSUER_SECRET, |
| 38 | RELAY_URL_SECRET, |
| 39 | } from "../cloud/config.ts"; |
| 40 | import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; |
| 41 | import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; |
| 42 | import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 43 | import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; |
| 44 | import * as ProjectService from "../project/ProjectService.ts"; |
| 45 | import * as AgentAwarenessRelay from "./AgentAwarenessRelay.ts"; |
| 46 | |
| 47 | const THREAD_ID = ThreadId.make("relay-thread"); |
| 48 | const SECOND_THREAD_ID = ThreadId.make("relay-thread-2"); |
| 49 | const PROJECT_ID = ProjectId.make("relay-project"); |
| 50 | const NOW = "2026-09-04T12:00:00.000Z"; |
| 51 | |
| 52 | function shell(overrides: Partial<OrchestrationV2ThreadShell> = {}): OrchestrationV2ThreadShell { |
| 53 | return { |
| 54 | id: THREAD_ID, |
| 55 | projectId: PROJECT_ID, |
| 56 | title: "Thread", |
| 57 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 58 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "test-model" }, |
| 59 | runtimeMode: "full-access", |
| 60 | interactionMode: "default", |
| 61 | worktreePath: null, |
| 62 | activeProviderThreadId: null, |
| 63 | lineage: { rootThreadId: THREAD_ID, parentThreadId: null, relationshipToParent: null }, |
| 64 | forkedFrom: null, |
| 65 | createdBy: "user", |
| 66 | creationSource: "web", |
| 67 | activeRunId: null, |
| 68 | latestVisibleMessage: null, |
| 69 | hasActionableProposedPlan: false, |
| 70 | itemCount: 0, |
| 71 | visibleItemCount: 0, |
| 72 | lastVisitedAt: null, |
| 73 | deletedAt: null, |
| 74 | branch: null, |
| 75 | linkedPullRequest: null, |
| 76 | status: "running", |
| 77 | activityRunStatus: null, |
| 78 | pendingRuntimeRequest: null, |
| 79 | pendingBackgroundTasks: [], |
| 80 | latestRunId: null, |
| 81 | latestRunRequestedAt: null, |
| 82 | latestRunStartedAt: null, |
| 83 | latestRunCompletedAt: null, |
| 84 | latestUserMessageAt: null, |
| 85 | createdAt: DateTime.makeUnsafe(NOW), |
| 86 | updatedAt: DateTime.makeUnsafe(NOW), |
| 87 | archivedAt: null, |
| 88 | settledOverride: null, |
| 89 | settledAt: null, |
| 90 | snoozedUntil: null, |
| 91 | snoozedAt: null, |
| 92 | pinnedAt: null, |
| 93 | ...overrides, |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | const CHILD_THREAD_ID = ThreadId.make("relay-child-thread"); |
| 98 | |
| 99 | function waitingChild( |
| 100 | overrides: Partial<OrchestrationV2ThreadShell> = {}, |
| 101 | ): OrchestrationV2ThreadShell { |
| 102 | return shell({ |
| 103 | id: CHILD_THREAD_ID, |
| 104 | title: "Child", |
| 105 | lineage: { |
| 106 | rootThreadId: THREAD_ID, |
| 107 | parentThreadId: THREAD_ID, |
| 108 | relationshipToParent: "subagent", |
| 109 | }, |
| 110 | createdBy: "agent", |
| 111 | creationSource: "mcp", |
| 112 | activityRunStatus: "running", |
| 113 | pendingRuntimeRequest: { |
| 114 | id: RuntimeRequestId.make("child-request"), |
| 115 | kind: "command", |
| 116 | createdAt: DateTime.makeUnsafe(NOW), |
| 117 | }, |
| 118 | ...overrides, |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | const PublishPayload = Schema.Struct({ state: Schema.NullOr(RelayAgentActivityState) }); |
| 123 | const decodePublishPayload = Schema.decodeUnknownSync(Schema.fromJsonString(PublishPayload)); |
| 124 | const unused = () => Effect.die("Unexpected test dependency call"); |
| 125 | |
| 126 | describe("startup agent activity", () => { |
| 127 | it("publishes active work and only terminal runs completed after startup", () => { |
| 128 | const startedAt = DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)); |
| 129 | const oldCompleted = ThreadId.make("old-completed"); |
| 130 | const newCompleted = ThreadId.make("new-completed"); |
| 131 | const oldFailed = ThreadId.make("old-failed"); |
| 132 | const newFailed = ThreadId.make("new-failed"); |
| 133 | const ids = AgentAwarenessRelay.resolveAgentAwarenessRelayActiveThreadIds({ |
| 134 | environmentId: EnvironmentId.make("relay-env"), |
| 135 | startedAt, |
| 136 | projects: [{ id: PROJECT_ID, title: "Project" }], |
| 137 | threads: [ |
| 138 | shell(), |
| 139 | shell({ |
| 140 | id: oldCompleted, |
| 141 | status: "completed", |
| 142 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"), |
| 143 | }), |
| 144 | shell({ |
| 145 | id: newCompleted, |
| 146 | status: "completed", |
| 147 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:01.000Z"), |
| 148 | }), |
| 149 | shell({ |
| 150 | id: oldFailed, |
| 151 | status: "failed", |
| 152 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"), |
| 153 | }), |
| 154 | shell({ |
| 155 | id: newFailed, |
| 156 | status: "failed", |
| 157 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:01.000Z"), |
| 158 | }), |
| 159 | shell({ id: ThreadId.make("idle"), status: "idle" }), |
| 160 | shell({ id: ThreadId.make("missing-project"), projectId: ProjectId.make("missing") }), |
| 161 | ], |
| 162 | }); |
| 163 | assert.deepStrictEqual(ids, [THREAD_ID, newCompleted, newFailed]); |
| 164 | }); |
| 165 | }); |
| 166 | |
| 167 | const makeTestRelay = Effect.fnUntraced(function* ( |
| 168 | options: { |
| 169 | readonly respond?: (attempt: number) => Response; |
| 170 | readonly failSecretRead?: (name: string) => boolean; |
| 171 | readonly linked?: boolean; |
| 172 | readonly readShell?: (threadId: ThreadId) => Effect.Effect<OrchestrationV2ThreadShell | null>; |
| 173 | readonly domainEvents?: Stream.Stream<OrchestrationV2DomainEvent>; |
| 174 | readonly catchUpThreads?: ReadonlyArray<OrchestrationV2ThreadShell>; |
| 175 | } = {}, |
| 176 | ) { |
| 177 | const values = new Map<string, Uint8Array>( |
| 178 | options.linked === false |
| 179 | ? [] |
| 180 | : [ |
| 181 | [PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true")], |
| 182 | [RELAY_URL_SECRET, new TextEncoder().encode("https://relay.example.test")], |
| 183 | [RELAY_ISSUER_SECRET, new TextEncoder().encode("https://relay.example.test")], |
| 184 | [RELAY_ENVIRONMENT_CREDENTIAL_SECRET, new TextEncoder().encode("credential-1")], |
| 185 | ], |
| 186 | ); |
| 187 | const secretReads: string[] = []; |
| 188 | const secrets = ServerSecretStore.ServerSecretStore.of({ |
| 189 | get: (name) => |
| 190 | Effect.suspend(() => { |
| 191 | secretReads.push(name); |
| 192 | if (options.failSecretRead?.(name)) { |
| 193 | return Effect.fail( |
| 194 | new ServerSecretStore.SecretStoreReadError({ |
| 195 | resource: name, |
| 196 | cause: "temporary read failure", |
| 197 | }), |
| 198 | ); |
| 199 | } |
| 200 | return Effect.succeed(Option.fromUndefinedOr(values.get(name))); |
| 201 | }), |
| 202 | create: (name, value) => Effect.sync(() => void values.set(name, value)), |
| 203 | set: (name, value) => Effect.sync(() => void values.set(name, value)), |
| 204 | remove: (name) => Effect.sync(() => void values.delete(name)), |
| 205 | getOrCreateRandom: unused, |
| 206 | }); |
| 207 | const currentShell = yield* Ref.make<OrchestrationV2ThreadShell | null>(shell()); |
| 208 | const shellReads: ThreadId[] = []; |
| 209 | const catchUp = { activitySnapshotReads: 0 }; |
| 210 | const threads = ThreadManagementService.ThreadManagementService.of({ |
| 211 | getThreadShell: unused, |
| 212 | getShellSnapshot: unused, |
| 213 | getAgentAwarenessSnapshot: (threadId) => |
| 214 | threadId === undefined |
| 215 | ? Effect.sync(() => { |
| 216 | catchUp.activitySnapshotReads += 1; |
| 217 | return { snapshotSequence: 1, threads: options.catchUpThreads ?? [] }; |
| 218 | }) |
| 219 | : Effect.sync(() => shellReads.push(threadId)).pipe( |
| 220 | Effect.andThen(options.readShell?.(threadId) ?? Ref.get(currentShell)), |
| 221 | Effect.map((thread) => ({ |
| 222 | snapshotSequence: 1, |
| 223 | threads: thread === null ? [] : [thread], |
| 224 | })), |
| 225 | ), |
| 226 | listActiveSubagentThreadIds: unused, |
| 227 | listAppOwnedChildThreadIds: unused, |
| 228 | countThreads: unused, |
| 229 | listActiveShellIndex: () => Effect.succeed([]), |
| 230 | listOlderSettledThreads: unused, |
| 231 | readShellSnapshot: unused, |
| 232 | getThreadTitles: unused, |
| 233 | getThreadLinkMetadata: unused, |
| 234 | ensureLegacyTranscript: unused, |
| 235 | dispatch: unused, |
| 236 | searchThread: () => Effect.die("unused"), |
| 237 | searchThreadStream: () => Stream.empty, |
| 238 | getThreadHistoryPage: () => Effect.die("unused"), |
| 239 | getTimelinePage: () => Effect.die("Unused timeline read"), |
| 240 | getMessageCount: () => Effect.die("unused message count"), |
| 241 | getRunCount: () => Effect.die("unused run count"), |
| 242 | getMessagesById: () => Effect.die("unused messages by id"), |
| 243 | getTurnItem: () => Effect.die("unused turn item read"), |
| 244 | getThreadRecords: () => Effect.die("unused record read"), |
| 245 | getThreadProjection: unused, |
| 246 | getCheckpointContext: unused, |
| 247 | getThreadSnapshot: unused, |
| 248 | getThreadTranscript: unused, |
| 249 | getThreadSnapshotWindow: unused, |
| 250 | getProjectThreadRecords: () => Effect.die("unused project record read"), |
| 251 | listProjectThreads: unused, |
| 252 | sendToThread: unused, |
| 253 | submitRunOutput: unused, |
| 254 | assertProviderInstanceEnabled: unused, |
| 255 | isProviderInstanceEnabledForProject: unused, |
| 256 | hasReceipt: unused, |
| 257 | waitForThread: unused, |
| 258 | settleAfterRun: unused, |
| 259 | settleThread: unused, |
| 260 | interruptThread: unused, |
| 261 | stopWorkflow: () => Effect.die("unused stopWorkflow"), |
| 262 | forceStopThread: () => Effect.die("unused forceStopThread"), |
| 263 | awaitBackgroundTasks: unused, |
| 264 | stopDelegatedTasks: unused, |
| 265 | getThreadEventSequence: unused, |
| 266 | recoverDelegatedTask: unused, |
| 267 | cancelDelegatedTask: unused, |
| 268 | delegatedTaskResultPending: unused, |
| 269 | streamStoredEvents: Stream.empty, |
| 270 | streamStoredEventsFrom: () => Stream.empty, |
| 271 | streamDomainEvents: options.domainEvents ?? Stream.empty, |
| 272 | }); |
| 273 | const publications: Array<{ |
| 274 | readonly url: string; |
| 275 | readonly authorization: string | null; |
| 276 | readonly state: RelayAgentActivityState | null; |
| 277 | }> = []; |
| 278 | const fetch: typeof globalThis.fetch = Object.assign( |
| 279 | ( |
| 280 | input: Parameters<typeof globalThis.fetch>[0], |
| 281 | init?: Parameters<typeof globalThis.fetch>[1], |
| 282 | ) => { |
| 283 | const body = init?.body; |
| 284 | if (typeof body !== "string" && !(body instanceof Uint8Array)) { |
| 285 | return Promise.reject(new Error("Expected a serialized activity publish payload")); |
| 286 | } |
| 287 | const payload = decodePublishPayload( |
| 288 | typeof body === "string" ? body : new TextDecoder().decode(body), |
| 289 | ); |
| 290 | publications.push({ |
| 291 | url: String(input), |
| 292 | authorization: new Headers(init?.headers).get("authorization"), |
| 293 | state: payload.state, |
| 294 | }); |
| 295 | try { |
| 296 | return Promise.resolve( |
| 297 | options.respond?.(publications.length) ?? Response.json({ ok: true, deliveries: [] }), |
| 298 | ); |
| 299 | } catch (error) { |
| 300 | return Promise.reject(error); |
| 301 | } |
| 302 | }, |
| 303 | { preconnect: () => {} }, |
| 304 | ); |
| 305 | const project = { |
| 306 | id: PROJECT_ID, |
| 307 | title: "Project", |
| 308 | workspaceRoot: "/workspace", |
| 309 | defaultModelSelection: null, |
| 310 | scripts: [], |
| 311 | createdAt: NOW, |
| 312 | updatedAt: NOW, |
| 313 | deletedAt: null, |
| 314 | }; |
| 315 | const relay = yield* AgentAwarenessRelay.make.pipe( |
| 316 | Effect.provide(EnvironmentKeys.layer), |
| 317 | Effect.provideService(ServerSecretStore.ServerSecretStore, secrets), |
| 318 | Effect.provideService(ThreadManagementService.ThreadManagementService, threads), |
| 319 | Effect.provideService(ServerEnvironment.ServerEnvironment, { |
| 320 | getEnvironmentId: Effect.succeed(EnvironmentId.make("relay-environment")), |
| 321 | getDescriptor: unused(), |
| 322 | }), |
| 323 | Effect.provideService(ProjectService.ProjectService, { |
| 324 | create: unused, |
| 325 | bootstrap: unused, |
| 326 | update: unused, |
| 327 | delete: unused, |
| 328 | getByWorkspaceRoot: unused, |
| 329 | workspaceScanDirectory: unused, |
| 330 | snapshot: Effect.succeed({ projects: [project], updatedAt: NOW }), |
| 331 | listPage: unused, |
| 332 | getShell: unused, |
| 333 | listShells: unused, |
| 334 | getById: () => Effect.succeedSome(project), |
| 335 | }), |
| 336 | Effect.provideService(FetchHttpClient.Fetch, fetch), |
| 337 | Effect.provide(NodeCrypto.layer), |
| 338 | ); |
| 339 | return { relay, secrets, secretReads, currentShell, shellReads, publications, catchUp }; |
| 340 | }); |
| 341 | |
| 342 | describe("AgentAwarenessRelay", () => { |
| 343 | it.effect("publishes the parent as needing approval while its subagent waits", () => |
| 344 | Effect.gen(function* () { |
| 345 | const shells = yield* Ref.make( |
| 346 | new Map<ThreadId, OrchestrationV2ThreadShell>([ |
| 347 | [THREAD_ID, shell({ status: "running" })], |
| 348 | [CHILD_THREAD_ID, waitingChild({ pendingRuntimeRequest: null })], |
| 349 | ]), |
| 350 | ); |
| 351 | const setShell = (next: OrchestrationV2ThreadShell) => |
| 352 | Ref.update(shells, (current) => new Map(current).set(next.id, next)); |
| 353 | const { relay, publications } = yield* makeTestRelay({ |
| 354 | readShell: (threadId) => |
| 355 | Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)), |
| 356 | }); |
| 357 | yield* relay.publishThread(THREAD_ID); |
| 358 | yield* setShell(shell({ status: "completed" })); |
| 359 | yield* setShell(waitingChild()); |
| 360 | yield* relay.publishThread(CHILD_THREAD_ID); |
| 361 | assert.equal(publications.length, 2); |
| 362 | assert.isTrue(publications[1]?.url.includes(`/threads/${THREAD_ID}/`) ?? false); |
| 363 | assert.equal(publications[1]?.state?.phase, "waiting_for_approval"); |
| 364 | |
| 365 | // A rename keeps the same request's alert time, so the phone doesn't re-alert. |
| 366 | yield* setShell( |
| 367 | shell({ |
| 368 | status: "completed", |
| 369 | title: "Renamed thread", |
| 370 | updatedAt: DateTime.makeUnsafe("2026-09-04T12:01:00.000Z"), |
| 371 | }), |
| 372 | ); |
| 373 | yield* relay.publishThread(THREAD_ID); |
| 374 | assert.equal(publications.length, 3); |
| 375 | assert.equal(publications[2]?.state?.threadTitle, "Renamed thread"); |
| 376 | assert.equal(publications[2]?.state?.updatedAt, publications[1]?.state?.updatedAt); |
| 377 | |
| 378 | yield* setShell(waitingChild({ pendingRuntimeRequest: null })); |
| 379 | yield* relay.publishThread(CHILD_THREAD_ID); |
| 380 | yield* TestClock.adjust("5 seconds"); |
| 381 | yield* relay.drain; |
| 382 | assert.equal(publications.length, 4); |
| 383 | assert.equal(publications[3]?.state?.phase, "completed"); |
| 384 | }), |
| 385 | ); |
| 386 | |
| 387 | it.effect("publishes the parent as needing input when its subagent raises a secret card", () => |
| 388 | Effect.gen(function* () { |
| 389 | const now = DateTime.makeUnsafe(NOW); |
| 390 | const child = waitingChild({ |
| 391 | pendingRuntimeRequest: { |
| 392 | id: RuntimeRequestId.make("child-secret-card"), |
| 393 | kind: "user_input", |
| 394 | createdAt: now, |
| 395 | blocking: true, |
| 396 | userOnly: true, |
| 397 | }, |
| 398 | }); |
| 399 | const parent = shell({ status: "completed" }); |
| 400 | const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>(); |
| 401 | const pulls = yield* Queue.unbounded<void>(); |
| 402 | const { relay, publications } = yield* makeTestRelay({ |
| 403 | readShell: (threadId) => Effect.succeed(threadId === child.id ? child : parent), |
| 404 | domainEvents: Stream.fromEffectRepeat( |
| 405 | Queue.offer(pulls, undefined).pipe(Effect.andThen(Queue.take(events))), |
| 406 | ), |
| 407 | }); |
| 408 | yield* relay.start(); |
| 409 | yield* Queue.take(pulls); |
| 410 | const runId = RunId.make("run:child-secret-card"); |
| 411 | // Recording a secret card emits only this event for the child. |
| 412 | yield* Queue.offer(events, { |
| 413 | id: EventId.make("event:child-secret-card"), |
| 414 | type: "turn-item.updated", |
| 415 | threadId: CHILD_THREAD_ID, |
| 416 | runId, |
| 417 | occurredAt: now, |
| 418 | payload: { |
| 419 | id: TurnItemId.make("child-secret-card"), |
| 420 | threadId: CHILD_THREAD_ID, |
| 421 | runId, |
| 422 | nodeId: NodeId.make("node:child-secret-card"), |
| 423 | providerThreadId: null, |
| 424 | providerTurnId: null, |
| 425 | nativeItemRef: null, |
| 426 | parentItemId: null, |
| 427 | ordinal: 1, |
| 428 | status: "waiting", |
| 429 | title: "SMS code", |
| 430 | startedAt: now, |
| 431 | completedAt: null, |
| 432 | updatedAt: now, |
| 433 | type: "secret_request", |
| 434 | label: "SMS code", |
| 435 | reason: "The agent needs the code.", |
| 436 | secretStatus: "pending", |
| 437 | }, |
| 438 | }); |
| 439 | yield* Queue.take(pulls); |
| 440 | yield* relay.drain; |
| 441 | assert.equal(publications.length, 1); |
| 442 | assert.isTrue(publications[0]?.url.includes(`/threads/${THREAD_ID}/`) ?? false); |
| 443 | assert.equal(publications[0]?.state?.phase, "waiting_for_input"); |
| 444 | }), |
| 445 | ); |
| 446 | |
| 447 | it.effect( |
| 448 | "clears the parent's input wait when the user steers its subagent past a link card", |
| 449 | () => |
| 450 | Effect.gen(function* () { |
| 451 | const now = DateTime.makeUnsafe(NOW); |
| 452 | let child = waitingChild({ |
| 453 | pendingRuntimeRequest: { |
| 454 | id: RuntimeRequestId.make("child-link-card"), |
| 455 | kind: "user_input", |
| 456 | createdAt: now, |
| 457 | blocking: true, |
| 458 | userOnly: true, |
| 459 | }, |
| 460 | }); |
| 461 | const parent = shell({ status: "completed" }); |
| 462 | const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>(); |
| 463 | const pulls = yield* Queue.unbounded<void>(); |
| 464 | const { relay, publications } = yield* makeTestRelay({ |
| 465 | readShell: (threadId) => Effect.succeed(threadId === child.id ? child : parent), |
| 466 | domainEvents: Stream.fromEffectRepeat( |
| 467 | Queue.offer(pulls, undefined).pipe(Effect.andThen(Queue.take(events))), |
| 468 | ), |
| 469 | }); |
| 470 | yield* relay.start(); |
| 471 | yield* Queue.take(pulls); |
| 472 | const runId = RunId.make("run:child-link-card"); |
| 473 | const item = { |
| 474 | threadId: CHILD_THREAD_ID, |
| 475 | runId, |
| 476 | nodeId: NodeId.make("node:child-link-card"), |
| 477 | providerThreadId: null, |
| 478 | providerTurnId: null, |
| 479 | nativeItemRef: null, |
| 480 | parentItemId: null, |
| 481 | title: null, |
| 482 | startedAt: now, |
| 483 | updatedAt: now, |
| 484 | } as const; |
| 485 | yield* Queue.offer(events, { |
| 486 | id: EventId.make("event:child-link-card"), |
| 487 | type: "turn-item.updated", |
| 488 | threadId: CHILD_THREAD_ID, |
| 489 | runId, |
| 490 | occurredAt: now, |
| 491 | payload: { |
| 492 | ...item, |
| 493 | id: TurnItemId.make("child-link-card"), |
| 494 | ordinal: 1, |
| 495 | status: "waiting", |
| 496 | completedAt: null, |
| 497 | type: "link_request", |
| 498 | url: "https://example.com/login", |
| 499 | label: "Sign in", |
| 500 | reason: "The agent needs you to sign in.", |
| 501 | linkStatus: "pending", |
| 502 | }, |
| 503 | }); |
| 504 | yield* Queue.take(pulls); |
| 505 | yield* relay.drain; |
| 506 | assert.equal(publications[0]?.state?.phase, "waiting_for_input"); |
| 507 | |
| 508 | // Steering a running child records only these events for it; the |
| 509 | // projection then stops counting the link card as pending. |
| 510 | child = waitingChild({ pendingRuntimeRequest: null }); |
| 511 | yield* Queue.offer(events, { |
| 512 | id: EventId.make("event:child-steer"), |
| 513 | type: "turn-item.updated", |
| 514 | threadId: CHILD_THREAD_ID, |
| 515 | runId, |
| 516 | occurredAt: now, |
| 517 | payload: { |
| 518 | ...item, |
| 519 | id: TurnItemId.make("child-steer"), |
| 520 | ordinal: 2, |
| 521 | status: "completed", |
| 522 | completedAt: now, |
| 523 | createdBy: "user", |
| 524 | creationSource: "web", |
| 525 | type: "user_message", |
| 526 | messageId: MessageId.make("message:child-steer"), |
| 527 | inputIntent: "steer", |
| 528 | text: "Skip the login.", |
| 529 | attachments: [], |
| 530 | }, |
| 531 | }); |
| 532 | yield* Queue.take(pulls); |
| 533 | // The relay confirms a move into completed before publishing it. |
| 534 | yield* TestClock.adjust("5 seconds"); |
| 535 | yield* relay.drain; |
| 536 | assert.equal(publications.length, 2); |
| 537 | assert.equal(publications[1]?.state?.phase, "completed"); |
| 538 | }), |
| 539 | ); |
| 540 | |
| 541 | it.effect("republishes a completed parent for its waiting child after a relink", () => |
| 542 | Effect.gen(function* () { |
| 543 | const parent = shell({ status: "completed" }); |
| 544 | const child = waitingChild(); |
| 545 | const { relay, secrets, publications } = yield* makeTestRelay({ |
| 546 | catchUpThreads: [parent, child], |
| 547 | readShell: (id) => Effect.succeed(id === child.id ? child : parent), |
| 548 | }); |
| 549 | yield* relay.start(); |
| 550 | yield* TestClock.adjust("1 second"); |
| 551 | yield* relay.drain; |
| 552 | assert.equal(publications.length, 1); |
| 553 | assert.equal(publications[0]?.state?.phase, "waiting_for_approval"); |
| 554 | |
| 555 | yield* secrets.set( |
| 556 | RELAY_ENVIRONMENT_CREDENTIAL_SECRET, |
| 557 | new TextEncoder().encode("credential-2"), |
| 558 | ); |
| 559 | yield* relay.requestCatchUp(); |
| 560 | yield* TestClock.adjust("1 second"); |
| 561 | yield* relay.drain; |
| 562 | assert.equal(publications.length, 2); |
| 563 | assert.equal(publications[1]?.state?.phase, "waiting_for_approval"); |
| 564 | }), |
| 565 | ); |
| 566 | |
| 567 | it.effect("keeps a child's alert time when the parent's own request comes and goes", () => |
| 568 | Effect.gen(function* () { |
| 569 | const shells = yield* Ref.make( |
| 570 | new Map<ThreadId, OrchestrationV2ThreadShell>([ |
| 571 | [THREAD_ID, shell()], |
| 572 | [CHILD_THREAD_ID, waitingChild()], |
| 573 | ]), |
| 574 | ); |
| 575 | const setParent = (next: OrchestrationV2ThreadShell) => |
| 576 | Ref.update(shells, (current) => new Map(current).set(THREAD_ID, next)); |
| 577 | const { relay, publications } = yield* makeTestRelay({ |
| 578 | readShell: (threadId) => |
| 579 | Ref.get(shells).pipe(Effect.map((current) => current.get(threadId) ?? null)), |
| 580 | }); |
| 581 | yield* relay.publishThread(CHILD_THREAD_ID); |
| 582 | yield* setParent( |
| 583 | shell({ |
| 584 | updatedAt: DateTime.makeUnsafe("2026-09-04T12:01:00.000Z"), |
| 585 | pendingRuntimeRequest: { |
| 586 | id: RuntimeRequestId.make("parent-request"), |
| 587 | kind: "command", |
| 588 | createdAt: DateTime.makeUnsafe("2026-09-04T12:01:00.000Z"), |
| 589 | }, |
| 590 | }), |
| 591 | ); |
| 592 | yield* relay.publishThread(THREAD_ID); |
| 593 | yield* setParent(shell({ updatedAt: DateTime.makeUnsafe("2026-09-04T12:02:00.000Z") })); |
| 594 | yield* relay.publishThread(THREAD_ID); |
| 595 | assert.deepStrictEqual( |
| 596 | publications.map(({ state }) => state?.phase), |
| 597 | ["waiting_for_approval", "waiting_for_approval", "waiting_for_approval"], |
| 598 | ); |
| 599 | assert.equal(publications[2]?.state?.updatedAt, publications[0]?.state?.updatedAt); |
| 600 | }), |
| 601 | ); |
| 602 | |
| 603 | it.effect("coalesces queued updates and reruns a thread dirtied during publishing", () => |
| 604 | Effect.gen(function* () { |
| 605 | const started = yield* Deferred.make<void>(); |
| 606 | const release = yield* Deferred.make<void>(); |
| 607 | const rerunStarted = yield* Deferred.make<void>(); |
| 608 | const releaseRerun = yield* Deferred.make<void>(); |
| 609 | const processed: Array<{ threadId: ThreadId; revision: number }> = []; |
| 610 | let revision = 1; |
| 611 | const worker = yield* AgentAwarenessRelay.makeAgentAwarenessPublishWorker((threadId) => |
| 612 | Effect.gen(function* () { |
| 613 | const currentRevision = revision; |
| 614 | const index = processed.length; |
| 615 | processed.push({ threadId, revision: currentRevision }); |
| 616 | if (index === 0) { |
| 617 | yield* Deferred.succeed(started, undefined); |
| 618 | yield* Deferred.await(release); |
| 619 | } else if (threadId === THREAD_ID) { |
| 620 | yield* Deferred.succeed(rerunStarted, undefined); |
| 621 | yield* Deferred.await(releaseRerun); |
| 622 | } |
| 623 | }), |
| 624 | ); |
| 625 | yield* worker.enqueue(THREAD_ID); |
| 626 | yield* Deferred.await(started); |
| 627 | revision = 2; |
| 628 | for (let i = 0; i < 100; i++) { |
| 629 | yield* worker.enqueue(SECOND_THREAD_ID); |
| 630 | yield* worker.enqueue(THREAD_ID); |
| 631 | } |
| 632 | const drained = yield* Deferred.make<void>(); |
| 633 | const draining = yield* worker.drain.pipe( |
| 634 | Effect.andThen(Deferred.succeed(drained, undefined)), |
| 635 | Effect.forkChild, |
| 636 | ); |
| 637 | yield* Deferred.succeed(release, undefined); |
| 638 | yield* Deferred.await(rerunStarted); |
| 639 | assert.isFalse(yield* Deferred.isDone(drained)); |
| 640 | yield* Deferred.succeed(releaseRerun, undefined); |
| 641 | yield* Fiber.join(draining); |
| 642 | assert.deepEqual(processed, [ |
| 643 | { threadId: THREAD_ID, revision: 1 }, |
| 644 | { threadId: SECOND_THREAD_ID, revision: 2 }, |
| 645 | { threadId: THREAD_ID, revision: 2 }, |
| 646 | ]); |
| 647 | }), |
| 648 | ); |
| 649 | |
| 650 | it.effect("deduplicates state and republishes title changes", () => |
| 651 | Effect.gen(function* () { |
| 652 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 653 | yield* relay.publishThread(THREAD_ID); |
| 654 | yield* Ref.set( |
| 655 | currentShell, |
| 656 | shell({ updatedAt: DateTime.makeUnsafe("2026-09-04T13:00:00Z") }), |
| 657 | ); |
| 658 | yield* relay.publishThread(THREAD_ID); |
| 659 | assert.equal(publications.length, 1); |
| 660 | yield* Ref.set(currentShell, shell({ title: "Renamed thread" })); |
| 661 | yield* relay.publishThread(THREAD_ID); |
| 662 | assert.equal(publications.length, 2); |
| 663 | assert.equal(publications[1]?.state?.threadTitle, "Renamed thread"); |
| 664 | }), |
| 665 | ); |
| 666 | |
| 667 | it.effect("republishes unchanged state with fresh credentials after relinking", () => |
| 668 | Effect.gen(function* () { |
| 669 | const { relay, secrets, publications } = yield* makeTestRelay(); |
| 670 | yield* relay.publishThread(THREAD_ID); |
| 671 | yield* secrets.set( |
| 672 | RELAY_ENVIRONMENT_CREDENTIAL_SECRET, |
| 673 | new TextEncoder().encode("credential-2"), |
| 674 | ); |
| 675 | yield* relay.publishThread(THREAD_ID); |
| 676 | assert.equal(publications.length, 2); |
| 677 | assert.equal(publications[0]?.authorization, "Bearer credential-1"); |
| 678 | assert.equal(publications[1]?.authorization, "Bearer credential-2"); |
| 679 | yield* secrets.set( |
| 680 | RELAY_URL_SECRET, |
| 681 | new TextEncoder().encode("https://new-relay.example.test"), |
| 682 | ); |
| 683 | yield* relay.publishThread(THREAD_ID); |
| 684 | assert.equal(publications.length, 3); |
| 685 | assert.isTrue(publications[2]?.url.startsWith("https://new-relay.example.test/") ?? false); |
| 686 | }), |
| 687 | ); |
| 688 | |
| 689 | it.effect.each(["completed", "failed"] as const)( |
| 690 | "publishes a tombstone when a %s thread is settled through a domain event", |
| 691 | (status) => |
| 692 | Effect.gen(function* () { |
| 693 | const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>(); |
| 694 | const pulls = yield* Queue.unbounded<void>(); |
| 695 | const { relay, currentShell, publications } = yield* makeTestRelay({ |
| 696 | domainEvents: Stream.fromEffectRepeat( |
| 697 | Queue.offer(pulls, undefined).pipe(Effect.andThen(Queue.take(events))), |
| 698 | ), |
| 699 | }); |
| 700 | yield* relay.start(); |
| 701 | yield* Queue.take(pulls); |
| 702 | yield* relay.publishThread(THREAD_ID); |
| 703 | const terminal = shell({ status }); |
| 704 | yield* Ref.set(currentShell, terminal); |
| 705 | yield* relay.publishThread(THREAD_ID); |
| 706 | yield* TestClock.adjust("5 seconds"); |
| 707 | yield* relay.drain; |
| 708 | assert.equal(publications[1]?.state?.phase, status); |
| 709 | |
| 710 | const now = yield* DateTime.now; |
| 711 | const settled = { |
| 712 | ...terminal, |
| 713 | settledOverride: "settled" as const, |
| 714 | settledAt: now, |
| 715 | lastVisitedAt: null, |
| 716 | }; |
| 717 | yield* Ref.set(currentShell, settled); |
| 718 | yield* Queue.offer(events, { |
| 719 | id: EventId.make("event:settled"), |
| 720 | type: "thread.settled", |
| 721 | threadId: THREAD_ID, |
| 722 | occurredAt: now, |
| 723 | payload: settled, |
| 724 | }); |
| 725 | yield* Queue.take(pulls); |
| 726 | yield* relay.drain; |
| 727 | assert.equal(publications.length, 2); |
| 728 | yield* TestClock.adjust("5 seconds"); |
| 729 | yield* relay.drain; |
| 730 | assert.equal(publications.length, 3); |
| 731 | assert.equal(publications[2]?.state, null); |
| 732 | }), |
| 733 | ); |
| 734 | |
| 735 | it.effect("holds a completion through a continuation gap, then publishes it once", () => |
| 736 | Effect.gen(function* () { |
| 737 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 738 | yield* relay.publishThread(THREAD_ID); |
| 739 | yield* Ref.set(currentShell, shell({ status: "completed" })); |
| 740 | yield* relay.publishThread(THREAD_ID); |
| 741 | yield* Ref.set(currentShell, shell()); |
| 742 | yield* relay.publishThread(THREAD_ID); |
| 743 | yield* TestClock.adjust("5 seconds"); |
| 744 | yield* relay.drain; |
| 745 | assert.deepEqual( |
| 746 | publications.map((publication) => publication.state?.phase), |
| 747 | ["running"], |
| 748 | ); |
| 749 | |
| 750 | yield* Ref.set(currentShell, shell({ status: "completed" })); |
| 751 | yield* relay.publishThread(THREAD_ID); |
| 752 | assert.equal(publications.length, 1); |
| 753 | yield* TestClock.adjust("5 seconds"); |
| 754 | yield* relay.drain; |
| 755 | assert.deepEqual( |
| 756 | publications.map((publication) => publication.state?.phase), |
| 757 | ["running", "completed"], |
| 758 | ); |
| 759 | }), |
| 760 | ); |
| 761 | |
| 762 | it.effect( |
| 763 | "clears a completion that landed while snoozed, even when processed after the wake", |
| 764 | () => |
| 765 | Effect.gen(function* () { |
| 766 | yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW))); |
| 767 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 768 | const snooze = { |
| 769 | snoozedAt: DateTime.makeUnsafe(NOW), |
| 770 | snoozedUntil: DateTime.makeUnsafe("2026-09-04T13:00:00.000Z"), |
| 771 | }; |
| 772 | yield* Ref.set(currentShell, shell(snooze)); |
| 773 | yield* relay.publishThread(THREAD_ID); |
| 774 | yield* TestClock.adjust("2 hours"); |
| 775 | yield* Ref.set( |
| 776 | currentShell, |
| 777 | shell({ |
| 778 | ...snooze, |
| 779 | status: "completed", |
| 780 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:30:00.000Z"), |
| 781 | }), |
| 782 | ); |
| 783 | yield* relay.publishThread(THREAD_ID); |
| 784 | yield* TestClock.adjust("5 seconds"); |
| 785 | yield* relay.drain; |
| 786 | assert.deepEqual( |
| 787 | publications.map((publication) => publication.state?.phase ?? null), |
| 788 | ["running", null], |
| 789 | ); |
| 790 | }), |
| 791 | ); |
| 792 | |
| 793 | it.effect("still publishes a completion when the thread settles during the hold", () => |
| 794 | Effect.gen(function* () { |
| 795 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 796 | yield* relay.publishThread(THREAD_ID); |
| 797 | const completed = shell({ status: "completed" }); |
| 798 | yield* Ref.set(currentShell, completed); |
| 799 | yield* relay.publishThread(THREAD_ID); |
| 800 | yield* Ref.set(currentShell, { |
| 801 | ...completed, |
| 802 | settledOverride: "settled", |
| 803 | settledAt: yield* DateTime.now, |
| 804 | lastVisitedAt: null, |
| 805 | }); |
| 806 | yield* relay.publishThread(THREAD_ID); |
| 807 | yield* TestClock.adjust("5 seconds"); |
| 808 | yield* relay.drain; |
| 809 | assert.deepEqual( |
| 810 | publications.map((publication) => publication.state?.phase ?? null), |
| 811 | ["running", "completed"], |
| 812 | ); |
| 813 | |
| 814 | yield* TestClock.adjust("5 seconds"); |
| 815 | yield* relay.drain; |
| 816 | assert.deepEqual( |
| 817 | publications.map((publication) => publication.state?.phase ?? null), |
| 818 | ["running", "completed", null], |
| 819 | ); |
| 820 | }), |
| 821 | ); |
| 822 | |
| 823 | it.effect("publishes a cleared thread's tombstone once while it stays cleared", () => |
| 824 | Effect.gen(function* () { |
| 825 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 826 | yield* relay.publishThread(THREAD_ID); |
| 827 | const completed = shell({ status: "completed" }); |
| 828 | yield* Ref.set(currentShell, completed); |
| 829 | yield* relay.publishThread(THREAD_ID); |
| 830 | yield* TestClock.adjust("5 seconds"); |
| 831 | yield* relay.drain; |
| 832 | yield* Ref.set(currentShell, { |
| 833 | ...completed, |
| 834 | settledOverride: "settled", |
| 835 | settledAt: yield* DateTime.now, |
| 836 | lastVisitedAt: null, |
| 837 | }); |
| 838 | yield* relay.publishThread(THREAD_ID); |
| 839 | yield* TestClock.adjust("5 seconds"); |
| 840 | yield* relay.drain; |
| 841 | assert.deepEqual( |
| 842 | publications.map((publication) => publication.state?.phase ?? null), |
| 843 | ["running", "completed", null], |
| 844 | ); |
| 845 | |
| 846 | for (let i = 0; i < 3; i++) { |
| 847 | yield* relay.publishThread(THREAD_ID); |
| 848 | yield* TestClock.adjust("5 seconds"); |
| 849 | yield* relay.drain; |
| 850 | } |
| 851 | assert.deepEqual( |
| 852 | publications.map((publication) => publication.state?.phase ?? null), |
| 853 | ["running", "completed", null], |
| 854 | ); |
| 855 | }), |
| 856 | ); |
| 857 | |
| 858 | it.effect("does not alert an old completion after publishing its thread's tombstone", () => |
| 859 | Effect.gen(function* () { |
| 860 | const oldCompleted = shell({ |
| 861 | status: "completed", |
| 862 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T11:59:00.000Z"), |
| 863 | }); |
| 864 | yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW))); |
| 865 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 866 | yield* Ref.set(currentShell, { |
| 867 | ...oldCompleted, |
| 868 | settledOverride: "settled", |
| 869 | settledAt: DateTime.makeUnsafe(NOW), |
| 870 | lastVisitedAt: null, |
| 871 | }); |
| 872 | yield* relay.publishThread(THREAD_ID); |
| 873 | yield* TestClock.adjust("5 seconds"); |
| 874 | yield* relay.drain; |
| 875 | assert.deepEqual( |
| 876 | publications.map((publication) => publication.state), |
| 877 | [null], |
| 878 | ); |
| 879 | |
| 880 | yield* Ref.set(currentShell, oldCompleted); |
| 881 | yield* relay.publishThread(THREAD_ID); |
| 882 | yield* TestClock.adjust("5 seconds"); |
| 883 | yield* relay.drain; |
| 884 | assert.deepEqual( |
| 885 | publications.map((publication) => publication.state), |
| 886 | [null], |
| 887 | ); |
| 888 | }), |
| 889 | ); |
| 890 | |
| 891 | it.effect("alerts a completion settled during its hold after an earlier tombstone", () => |
| 892 | Effect.gen(function* () { |
| 893 | yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW))); |
| 894 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 895 | const settledAt = DateTime.makeUnsafe(NOW); |
| 896 | yield* Ref.set(currentShell, { |
| 897 | ...shell({ status: "completed" }), |
| 898 | settledOverride: "settled", |
| 899 | settledAt, |
| 900 | lastVisitedAt: null, |
| 901 | }); |
| 902 | yield* relay.publishThread(THREAD_ID); |
| 903 | yield* TestClock.adjust("5 seconds"); |
| 904 | yield* relay.drain; |
| 905 | |
| 906 | const completed = shell({ |
| 907 | status: "completed", |
| 908 | latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:06.000Z"), |
| 909 | }); |
| 910 | yield* Ref.set(currentShell, completed); |
| 911 | yield* relay.publishThread(THREAD_ID); |
| 912 | yield* Ref.set(currentShell, { |
| 913 | ...completed, |
| 914 | settledOverride: "settled", |
| 915 | settledAt, |
| 916 | lastVisitedAt: null, |
| 917 | }); |
| 918 | yield* relay.publishThread(THREAD_ID); |
| 919 | yield* TestClock.adjust("5 seconds"); |
| 920 | yield* relay.drain; |
| 921 | yield* TestClock.adjust("5 seconds"); |
| 922 | yield* relay.drain; |
| 923 | assert.deepEqual( |
| 924 | publications.map((publication) => publication.state?.phase ?? null), |
| 925 | [null, "completed", null], |
| 926 | ); |
| 927 | }), |
| 928 | ); |
| 929 | |
| 930 | it.effect("drops a held completion when a queued continuation hides the thread", () => |
| 931 | Effect.gen(function* () { |
| 932 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 933 | yield* relay.publishThread(THREAD_ID); |
| 934 | yield* Ref.set(currentShell, shell({ status: "completed" })); |
| 935 | yield* relay.publishThread(THREAD_ID); |
| 936 | yield* Ref.set(currentShell, shell({ status: "queued" })); |
| 937 | yield* relay.publishThread(THREAD_ID); |
| 938 | yield* TestClock.adjust("5 seconds"); |
| 939 | yield* relay.drain; |
| 940 | assert.notInclude( |
| 941 | publications.map((publication) => publication.state?.phase ?? null), |
| 942 | "completed", |
| 943 | ); |
| 944 | }), |
| 945 | ); |
| 946 | |
| 947 | it.effect("retries a failed completion even after the thread settles", () => |
| 948 | Effect.gen(function* () { |
| 949 | const { relay, currentShell, publications } = yield* makeTestRelay({ |
| 950 | respond: (attempt) => |
| 951 | attempt === 2 |
| 952 | ? new Response("unavailable", { status: 503 }) |
| 953 | : Response.json({ ok: true, deliveries: [] }), |
| 954 | }); |
| 955 | yield* relay.publishThread(THREAD_ID); |
| 956 | const completed = shell({ status: "completed" }); |
| 957 | yield* Ref.set(currentShell, completed); |
| 958 | yield* relay.publishThread(THREAD_ID); |
| 959 | yield* TestClock.adjust("5 seconds"); |
| 960 | yield* relay.drain; |
| 961 | yield* Ref.set(currentShell, { |
| 962 | ...completed, |
| 963 | settledOverride: "settled", |
| 964 | settledAt: yield* DateTime.now, |
| 965 | lastVisitedAt: null, |
| 966 | }); |
| 967 | yield* TestClock.adjust("1 second"); |
| 968 | yield* relay.drain; |
| 969 | assert.deepEqual( |
| 970 | publications.map((publication) => publication.state?.phase ?? null), |
| 971 | ["running", "completed", "completed"], |
| 972 | ); |
| 973 | |
| 974 | yield* TestClock.adjust("5 seconds"); |
| 975 | yield* relay.drain; |
| 976 | assert.equal(publications.at(-1)?.state ?? null, null); |
| 977 | }), |
| 978 | ); |
| 979 | |
| 980 | it.effect("delivers a completion once the network returns after the retry budget runs out", () => |
| 981 | Effect.gen(function* () { |
| 982 | let networkUp = true; |
| 983 | const delivered: Array<string | null> = []; |
| 984 | const { relay, currentShell, publications } = yield* makeTestRelay({ |
| 985 | respond: (attempt) => { |
| 986 | if (!networkUp) throw new TypeError("fetch failed: getaddrinfo EAI_AGAIN"); |
| 987 | delivered.push(publications[attempt - 1]?.state?.phase ?? null); |
| 988 | return Response.json({ ok: true, deliveries: [] }); |
| 989 | }, |
| 990 | }); |
| 991 | yield* relay.publishThread(THREAD_ID); |
| 992 | networkUp = false; |
| 993 | yield* Ref.set(currentShell, shell({ status: "completed" })); |
| 994 | yield* relay.publishThread(THREAD_ID); |
| 995 | yield* TestClock.adjust("5 seconds"); |
| 996 | yield* relay.drain; |
| 997 | for (const delay of [ |
| 998 | "1 second", |
| 999 | "2 seconds", |
| 1000 | "4 seconds", |
| 1001 | "8 seconds", |
| 1002 | "16 seconds", |
| 1003 | ] as const) { |
| 1004 | yield* TestClock.adjust(delay); |
| 1005 | yield* relay.drain; |
| 1006 | } |
| 1007 | assert.equal(publications.length, 7); |
| 1008 | assert.deepEqual(delivered, ["running"]); |
| 1009 | |
| 1010 | networkUp = true; |
| 1011 | yield* TestClock.adjust("10 minutes"); |
| 1012 | yield* relay.drain; |
| 1013 | assert.deepEqual(delivered, ["running", "completed"]); |
| 1014 | }), |
| 1015 | ); |
| 1016 | |
| 1017 | it.effect("keeps settled live work until its awaited monitor finishes", () => |
| 1018 | Effect.gen(function* () { |
| 1019 | const { relay, currentShell, publications } = yield* makeTestRelay(); |
| 1020 | yield* relay.publishThread(THREAD_ID); |
| 1021 | const held = shell({ |
| 1022 | status: "completed", |
| 1023 | settledOverride: "settled", |
| 1024 | pendingBackgroundTasks: [{ taskId: "watch", kind: "monitor", awaited: true }], |
| 1025 | }); |
| 1026 | yield* Ref.set(currentShell, held); |
| 1027 | yield* relay.publishThread(THREAD_ID); |
| 1028 | yield* TestClock.adjust("5 seconds"); |
| 1029 | yield* relay.drain; |
| 1030 | assert.equal(publications.length, 1); |
| 1031 | assert.equal(publications[0]?.state?.phase, "running"); |
| 1032 | |
| 1033 | yield* Ref.set(currentShell, { ...held, pendingBackgroundTasks: [] }); |
| 1034 | yield* relay.publishThread(THREAD_ID); |
| 1035 | yield* TestClock.adjust("5 seconds"); |
| 1036 | yield* relay.drain; |
| 1037 | assert.equal(publications.length, 2); |
| 1038 | assert.equal(publications[1]?.state, null); |
| 1039 | }), |
| 1040 | ); |
| 1041 | it.effect("publishes a held completion when its background item ends, not on tool output", () => |
| 1042 | Effect.gen(function* () { |
| 1043 | const store = yield* ProjectionStore.ProjectionStoreV2; |
| 1044 | const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>(); |
| 1045 | const pulls = yield* Queue.unbounded<void>(); |
| 1046 | const { relay, shellReads, publications } = yield* makeTestRelay({ |
| 1047 | readShell: (threadId) => store.getThreadShell(threadId).pipe(Effect.orDie), |
| 1048 | domainEvents: Stream.fromEffectRepeat( |
| 1049 | Queue.offer(pulls, undefined).pipe(Effect.andThen(Queue.take(events))), |
| 1050 | ), |
| 1051 | }); |
| 1052 | yield* relay.start(); |
| 1053 | yield* Queue.take(pulls); |
| 1054 | const deliver = Effect.fnUntraced(function* (event: OrchestrationV2DomainEvent) { |
| 1055 | yield* store.apply(event); |
| 1056 | yield* Queue.offer(events, event); |
| 1057 | yield* Queue.take(pulls); |
| 1058 | yield* relay.drain; |
| 1059 | }); |
| 1060 | |
| 1061 | const now = yield* DateTime.now; |
| 1062 | const runId = RunId.make("run:held-item"); |
| 1063 | const run = { |
| 1064 | id: runId, |
| 1065 | threadId: THREAD_ID, |
| 1066 | ordinal: 1, |
| 1067 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1068 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "test-model" }, |
| 1069 | providerThreadId: null, |
| 1070 | userMessageId: MessageId.make("message:held-item"), |
| 1071 | rootNodeId: null, |
| 1072 | activeAttemptId: null, |
| 1073 | status: "running" as const, |
| 1074 | requestedAt: now, |
| 1075 | startedAt: now, |
| 1076 | completedAt: null, |
| 1077 | checkpointId: null, |
| 1078 | contextHandoffId: null, |
| 1079 | }; |
| 1080 | const item = ( |
| 1081 | id: string, |
| 1082 | fields: Pick<OrchestrationV2TurnItem, "status"> & |
| 1083 | ( |
| 1084 | | { readonly type: "dynamic_tool"; readonly toolName: string; readonly input: unknown } |
| 1085 | | { |
| 1086 | readonly type: "assistant_message"; |
| 1087 | readonly messageId: MessageId; |
| 1088 | readonly text: string; |
| 1089 | readonly streaming: boolean; |
| 1090 | } |
| 1091 | ), |
| 1092 | ): OrchestrationV2TurnItem => ({ |
| 1093 | id: TurnItemId.make(id), |
| 1094 | threadId: THREAD_ID, |
| 1095 | runId, |
| 1096 | nodeId: NodeId.make("node:held-item"), |
| 1097 | providerThreadId: null, |
| 1098 | providerTurnId: null, |
| 1099 | nativeItemRef: null, |
| 1100 | parentItemId: null, |
| 1101 | ordinal: 1, |
| 1102 | title: null, |
| 1103 | startedAt: now, |
| 1104 | completedAt: null, |
| 1105 | updatedAt: now, |
| 1106 | ...fields, |
| 1107 | }); |
| 1108 | const itemEvent = (id: string, payload: OrchestrationV2TurnItem) => |
| 1109 | ({ |
| 1110 | id: EventId.make(id), |
| 1111 | type: "turn-item.updated", |
| 1112 | threadId: THREAD_ID, |
| 1113 | runId, |
| 1114 | occurredAt: now, |
| 1115 | payload, |
| 1116 | }) satisfies OrchestrationV2DomainEvent; |
| 1117 | const background = { type: "dynamic_tool" as const, toolName: "watch", input: {} }; |
| 1118 | |
| 1119 | yield* deliver({ |
| 1120 | id: EventId.make("event:held-item:thread"), |
| 1121 | type: "thread.created", |
| 1122 | threadId: THREAD_ID, |
| 1123 | occurredAt: now, |
| 1124 | payload: { |
| 1125 | createdBy: "user", |
| 1126 | creationSource: "web", |
| 1127 | id: THREAD_ID, |
| 1128 | projectId: PROJECT_ID, |
| 1129 | title: "Thread", |
| 1130 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1131 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "test-model" }, |
| 1132 | runtimeMode: "full-access", |
| 1133 | interactionMode: "default", |
| 1134 | branch: null, |
| 1135 | worktreePath: null, |
| 1136 | activeProviderThreadId: null, |
| 1137 | lineage: { rootThreadId: THREAD_ID, parentThreadId: null, relationshipToParent: null }, |
| 1138 | forkedFrom: null, |
| 1139 | createdAt: now, |
| 1140 | updatedAt: now, |
| 1141 | archivedAt: null, |
| 1142 | settledOverride: null, |
| 1143 | settledAt: null, |
| 1144 | lastVisitedAt: null, |
| 1145 | deletedAt: null, |
| 1146 | }, |
| 1147 | }); |
| 1148 | yield* deliver({ |
| 1149 | id: EventId.make("event:held-item:run"), |
| 1150 | type: "run.created", |
| 1151 | threadId: THREAD_ID, |
| 1152 | runId, |
| 1153 | occurredAt: now, |
| 1154 | payload: run, |
| 1155 | }); |
| 1156 | yield* deliver( |
| 1157 | itemEvent("event:held-item:tool", item("item:tool", { ...background, status: "running" })), |
| 1158 | ); |
| 1159 | yield* deliver({ |
| 1160 | id: EventId.make("event:held-item:run-completed"), |
| 1161 | type: "run.updated", |
| 1162 | threadId: THREAD_ID, |
| 1163 | runId, |
| 1164 | occurredAt: now, |
| 1165 | payload: { ...run, status: "completed", completedAt: now }, |
| 1166 | }); |
| 1167 | assert.deepEqual( |
| 1168 | publications.map((publication) => publication.state?.phase), |
| 1169 | ["running"], |
| 1170 | ); |
| 1171 | |
| 1172 | const readsBeforeOutput = shellReads.length; |
| 1173 | yield* deliver( |
| 1174 | itemEvent( |
| 1175 | "event:held-item:reply", |
| 1176 | item("item:reply", { |
| 1177 | type: "assistant_message", |
| 1178 | messageId: MessageId.make("message:held-item:reply"), |
| 1179 | text: "Still watching", |
| 1180 | streaming: true, |
| 1181 | status: "running", |
| 1182 | }), |
| 1183 | ), |
| 1184 | ); |
| 1185 | yield* deliver( |
| 1186 | itemEvent("event:held-item:tick", item("item:tool", { ...background, status: "running" })), |
| 1187 | ); |
| 1188 | assert.equal(shellReads.length, readsBeforeOutput); |
| 1189 | |
| 1190 | yield* deliver( |
| 1191 | itemEvent("event:held-item:end", item("item:tool", { ...background, status: "completed" })), |
| 1192 | ); |
| 1193 | yield* TestClock.adjust("5 seconds"); |
| 1194 | yield* relay.drain; |
| 1195 | assert.deepEqual( |
| 1196 | publications.map((publication) => publication.state?.phase), |
| 1197 | ["running", "completed"], |
| 1198 | ); |
| 1199 | }).pipe( |
| 1200 | Effect.scoped, |
| 1201 | Effect.provide(ProjectionStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory))), |
| 1202 | ), |
| 1203 | ); |
| 1204 | }); |
| 1205 | |
| 1206 | it.effect("catches up after startup without polling after success", () => |
| 1207 | Effect.gen(function* () { |
| 1208 | const { relay, secrets, secretReads, catchUp, publications } = yield* makeTestRelay({ |
| 1209 | catchUpThreads: [shell()], |
| 1210 | }); |
| 1211 | yield* relay.start(); |
| 1212 | yield* TestClock.adjust("1 second"); |
| 1213 | yield* relay.drain; |
| 1214 | assert.equal(catchUp.activitySnapshotReads, 1); |
| 1215 | assert.equal(publications.length, 1); |
| 1216 | assert.include(publications[0]?.state, { |
| 1217 | threadId: THREAD_ID, |
| 1218 | projectTitle: "Project", |
| 1219 | threadTitle: "Thread", |
| 1220 | phase: "running", |
| 1221 | }); |
| 1222 | |
| 1223 | yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("false")); |
| 1224 | yield* relay.requestCatchUp(); |
| 1225 | yield* TestClock.adjust("1 second"); |
| 1226 | assert.equal(catchUp.activitySnapshotReads, 1); |
| 1227 | |
| 1228 | yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, new TextEncoder().encode("true")); |
| 1229 | yield* relay.requestCatchUp(); |
| 1230 | yield* TestClock.adjust("1 second"); |
| 1231 | assert.equal(catchUp.activitySnapshotReads, 2); |
| 1232 | |
| 1233 | const readsBeforeIdle = secretReads.length; |
| 1234 | yield* TestClock.adjust("10 minutes"); |
| 1235 | assert.equal(catchUp.activitySnapshotReads, 2); |
| 1236 | assert.equal(secretReads.length, readsBeforeIdle); |
| 1237 | |
| 1238 | yield* secrets.set( |
| 1239 | RELAY_ENVIRONMENT_CREDENTIAL_SECRET, |
| 1240 | new TextEncoder().encode("credential-2"), |
| 1241 | ); |
| 1242 | yield* relay.requestCatchUp(); |
| 1243 | yield* TestClock.adjust("1 second"); |
| 1244 | yield* relay.drain; |
| 1245 | assert.equal(catchUp.activitySnapshotReads, 3); |
| 1246 | assert.equal(publications.length, 2); |
| 1247 | assert.equal(publications[1]?.state?.threadId, THREAD_ID); |
| 1248 | }), |
| 1249 | ); |