GeezerVison is showing github.com — fix(fork): preserve steering and delivery invariants after sync · Alb11747/t3code@c1eda99
File treeExpand file treeCollapse file tree Open diff view settings packages/provider-cursor/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpService.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3361,7 +3361,13 @@ const make = Effect.gen(function* () { | 3361 | 3361 | ); | 3362 | 3362 | // A read past the end of an item's text shows the agent nothing to acknowledge. | 3363 | 3363 | const readPastEnd = (input.textOffset ?? 0) > 0 && items.every((item) => !item.text); | 3364 | | - if (parent !== undefined && scope.thread !== undefined && tasks.length > 0 && !readPastEnd) { | | 3364 | + if ( | | 3365 | + parent !== undefined && | | 3366 | + scope.thread !== undefined && | | 3367 | + tasks.length > 0 && | | 3368 | + (input.textOffset ?? 0) === 0 && | | 3369 | + !readPastEnd | | 3370 | + ) { | 3365 | 3371 | for (const task of tasks) { | 3366 | 3372 | const transfer = taskResultTransfer(parent.thread.id, target, task); | 3367 | 3373 | const resultRunId = transfer?.sourcePoint.runId ?? delegatedTaskRun(target, task)?.id; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/OrchestratorMcpToolkit.fork.integration.test.ts +70-16Lines changed: 70 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -186,13 +186,33 @@ describe("orchestrator MCP toolkit", () => { | 186 | 186 | take: Effect.never, | 187 | 187 | }, | 188 | 188 | ); | 189 | | - const waitForContinuationOffers = (count: number) => | | 189 | + const continuationReservations = ( | | 190 | + offers: ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>, | | 191 | + ) => { | | 192 | + const reservations = new Map<string, (typeof offers)[number]>(); | | 193 | + for (const offer of offers) { | | 194 | + expect(offer.delegatedCompletion).toBeDefined(); | | 195 | + const key = JSON.stringify([ | | 196 | + offer.threadId, | | 197 | + offer.providerThreadId, | | 198 | + offer.delegatedCompletion, | | 199 | + ]); | | 200 | + const previous = reservations.get(key); | | 201 | + if (previous === undefined) reservations.set(key, offer); | | 202 | + else expect(offer).toEqual(previous); | | 203 | + } | | 204 | + return [...reservations.values()]; | | 205 | + }; | | 206 | + const waitForContinuationOffers = (count: number, distinctReservations = false) => | 190 | 207 | Effect.scoped( | 191 | 208 | Effect.gen(function* () { | 192 | 209 | const subscription = yield* PubSub.subscribe(offerEvents); | 193 | 210 | while (true) { | 194 | 211 | const current = yield* Ref.get(continuationOffers); | 195 | | - if (current.length >= count) return current; | | 212 | + const matched = distinctReservations | | 213 | + ? continuationReservations(current) | | 214 | + : current; | | 215 | + if (matched.length >= count) return matched; | 196 | 216 | yield* PubSub.take(subscription); | 197 | 217 | } | 198 | 218 | }), | @@ -201,7 +221,16 @@ describe("orchestrator MCP toolkit", () => { | 201 | 221 | Effect.gen(function* () { | 202 | 222 | const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; | 203 | 223 | yield* worker.drain(); | 204 | | - expect(yield* Ref.get(continuationOffers)).toHaveLength(count); | | 224 | + const offers = yield* Ref.get(continuationOffers); | | 225 | + expect(offers, JSON.stringify(offers)).toHaveLength(count); | | 226 | + }); | | 227 | + const expectDeliveryReservationsToStay = (count: number) => | | 228 | + Effect.gen(function* () { | | 229 | + yield* (yield* EffectWorker.OrchestrationEffectWorkerV2).drain(); | | 230 | + const offers = yield* Ref.get(continuationOffers); | | 231 | + const reservations = continuationReservations(offers); | | 232 | + expect(reservations, JSON.stringify(offers)).toHaveLength(count); | | 233 | + return reservations; | 205 | 234 | }); | 206 | 235 | const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( | 207 | 236 | { | @@ -325,7 +354,7 @@ describe("orchestrator MCP toolkit", () => { | 325 | 354 | Layer.mock(ProjectService.ProjectService)({ | 326 | 355 | getById: (id) => | 327 | 356 | Effect.succeed( | 328 | | - id === projectId | | 357 | + id === projectId || id === "project:mcp-foreign" | 329 | 358 | ? Option.some({ id, defaultModelSelection: projectDefaultSelection } as never) | 330 | 359 | : Option.none(), | 331 | 360 | ), | @@ -706,6 +735,7 @@ describe("orchestrator MCP toolkit", () => { | 706 | 735 |
| 707 | 736 | const childPromptReadCall = yield* invoke("t3_thread_read", { | 708 | 737 | threadId: directChildThreadId, | | 738 | + afterPosition: -1, | 709 | 739 | limit: 1, | 710 | 740 | }); | 711 | 741 | const childPromptRead = yield* decodeThreadReadResult( | @@ -1059,7 +1089,7 @@ describe("orchestrator MCP toolkit", () => { | 1059 | 1089 | threadId: parentThreadId, | 1060 | 1090 | cursor: 90, | 1061 | 1091 | }).pipe(Effect.flip); | 1062 | | - expect(unknownPagingParamError.message).toContain("Expected no excess property"); | | 1092 | + expect(unknownPagingParamError.message).toContain('Unknown argument "cursor"'); | 1063 | 1093 | const threadUpdateTool = server.tools.find( | 1064 | 1094 | ({ tool }) => tool.name === "t3_thread_update", | 1065 | 1095 | ); | @@ -1582,8 +1612,27 @@ describe("orchestrator MCP toolkit", () => { | 1582 | 1612 |
| 1583 | 1613 | expect(childFollowup.taskId).toBeDefined(); | 1584 | 1614 | expect(activeChildFollowup.taskId).toBeDefined(); | 1585 | | - yield* waitForContinuationOffers(2); | 1586 | | - yield* expectOffersToStay(2); | | 1615 | + expect( | | 1616 | + (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find( | | 1617 | + (task) => task.id === heldFollowup.taskId, | | 1618 | + ), | | 1619 | + ).toMatchObject({ | | 1620 | + cancelledRunId: heldFollowup.runId, | | 1621 | + completionDelivery: { state: "disposed" }, | | 1622 | + }); | | 1623 | + // Recovery can retry the open cohort while task_status observes it. | | 1624 | + const followupOffers = yield* waitForContinuationOffers(1, true); | | 1625 | + expect(followupOffers[0]).toMatchObject({ | | 1626 | + threadId: parentThreadId, | | 1627 | + providerThreadId: parentRun.providerThreadId, | | 1628 | + delivery: "message_text", | | 1629 | + delegatedCompletion: { | | 1630 | + parentRunId: parentRun.id, | | 1631 | + generation: expect.any(Number), | | 1632 | + messageId: expect.any(String), | | 1633 | + }, | | 1634 | + }); | | 1635 | + yield* expectDeliveryReservationsToStay(1); | 1587 | 1636 | yield* invoke("task_status", { taskId: childFollowup.taskId }); | 1588 | 1637 | yield* invoke("task_status", { taskId: activeChildFollowup.taskId }); | 1589 | 1638 | yield* Ref.set(continuationOffers, []); | @@ -2188,6 +2237,7 @@ describe("orchestrator MCP toolkit", () => { | 2188 | 2237 |
| 2189 | 2238 | const promptedReadCall = yield* invoke("t3_thread_read", { | 2190 | 2239 | threadId: promptedThread.threadId, | | 2240 | + afterPosition: -1, | 2191 | 2241 | limit: 1, | 2192 | 2242 | }); | 2193 | 2243 | const promptedRead = yield* decodeThreadReadResult( | @@ -3041,7 +3091,7 @@ describe("orchestrator MCP toolkit", () => { | 3041 | 3091 | generation: firstLateDelivery.generation + 1, | 3042 | 3092 | taskIds: [lateTask.id], | 3043 | 3093 | }); | 3044 | | - const lateOffers = yield* waitForContinuationOffers(2); | | 3094 | + const lateOffers = yield* waitForContinuationOffers(2, true); | 3045 | 3095 | expect(lateOffers).toHaveLength(2); | 3046 | 3096 | expect( | 3047 | 3097 | successorReserved.runs.filter((run) => { | @@ -3150,7 +3200,7 @@ describe("orchestrator MCP toolkit", () => { | 3150 | 3200 | generation: successorDelivery.generation, | 3151 | 3201 | taskIds: successorDelivery.taskIds, | 3152 | 3202 | }); | 3153 | | - yield* expectOffersToStay(2); | | 3203 | + yield* expectDeliveryReservationsToStay(2); | 3154 | 3204 | yield* Deferred.succeed(successorGate, undefined); | 3155 | 3205 | const batchedReserved = yield* waitForProjection( | 3156 | 3206 | orchestrator, | @@ -3182,8 +3232,8 @@ describe("orchestrator MCP toolkit", () => { | 3182 | 3232 | ?.state, | 3183 | 3233 | ), | 3184 | 3234 | ).toEqual(["delivered", "claimed", "claimed"]); | 3185 | | - yield* waitForContinuationOffers(3); | 3186 | | - yield* expectOffersToStay(3); | | 3235 | + yield* waitForContinuationOffers(3, true); | | 3236 | + yield* expectDeliveryReservationsToStay(3); | 3187 | 3237 | expect( | 3188 | 3238 | batchedReserved.runs.filter((run) => { | 3189 | 3239 | const message = batchedReserved.messages.find( | @@ -3230,7 +3280,7 @@ describe("orchestrator MCP toolkit", () => { | 3230 | 3280 | ?.state, | 3231 | 3281 | ), | 3232 | 3282 | ).toEqual(["delivered", "delivered", "delivered", "delivered"]); | 3233 | | - yield* expectOffersToStay(3); | | 3283 | + yield* expectDeliveryReservationsToStay(3); | 3234 | 3284 |
| 3235 | 3285 | const removeParentGate = yield* Deferred.make<void>(); | 3236 | 3286 | parentTerminalGates.set(lateParentThreadId, removeParentGate); | @@ -3924,8 +3974,10 @@ describe("orchestrator MCP toolkit", () => { | 3924 | 3974 | completionWake: "always", | 3925 | 3975 | result: `Codex completed: ${approvalCompletionPrompt}`, | 3926 | 3976 | }); | 3927 | | - const offers = (yield* Ref.get(continuationOffers)).filter( | 3928 | | - (offer) => offer.threadId === approvalParentId, | | 3977 | + const offers = continuationReservations( | | 3978 | + (yield* Ref.get(continuationOffers)).filter( | | 3979 | + (offer) => offer.threadId === approvalParentId, | | 3980 | + ), | 3929 | 3981 | ); | 3930 | 3982 | expect(offers).toHaveLength(1); | 3931 | 3983 | const approvalParent = yield* orchestrator.getThreadProjection(approvalParentId); | @@ -3943,8 +3995,10 @@ describe("orchestrator MCP toolkit", () => { | 3943 | 3995 | expect((yield* readApprovalStatus()).pendingApproval).toBeNull(); | 3944 | 3996 | yield* worker.drain(); | 3945 | 3997 | expect( | 3946 | | - (yield* Ref.get(continuationOffers)).filter( | 3947 | | - (offer) => offer.threadId === approvalParentId, | | 3998 | + continuationReservations( | | 3999 | + (yield* Ref.get(continuationOffers)).filter( | | 4000 | + (offer) => offer.threadId === approvalParentId, | | 4001 | + ), | 3948 | 4002 | ), | 3949 | 4003 | ).toHaveLength(1); | 3950 | 4004 | yield* Deferred.succeed(approvalParentGate, undefined); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +16-1Lines changed: 16 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -9662,7 +9662,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 9662 | 9662 | if (current === context) { | 9663 | 9663 | const retry = (yield* Ref.get(providerRetries)).get(context.providerTurnId); | 9664 | 9664 | const queryError = cause === undefined ? undefined : Cause.squash(cause); | 9665 | | - const failure = providerFailureFromQueryError(queryError, closedQuery.receivedInit); | | 9665 | + const queryFailure = providerFailureFromQueryError( | | 9666 | + queryError, | | 9667 | + closedQuery.receivedInit, | | 9668 | + ); | | 9669 | + const processExit = cause === undefined ? undefined : claudeProcessExitFailure(cause); | | 9670 | + const failure = | | 9671 | + processExit === undefined | | 9672 | + ? queryFailure | | 9673 | + : { | | 9674 | + ...queryFailure, | | 9675 | + ...processExit, | | 9676 | + retryable: | | 9677 | + processExit.code === "process_killed_by_signal" | | 9678 | + ? false | | 9679 | + : queryFailure.retryable, | | 9680 | + }; | 9666 | 9681 | const retryFailure = | 9667 | 9682 | cause === undefined || | 9668 | 9683 | (context.overloadRetry !== null && Cause.hasInterruptsOnly(cause)) | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/DelegatedTaskQuestionDispatch.test.ts +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -263,6 +263,48 @@ it.effect.each([ | 263 | 263 | ], | 264 | 264 | }); | 265 | 265 |
| | 266 | + const seedQuestion = Effect.fn("DelegatedTaskQuestionDispatch.seedQuestion")(function* ( | | 267 | + childId: ThreadId, | | 268 | + questionId: RuntimeRequestId, | | 269 | + ) { | | 270 | + yield* orchestrator.dispatch({ | | 271 | + type: "thread.create", | | 272 | + commandId: CommandId.make(`create:${childId}`), | | 273 | + threadId: childId, | | 274 | + projectId: ProjectId.make("project:delegated-question"), | | 275 | + title: "Question child", | | 276 | + modelSelection, | | 277 | + runtimeMode: "full-access", | | 278 | + interactionMode: "default", | | 279 | + branch: null, | | 280 | + worktreePath: cwd, | | 281 | + createdBy: "agent", | | 282 | + creationSource: "mcp", | | 283 | + }); | | 284 | + yield* sink.write({ | | 285 | + events: [ | | 286 | + { | | 287 | + id: EventId.make(`event:pending:${questionId}`), | | 288 | + type: "runtime-request.updated", | | 289 | + threadId: childId, | | 290 | + occurredAt: now, | | 291 | + payload: { | | 292 | + id: questionId, | | 293 | + nodeId: NodeId.make(`node:${questionId}`), | | 294 | + providerTurnId: null, | | 295 | + nativeRequestRef: null, | | 296 | + kind: "user_input", | | 297 | + status: "pending", | | 298 | + responseCapability: { type: "message" }, | | 299 | + createdAt: now, | | 300 | + resolvedAt: null, | | 301 | + }, | | 302 | + }, | | 303 | + ], | | 304 | + }); | | 305 | + }); | | 306 | + yield* seedQuestion(childThreadId, requestId); | | 307 | + | 266 | 308 | const notice = questionNotification(parentThreadId, delegated, childThreadId, { | 267 | 309 | requestId, | 268 | 310 | }); | @@ -381,6 +423,10 @@ it.effect.each([ | 381 | 423 | { requestId: RuntimeRequestId.make("request:earlier-question") }, | 382 | 424 | ); | 383 | 425 | earlierMessageId = earlierNotice.messageId; | | 426 | + yield* seedQuestion( | | 427 | + earlierChildThreadId, | | 428 | + RuntimeRequestId.make("request:earlier-question"), | | 429 | + ); | 384 | 430 | yield* orchestrator.dispatch(earlierNotice); | 385 | 431 | yield* orchestrator.dispatch({ | 386 | 432 | type: "run.interrupt", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +32-7Lines changed: 32 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7275,7 +7275,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 7275 | 7275 | .pipe(mapDispatchError(command)); | 7276 | 7276 | if ( | 7277 | 7277 | Option.isSome(session) && | 7278 | | - session.value.providerSession.capabilities.turns.activeSteeringInterruptsTools === true | | 7278 | + session.value.providerSession.capabilities.turns.activeSteeringInterruptsTools === | | 7279 | + true && | | 7280 | + // Cursor accepts human input natively and has no Claude boundary hook to drain it. | | 7281 | + !(providerThread.driver === "cursor" && command.createdBy === "user") | 7279 | 7282 | ) { | 7280 | 7283 | dispatchMode = { type: "queue_after_active" }; | 7281 | 7284 | atToolBoundary = true; | @@ -7555,7 +7558,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 7555 | 7558 | .pipe(mapDispatchError(command)); | 7556 | 7559 | yield* ensureSteeringMessage(command, command); | 7557 | 7560 | steerWhenReadyRunId = target.id; | 7558 | | - if (command.modelSelection === undefined) modelSelection = target.modelSelection; | | 7561 | + if ( | | 7562 | + command.modelSelection === undefined && | | 7563 | + modelSelection.instanceId === target.providerInstanceId | | 7564 | + ) { | | 7565 | + modelSelection = target.modelSelection; | | 7566 | + } | 7559 | 7567 | dispatchMode = { type: "queue_after_active" }; | 7560 | 7568 | } | 7561 | 7569 | } | @@ -7702,6 +7710,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 7702 | 7710 | if ( | 7703 | 7711 | startupDeliveryIntent !== undefined && | 7704 | 7712 | activeRun !== undefined && | | 7713 | + modelSelection.instanceId === activeRun.providerInstanceId && | 7705 | 7714 | command.modelSelection === undefined && | 7706 | 7715 | !projection.messages.some((message) => message.id === command.messageId) | 7707 | 7716 | ) { | @@ -11819,7 +11828,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 11819 | 11828 | .pipe(mapDispatchError(command)); | 11820 | 11829 | if ( | 11821 | 11830 | Option.isSome(session) && | 11822 | | - session.value.providerSession.capabilities.turns.activeSteeringInterruptsTools === true | | 11831 | + session.value.providerSession.capabilities.turns.activeSteeringInterruptsTools === true && | | 11832 | + !( | | 11833 | + targetProviderThread?.driver === "cursor" && | | 11834 | + queuedMessage.createdBy === "user" && | | 11835 | + command.requestedBy !== "agent" | | 11836 | + ) | 11823 | 11837 | ) { | 11824 | 11838 | // The same refusals as the boundary join: Stop wins, and a run that | 11825 | 11839 | // owns a delegated task keeps its own turn. | @@ -16606,11 +16620,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 16606 | 16620 | Effect.gen(function* () { | 16607 | 16621 | const attempted = new Set<RunId>(); | 16608 | 16622 | while (true) { | 16609 | | - const projection = yield* projectionStore.getThreadRecords( | | 16623 | + const { runs } = yield* projectionStore.getThreadRecords(threadId, ["runs"], { | | 16624 | + runStatuses: ["queued", "running"], | | 16625 | + }); | | 16626 | + if (!runs.some((run) => run.status === "queued")) return; | | 16627 | + const records = yield* projectionStore.getThreadRecords( | 16610 | 16628 | threadId, | 16611 | | - ["runs", "messages", "providerTurns", "providerThreads", "providerSessions"], | 16612 | | - { turnItemTypes: [], messageRoles: ["user"] }, | | 16629 | + ["messages", "providerTurns", "providerThreads", "providerSessions"], | | 16630 | + { | | 16631 | + messageRoles: ["user"], | | 16632 | + messageRunIds: runs.map((run) => run.id), | | 16633 | + providerTurnAttemptIds: runs.flatMap((run) => | | 16634 | + run.activeAttemptId === null ? [] : [run.activeAttemptId], | | 16635 | + ), | | 16636 | + }, | 16613 | 16637 | ); | | 16638 | + const projection = { ...records, runs }; | 16614 | 16639 | const steeringRunIds = queuedSteeringRunIds(projection.runs); | 16615 | 16640 | const next = queuedRunsInDeliveryOrder(projection) | 16616 | 16641 | .filter((queued) => { | @@ -16922,7 +16947,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 16922 | 16947 | candidate.id === input.taskId && | 16923 | 16948 | candidate.origin === "app_owned" && | 16924 | 16949 | candidate.childThreadId === command.threadId && | 16925 | | - isOrchestrationV2WorkActive(candidate.status), | | 16950 | + (isOrchestrationV2WorkActive(candidate.status) || candidate.status === "idle"), | 16926 | 16951 | ); | 16927 | 16952 | const child = yield* projectionStore | 16928 | 16953 | .getThreadRecords( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -696,6 +696,8 @@ function layerTest(input: { | 696 | 696 | ...(input.startTurn === undefined ? {} : { startTurn: input.startTurn }), | 697 | 697 | ...(input.interruptTurn === undefined ? {} : { interruptTurn: input.interruptTurn }), | 698 | 698 | ...(input.beforeUnload === undefined ? {} : { beforeUnload: input.beforeUnload }), | | 699 | + ...(input.unloads === undefined ? {} : { unloads: input.unloads }), | | 700 | + ...(input.archive === undefined ? {} : { archive: input.archive }), | 699 | 701 | ...(input.spawnBeforeOpen === undefined ? {} : { spawnBeforeOpen: input.spawnBeforeOpen }), | 700 | 702 | ...(input.scopeCloseReached === undefined | 701 | 703 | ? {} | |
|