GeezerVison is showing github.com — fix(server): interrupt a provider thread through the turn that owns i… · pingdotgg/t3code@60b0dfe
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/BackgroundWorkStop.integration.test.ts +59-34Lines changed: 59 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -229,11 +229,11 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 229 | 229 | const codexProviderThread = (yield* orchestrator.getThreadProjection(threadId)) | 230 | 230 | .providerThreads[0]!; | 231 | 231 | // A settled run with its root node, attempt, provider turn and, | 232 | | - // optionally, a command it left running. | | 232 | + // optionally, a command or native subagent it left running. | 233 | 233 | const settledRun = (input: { | 234 | 234 | readonly ordinal: number; | 235 | 235 | readonly providerThreadId: ProviderThreadId; | 236 | | - readonly runningCommandId?: TurnItemId; | | 236 | + readonly runningItem?: { readonly id: TurnItemId; readonly kind: "command" | "subagent" }; | 237 | 237 | }) => { | 238 | 238 | const runId = RunId.make(`run:${input.ordinal}`); | 239 | 239 | const attemptId = RunAttemptId.make(`attempt:${input.ordinal}`); | @@ -324,49 +324,73 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 324 | 324 | }, | 325 | 325 | }, | 326 | 326 | ]; | 327 | | - if (input.runningCommandId !== undefined) { | | 327 | + const item = input.runningItem; | | 328 | + if (item !== undefined) { | | 329 | + const base = { | | 330 | + id: item.id, | | 331 | + threadId, | | 332 | + runId, | | 333 | + nodeId, | | 334 | + providerThreadId: input.providerThreadId, | | 335 | + providerTurnId, | | 336 | + nativeItemRef: null, | | 337 | + parentItemId: null, | | 338 | + ordinal: input.ordinal * 100, | | 339 | + status: "running", | | 340 | + title: null, | | 341 | + startedAt: now, | | 342 | + completedAt: null, | | 343 | + updatedAt: now, | | 344 | + } as const; | 328 | 345 | events.push({ | 329 | | - id: EventId.make(`command:${input.ordinal}`), | | 346 | + id: EventId.make(`item:${input.ordinal}`), | 330 | 347 | type: "turn-item.updated", | 331 | 348 | threadId, | 332 | 349 | runId, | 333 | 350 | occurredAt: now, | 334 | | - payload: { | 335 | | - id: input.runningCommandId, | 336 | | - threadId, | 337 | | - runId, | 338 | | - nodeId, | 339 | | - providerThreadId: input.providerThreadId, | 340 | | - providerTurnId, | 341 | | - nativeItemRef: null, | 342 | | - parentItemId: null, | 343 | | - ordinal: input.ordinal * 100, | 344 | | - status: "running", | 345 | | - title: null, | 346 | | - startedAt: now, | 347 | | - completedAt: null, | 348 | | - updatedAt: now, | 349 | | - type: "command_execution", | 350 | | - input: "vp run test --watch", | 351 | | - }, | | 351 | + payload: | | 352 | + item.kind === "command" | | 353 | + ? { ...base, type: "command_execution", input: "vp run test --watch" } | | 354 | + : { | | 355 | + ...base, | | 356 | + // A native subagent item names its own provider thread | | 357 | + // but its parent's provider turn. | | 358 | + providerThreadId: ProviderThreadId.make("provider-thread:codex-subagent"), | | 359 | + type: "subagent", | | 360 | + subagentId: NodeId.make(`subagent:${input.ordinal}`), | | 361 | + origin: "provider_native", | | 362 | + driver, | | 363 | + providerInstanceId: instanceId, | | 364 | + childThreadId: null, | | 365 | + prompt: "Review the change", | | 366 | + result: null, | | 367 | + }, | 352 | 368 | }); | 353 | 369 | } | 354 | 370 | return { runId, providerTurnId, events }; | 355 | 371 | }; | 356 | 372 |
| 357 | | - // Codex leaves a second command in a later run. Then the thread moves | 358 | | - // on to another provider thread, which also has a live session. | | 373 | + // Later Codex runs leave a second command and a native subagent. Then | | 374 | + // the thread moves on to another provider thread, which also has a | | 375 | + // live session. | 359 | 376 | const watcherId = TurnItemId.make("turn-item:watcher"); | 360 | | - const laterCodexRun = settledRun({ | | 377 | + const watcherRun = settledRun({ | 361 | 378 | ordinal: 2, | 362 | 379 | providerThreadId: codexProviderThread.id, | 363 | | - runningCommandId: watcherId, | | 380 | + runningItem: { id: watcherId, kind: "command" }, | | 381 | + }); | | 382 | + const reviewerId = TurnItemId.make("turn-item:reviewer"); | | 383 | + const reviewerRun = settledRun({ | | 384 | + ordinal: 3, | | 385 | + providerThreadId: codexProviderThread.id, | | 386 | + runningItem: { id: reviewerId, kind: "subagent" }, | 364 | 387 | }); | 365 | 388 | const otherProviderThreadId = ProviderThreadId.make("provider-thread:other"); | 366 | | - const latestRun = settledRun({ ordinal: 3, providerThreadId: otherProviderThreadId }); | | 389 | + const latestRun = settledRun({ ordinal: 4, providerThreadId: otherProviderThreadId }); | 367 | 390 | yield* sink.write({ | 368 | 391 | events: [ | 369 | | - ...laterCodexRun.events, | | 392 | + ...watcherRun.events, | | 393 | + ...reviewerRun.events, | 370 | 394 | { | 371 | 395 | id: EventId.make("provider-thread:other"), | 372 | 396 | type: "provider-thread.updated", | @@ -375,8 +399,8 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 375 | 399 | payload: { | 376 | 400 | ...codexProviderThread, | 377 | 401 | id: otherProviderThreadId, | 378 | | - firstRunOrdinal: 3, | 379 | | - lastRunOrdinal: 3, | | 402 | + firstRunOrdinal: 4, | | 403 | + lastRunOrdinal: 4, | 380 | 404 | }, | 381 | 405 | }, | 382 | 406 | ...latestRun.events, | @@ -392,20 +416,21 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 392 | 416 | yield* worker.drain(); | 393 | 417 |
| 394 | 418 | // Stop reaches both provider threads. The Codex one is interrupted at | 395 | | - // its latest pending work, so its settle covers both of its runs. | | 419 | + // its latest pending work, the subagent's parent turn, so its settle | | 420 | + // covers all three Codex runs. | 396 | 421 | assert.sameDeepMembers( | 397 | 422 | interrupts.map((interrupt) => [interrupt.providerThread.id, interrupt.providerTurnId]), | 398 | 423 | [ | 399 | 424 | [otherProviderThreadId, latestRun.providerTurnId], | 400 | | - [codexProviderThread.id, laterCodexRun.providerTurnId], | | 425 | + [codexProviderThread.id, reviewerRun.providerTurnId], | 401 | 426 | ], | 402 | 427 | ); | 403 | 428 | const after = yield* orchestrator.getThreadProjection(threadId); | 404 | 429 | assert.deepEqual( | 405 | | - [devServerId, watcherId].map( | | 430 | + [devServerId, watcherId, reviewerId].map( | 406 | 431 | (id) => after.turnItems.find((item) => item.id === id)?.status, | 407 | 432 | ), | 408 | | - ["interrupted", "interrupted"], | | 433 | + ["interrupted", "interrupted", "interrupted"], | 409 | 434 | ); | 410 | 435 | }).pipe( | 411 | 436 | Effect.provide( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +20-26Lines changed: 20 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8120,53 +8120,47 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 8120 | 8120 | ); | 8121 | 8121 | const runOrdinalOf = (item: (typeof projection.turnItems)[number]) => | 8122 | 8122 | item.runId === null ? -1 : (runOrdinals.get(item.runId) ?? -1); | 8123 | | - // Each provider thread is interrupted at its latest pending item: that | 8124 | | - // item's run bounds the settle follow-up, which must cover all of the | 8125 | | - // thread's work. | 8126 | | - const latestItemByProviderThread = new Map< | | 8123 | + // Each provider thread is interrupted at its latest pending work: that | | 8124 | + // turn's run bounds the settle follow-up, which must cover all of the | | 8125 | + // thread's work. The target comes from the item's provider turn, since | | 8126 | + // a native subagent item names its own provider thread but its | | 8127 | + // parent's turn; interrupting the parent's turn reaches the subagent. | | 8128 | + const latestTurnByProviderThread = new Map< | 8127 | 8129 | OrchestrationV2ProviderThread["id"], | 8128 | | - (typeof projection.turnItems)[number] | | 8130 | + { readonly turn: (typeof projection.providerTurns)[number]; readonly runOrdinal: number } | 8129 | 8131 | >(); | 8130 | 8132 | for (const item of pendingBackgroundTurnItems({ | 8131 | 8133 | turnItems: projection.turnItems, | 8132 | 8134 | runs: projection.runs, | 8133 | 8135 | })) { | 8134 | | - if ( | 8135 | | - item.providerThreadId == null || | 8136 | | - item.providerThreadId === providerThread.id || | 8137 | | - item.providerTurnId === null | 8138 | | - ) { | 8139 | | - continue; | 8140 | | - } | 8141 | | - const latest = latestItemByProviderThread.get(item.providerThreadId); | 8142 | | - if (latest === undefined || runOrdinalOf(item) > runOrdinalOf(latest)) { | 8143 | | - latestItemByProviderThread.set(item.providerThreadId, item); | | 8136 | + const turn = projection.providerTurns.find( | | 8137 | + (candidate) => candidate.id === item.providerTurnId, | | 8138 | + ); | | 8139 | + if (turn === undefined || turn.providerThreadId === providerThread.id) continue; | | 8140 | + const runOrdinal = runOrdinalOf(item); | | 8141 | + const latest = latestTurnByProviderThread.get(turn.providerThreadId); | | 8142 | + if (latest === undefined || runOrdinal > latest.runOrdinal) { | | 8143 | + latestTurnByProviderThread.set(turn.providerThreadId, { turn, runOrdinal }); | 8144 | 8144 | } | 8145 | 8145 | } | 8146 | | - for (const item of latestItemByProviderThread.values()) { | | 8146 | + for (const { turn } of latestTurnByProviderThread.values()) { | 8147 | 8147 | const owner = projection.providerThreads.find( | 8148 | | - (candidate) => candidate.id === item.providerThreadId, | | 8148 | + (candidate) => candidate.id === turn.providerThreadId, | 8149 | 8149 | ); | 8150 | | - if ( | 8151 | | - owner === undefined || | 8152 | | - owner.providerSessionId === null || | 8153 | | - item.providerTurnId === null | 8154 | | - ) { | 8155 | | - continue; | 8156 | | - } | | 8150 | + if (owner === undefined || owner.providerSessionId === null) continue; | 8157 | 8151 | const ownerSession = yield* providerSessions | 8158 | 8152 | .get(owner.providerSessionId) | 8159 | 8153 | .pipe(Effect.orElseSucceed(() => Option.none())); | 8160 | 8154 | if (Option.isNone(ownerSession)) continue; | 8161 | 8155 | otherProviderInterrupts.push({ | 8162 | | - id: `effect:${command.commandId}:provider-turn.interrupt:${item.providerTurnId}`, | | 8156 | + id: `effect:${command.commandId}:provider-turn.interrupt:${turn.id}`, | 8163 | 8157 | commandId: command.commandId, | 8164 | 8158 | threadId: command.threadId, | 8165 | 8159 | request: { | 8166 | 8160 | type: "provider-turn.interrupt", | 8167 | 8161 | providerSessionId: owner.providerSessionId, | 8168 | 8162 | providerThreadId: owner.id, | 8169 | | - providerTurnId: item.providerTurnId, | | 8163 | + providerTurnId: turn.id, | 8170 | 8164 | }, | 8171 | 8165 | }); | 8172 | 8166 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -247,7 +247,7 @@ it.effect.each([ | 247 | 247 | forkedFrom: null, | 248 | 248 | createdAt: old, | 249 | 249 | updatedAt: old, | 250 | | - pendingBackgroundTasks: [{ taskId: "running-task", kind: "command" }], | | 250 | + pendingBackgroundTasks: [{ taskId: "running-task", kind: "subagent" }], | 251 | 251 | }, | 252 | 252 | }); | 253 | 253 | const candidates = yield* store.getSettlementCandidates(); | |
|