GeezerVison is showing github.com — fix(server): agent auto sends steer past an explicit queue (#43357) · patroza/t3code@5fda4f7
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/Orchestrator.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7678,14 +7678,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 7678 | 7678 | dispatchMode = { type: "queue_after_active" }; | 7679 | 7679 | } | 7680 | 7680 | // An agent's automatic send must not overtake its own earlier message still queued | 7681 | | - // (e.g. behind /compact), or the agent receives them out of send order. | | 7681 | + // (e.g. behind /compact), or the agent receives them out of send order. A message it | | 7682 | + // explicitly queued as a later turn is no such barrier. | 7682 | 7683 | if ( | 7683 | 7684 | command.deliveryIntent === "auto" && | 7684 | 7685 | command.createdBy === "agent" && | 7685 | 7686 | (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") | 7686 | 7687 | ) { | 7687 | 7688 | const queuedRuns = projection.runs.filter( | 7688 | | - (run) => run.status === "queued" && run.queueHeld !== true, | | 7689 | + (run) => run.status === "queued" && run.queueHeld !== true && run.explicitQueue !== true, | 7689 | 7690 | ); | 7690 | 7691 | if (queuedRuns.length > 0) { | 7691 | 7692 | const queuedMessages = yield* projectionStore | @@ -8292,6 +8293,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 8292 | 8293 | rootNodeId, | 8293 | 8294 | activeAttemptId: attemptId, | 8294 | 8295 | status: "queued", | | 8296 | + ...(command.explicitQueue === true ? { explicitQueue: true as const } : {}), | 8295 | 8297 | ...(steerWhenReadyRunId === undefined ? {} : { steerWhenReadyRunId }), | 8296 | 8298 | // A restart cancels its run; nobody knows whether the provider read the steer, so a person decides. | 8297 | 8299 | ...(worktreeMissingRun !== undefined || | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.test.ts +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3982,6 +3982,29 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { | 3982 | 3982 | "keeps restart-cancelled work through a stale run.updated", | 3983 | 3983 | () => restartCancelledWorkSurvivesStaleRunUpdate, | 3984 | 3984 | ); | | 3985 | + it.effect("keeps a run's explicit queue marker through a run.updated that omits it", () => | | 3986 | + Effect.gen(function* () { | | 3987 | + const store = yield* ProjectionStore.ProjectionStoreV2; | | 3988 | + const threadId = yield* addRolledBackRecoveryCandidate("explicit-queue-stale-update"); | | 3989 | + const run = (yield* store.getThreadProjection(threadId)).runs[0]!; | | 3990 | + const now = yield* DateTime.now; | | 3991 | + const update = (id: string, payload: typeof run) => | | 3992 | + store.apply({ | | 3993 | + id: EventId.make(`event:explicit-queue-stale-update:${id}`), | | 3994 | + type: "run.updated", | | 3995 | + threadId, | | 3996 | + runId: run.id, | | 3997 | + providerInstanceId, | | 3998 | + occurredAt: now, | | 3999 | + payload, | | 4000 | + }); | | 4001 | + yield* update("marked", { ...run, explicitQueue: true }); | | 4002 | + yield* update("completed", { ...run, status: "completed", completedAt: now }); | | 4003 | + const updated = (yield* store.getThreadProjection(threadId)).runs[0]; | | 4004 | + assert.equal(updated?.status, "completed"); | | 4005 | + assert.equal(updated?.explicitQueue, true); | | 4006 | + }), | | 4007 | + ); | 3985 | 4008 | it.effect("records restart-cancelled work without regressing a run that completed since", () => | 3986 | 4009 | Effect.gen(function* () { | 3987 | 4010 | const store = yield* ProjectionStore.ProjectionStoreV2; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.ts +9-3Lines changed: 9 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -998,6 +998,9 @@ function preserveRunRecordedFields( | 998 | 998 | current.restartCancelledBackgroundWork !== undefined | 999 | 999 | ? { restartCancelledBackgroundWork: current.restartCancelledBackgroundWork } | 1000 | 1000 | : {}), | | 1001 | + ...(next.explicitQueue === undefined && current.explicitQueue !== undefined | | 1002 | + ? { explicitQueue: current.explicitQueue } | | 1003 | + : {}), | 1001 | 1004 | // Only a submission sets the output; a lifecycle write carries it as it was when read. | 1002 | 1005 | ...(current.output !== undefined ? { output: current.output } : {}), | 1003 | 1006 | }; | @@ -2437,7 +2440,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 2437 | 2440 | THEN json_set( | 2438 | 2441 | ${payload}, | 2439 | 2442 | ${path}, | 2440 | | - json_extract(orchestration_v2_projection_runs.payload_json, ${path}) | | 2443 | + orchestration_v2_projection_runs.payload_json -> ${path} | 2441 | 2444 | ) | 2442 | 2445 | ELSE ${payload} | 2443 | 2446 | END | @@ -2668,8 +2671,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 2668 | 2671 | completed_at = excluded.completed_at, | 2669 | 2672 | payload_json = ${keepRecordedRunOutput( | 2670 | 2673 | keepRecordedRunField( | 2671 | | - keepRecordedRunField(sql`excluded.payload_json`, "$.delegatedCompletion"), | 2672 | | - "$.restartCancelledBackgroundWork", | | 2674 | + keepRecordedRunField( | | 2675 | + keepRecordedRunField(sql`excluded.payload_json`, "$.delegatedCompletion"), | | 2676 | + "$.restartCancelledBackgroundWork", | | 2677 | + ), | | 2678 | + "$.explicitQueue", | 2673 | 2679 | ), | 2674 | 2680 | )} | 2675 | 2681 | `; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4494,3 +4494,62 @@ it.effect("a side chat that opens with /goal gets its context on the next questi | 4494 | 4494 | }).pipe(Effect.provide(layer)); | 4495 | 4495 | }), | 4496 | 4496 | ); | | 4497 | + | | 4498 | +it.effect("an agent's auto send steers past its own explicitly queued message", () => | | 4499 | + Effect.gen(function* () { | | 4500 | + const name = "agent-auto-after-explicit-queue"; | | 4501 | + const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(name); | | 4502 | + yield* Effect.gen(function* () { | | 4503 | + const orchestrator = yield* Orchestrator.OrchestratorV2; | | 4504 | + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; | | 4505 | + const manager = yield* ThreadManagementService.ThreadManagementService; | | 4506 | + const threadId = yield* startFirstTurn; | | 4507 | + const send = (text: string, mode: "auto" | "queue") => | | 4508 | + manager.sendToThread({ | | 4509 | + projectId: ProjectId.make(`project:${name}`), | | 4510 | + threadId, | | 4511 | + commandId: CommandId.make(text), | | 4512 | + messageId: MessageId.make(text), | | 4513 | + text, | | 4514 | + attachments: [], | | 4515 | + mode, | | 4516 | + createdBy: "agent", | | 4517 | + creationSource: "mcp", | | 4518 | + senderThreadId: ThreadId.make("thread:sender"), | | 4519 | + }); | | 4520 | + const later = yield* send("later", "queue"); | | 4521 | + const now = yield* send("now", "auto"); | | 4522 | + yield* worker.drain(); | | 4523 | + | | 4524 | + const projection = yield* orchestrator.getThreadProjection(threadId); | | 4525 | + assert.deepEqual( | | 4526 | + { | | 4527 | + later: later.delivery, | | 4528 | + now: now.delivery, | | 4529 | + targetsRunningTurn: now.run.id === started[0]?.runId, | | 4530 | + adapterReceived: steered.map((text) => text.split("\n").at(-1)), | | 4531 | + queuedRunIds: projection.runs | | 4532 | + .filter((run) => run.status === "queued") | | 4533 | + .map((run) => run.id), | | 4534 | + }, | | 4535 | + { | | 4536 | + later: "queued", | | 4537 | + now: "steered", | | 4538 | + targetsRunningTurn: true, | | 4539 | + adapterReceived: ["now"], | | 4540 | + queuedRunIds: [later.run.id], | | 4541 | + }, | | 4542 | + ); | | 4543 | + }).pipe( | | 4544 | + Effect.provide( | | 4545 | + ThreadManagementService.layer.pipe( | | 4546 | + Layer.provideMerge(layer), | | 4547 | + Layer.provide(ServerSettings.layerTest()), | | 4548 | + Layer.provide( | | 4549 | + CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)), | | 4550 | + ), | | 4551 | + ), | | 4552 | + ), | | 4553 | + ); | | 4554 | + }), | | 4555 | +); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadManagementService.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -986,6 +986,7 @@ const make = Effect.gen(function* () { | 986 | 986 | ...(input.outputSchema === undefined ? {} : { outputSchema: input.outputSchema }), | 987 | 987 | dispatchMode, | 988 | 988 | ...(mode === "auto" ? { deliveryIntent: "auto" } : {}), | | 989 | + ...(mode === "queue" ? { explicitQueue: true as const } : {}), | 989 | 990 | createdBy: input.createdBy, | 990 | 991 | creationSource: input.creationSource, | 991 | 992 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/contracts/src/orchestrationV2.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -830,6 +830,8 @@ export const OrchestrationV2Run = Schema.Struct({ | 830 | 830 | status: OrchestrationV2RunStatus, | 831 | 831 | queuePosition: Schema.optional(Schema.NullOr(PositiveInt)), | 832 | 832 | queueHeld: Schema.optional(Schema.Boolean), | | 833 | + /** The sender asked for a separate turn; it is not an ordering barrier for a later auto send. */ | | 834 | + explicitQueue: Schema.optional(Schema.Literal(true)), | 833 | 835 | steerWhenReadyRunId: Schema.optional(RunId), | 834 | 836 | requestedAt: Schema.DateTimeUtc, | 835 | 837 | startedAt: Schema.NullOr(Schema.DateTimeUtc), | @@ -3637,6 +3639,7 @@ export const OrchestrationV2Command = Schema.Union([ | 3637 | 3639 | steerFollowUpAfterRunOrdinal: Schema.optional(NonNegativeInt), | 3638 | 3640 | usageLimitRecoveryRequestId: Schema.optional(CommandId), | 3639 | 3641 | deliveryIntent: Schema.optional(Schema.Literals(["auto", "steer", "restart"])), | | 3642 | + explicitQueue: Schema.optional(Schema.Literal(true)), | 3640 | 3643 | delegatedCompletion: Schema.optional( | 3641 | 3644 | Schema.Struct({ | 3642 | 3645 | parentRunId: RunId, | |
|