Repository navigation
fix(server): cancel approval-blocked tools when runs end #40848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2bcbd16
3d14902
147b227
6613295
ed20a09
17a4a38
f2c8283
190c7de
4aaff29
4dc9446
46db9d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| import type { ProjectThreadAwarenessV2Input } from "@t3tools/shared/agentAwareness"; | ||
| import { applyOrchestrationV2ThreadVisit, pendingGuestMessageCount } from "@t3tools/contracts"; | ||
| import { | ||
| applyOrchestrationV2ThreadVisit, | ||
| isOrchestrationV2WorkActive, | ||
| pendingGuestMessageCount, | ||
| } from "@t3tools/contracts"; | ||
| import { applyThreadPullRequestLinkUpdate } from "../sourceControl/threadPullRequests.ts"; | ||
| import { projectTurnItemForWire } from "./WireProjection.ts"; | ||
| import * as Stream from "effect/Stream"; | ||
|
|
@@ -1195,11 +1199,20 @@ export function applyToProjection( | |
| ...base, | ||
| messages: upsertById(base.messages, event.payload), | ||
| }; | ||
| case "turn-item.updated": | ||
| case "turn-item.updated": { | ||
| const previous = base.turnItems.find((item) => item.id === event.payload.id); | ||
| // A delayed tool_use frame cannot restart work cancelled before execution. | ||
| const item = | ||
| previous?.toolNonExecutionKind === "cancelled" && | ||
| (previous.type === "command_execution" || previous.type === "dynamic_tool") && | ||
| isOrchestrationV2WorkActive(event.payload.status) | ||
| ? previous | ||
| : event.payload; | ||
| return withLocalVisibleTurnItems({ | ||
| ...base, | ||
| turnItems: upsertById(base.turnItems, event.payload), | ||
| turnItems: upsertById(base.turnItems, item), | ||
| }); | ||
| } | ||
| case "plan.updated": | ||
| return { | ||
| ...base, | ||
|
|
@@ -3190,6 +3203,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | |
| updated_at = excluded.updated_at, | ||
| payload_json = excluded.payload_json, | ||
| inline_work_output = excluded.inline_work_output | ||
| WHERE NOT ( | ||
| orchestration_v2_projection_turn_items.type IN ('command_execution', 'dynamic_tool') | ||
| AND CASE WHEN orchestration_v2_projection_turn_items.status = 'cancelled' | ||
| THEN json_extract(orchestration_v2_projection_turn_items.payload_json, '$.toolNonExecutionKind') IS 'cancelled' | ||
| ELSE FALSE END | ||
| AND excluded.status IN ('pending', 'running', 'waiting') | ||
| ) | ||
|
Comment on lines
+3206
to
+3212
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not publish active updates that the canonical projection rejects. The SQL guard preserves the cancelled row, but Filter these updates against canonical state before appending and publishing them, or publish the effective item instead. Keep normal updates for tools without a cancelled-before-execution row.
|
||
| `; | ||
| break; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -336,6 +336,24 @@ export const layer: Layer.Layer< | |||||||||||
| ), | ||||||||||||
| Effect.catchCause(() => Effect.succeed(input.inheritedBackgroundTurnItems)), | ||||||||||||
| ), | ||||||||||||
| loadRunOwnedTools: Effect.fn("ProviderTurnStartService.loadRunOwnedTools")(function* () { | ||||||||||||
| const { turnItems, runtimeRequests } = yield* projectionStore.getThreadRecords( | ||||||||||||
| input.threadId, | ||||||||||||
| ["turnItems", "runtimeRequests"], | ||||||||||||
| { | ||||||||||||
| turnItemRunIds: [input.runId], | ||||||||||||
| turnItemTypes: ["command_execution", "dynamic_tool", "approval_request"], | ||||||||||||
| turnItemStatuses: ["pending", "running", "waiting", "cancelled", "failed"], | ||||||||||||
| runtimeRequestStatuses: ["pending", "expired", "cancelled"], | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scope the approval read to the run being finalized.
|
||||||||||||
| }, | ||||||||||||
| ); | ||||||||||||
| const nodeIds = turnItems.flatMap((item) => (item.nodeId === null ? [] : [item.nodeId])); | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include nodes of pending runtime requests in the finalization node lookup. The node lookup uses only the node IDs of the loaded turn items. Approval producers write the Add the nodes of the loaded runtime requests to the lookup: Suggested fix
Committable suggestion
|
||||||||||||
| if (nodeIds.length === 0) return { turnItems, nodes: [], runtimeRequests }; | ||||||||||||
| const { nodes } = yield* projectionStore.getThreadRecords(input.threadId, ["nodes"], { | ||||||||||||
| nodeIds, | ||||||||||||
| }); | ||||||||||||
| return { turnItems, nodes, runtimeRequests }; | ||||||||||||
| }), | ||||||||||||
| shouldStartProviderTurn: () => isCurrentAttemptInStatus("running"), | ||||||||||||
| shouldFinalizeRun: () => isCurrentAttemptIn(["starting", "running"]), | ||||||||||||
| hasUnpairedRunInterruptRequest: () => | ||||||||||||
|
|
@@ -2244,6 +2262,7 @@ export const layer: Layer.Layer< | |||||||||||
| attemptId: attempt.id, | ||||||||||||
| baseline, | ||||||||||||
| loadInheritedBackgroundTurnItems: runControls.loadInheritedBackgroundTurnItems, | ||||||||||||
| loadRunOwnedTools: runControls.loadRunOwnedTools, | ||||||||||||
| relatedThreadIds: routableSubagents.flatMap((subagent) => | ||||||||||||
| subagent.childThreadId === null ? [] : [subagent.childThreadId], | ||||||||||||
| ), | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exclude declined approvals from the "tool started" check.
A declined approval is persisted as
resolvedwithdecision: "decline". The guard treats everyresolvedapproval as proof that the tool started, so it drops the parent tool's cancellation. The denial path does not cancel the parent tool, so that tool can remain running after the run ends. Only an accepted approval should suppress the cancellation. Declined and cancelled decisions should not.Suggested fix