fix(server): a held or unread delegated message tells its owner #47979
ClosedAlb11747(GitHub profile, opens in a new tab)wants to merge 7 commits into
main fromrefs/pull/47979/headopened View on GitHubtanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -0,0 +35,21 @@35+ const wakeOwner = Effect.fnUntraced(function* (task: OrchestrationV2Subagent) {36+ const childThreadId = task.childThreadId;37+ if (childThreadId === null) return;38+ const { runs } = yield* projections.getThreadRecords(childThreadId, ["runs"], {39+ runStatuses: ["queued"],40+ });41+ const held = runs.find((run) => run.delegatedTaskId === task.id && run.queueHeld === true);42+ if (held === undefined) return;43+ const { messages } = yield* projections.getThreadRecords(childThreadId, ["messages"], {44+ messageIds: [held.userMessageId],45+ messageRoles: ["user"],46+ });47+ // An owner that sent into an already-held queue read so in its send receipt.48+ const createdHeld =49+ DateTime.toEpochMillis(task.startedAt ?? task.updatedAt) ===50+ DateTime.toEpochMillis(task.updatedAt);51+ if (createdHeld && messages[0]?.senderThreadId === task.threadId) return;52+ const owner = yield* projections.getThread(task.threadId);53+ if (owner.archivedAt !== null || owner.deletedAt !== null) return;54+ // One notice per hold; the orchestrator drops it as stale once the run is no longer held.55+ const key = `${task.id}:${held.id}:${DateTime.toEpochMillis(task.updatedAt)}`;
- @@ -0,0 +31,21 @@31+const make = Effect.gen(function* () {32+ const engine = yield* Orchestrator.OrchestratorV2;33+ const projections = yield* ProjectionStore.ProjectionStoreV2;34+35+ const wakeOwner = Effect.fnUntraced(function* (task: OrchestrationV2Subagent) {36+ const childThreadId = task.childThreadId;37+ if (childThreadId === null) return;38+ const { runs } = yield* projections.getThreadRecords(childThreadId, ["runs"], {39+ runStatuses: ["queued"],40+ });41+ const held = runs.find((run) => run.delegatedTaskId === task.id && run.queueHeld === true);42+ if (held === undefined) return;43+ const { messages } = yield* projections.getThreadRecords(childThreadId, ["messages"], {44+ messageIds: [held.userMessageId],45+ messageRoles: ["user"],46+ });47+ // An owner that sent into an already-held queue read so in its send receipt.48+ const createdHeld =49+ DateTime.toEpochMillis(task.startedAt ?? task.updatedAt) ===50+ DateTime.toEpochMillis(task.updatedAt);51+ if (createdHeld && messages[0]?.senderThreadId === task.threadId) return;@@ -0,0 +42,21 @@42+ if (held === undefined) return;43+ const { messages } = yield* projections.getThreadRecords(childThreadId, ["messages"], {44+ messageIds: [held.userMessageId],45+ messageRoles: ["user"],46+ });47+ // An owner that sent into an already-held queue read so in its send receipt.48+ const createdHeld =49+ DateTime.toEpochMillis(task.startedAt ?? task.updatedAt) ===50+ DateTime.toEpochMillis(task.updatedAt);51+ if (createdHeld && messages[0]?.senderThreadId === task.threadId) return;52+ const owner = yield* projections.getThread(task.threadId);53+ if (owner.archivedAt !== null || owner.deletedAt !== null) return;54+ // One notice per hold; the orchestrator drops it as stale once the run is no longer held.55+ const key = `${task.id}:${held.id}:${DateTime.toEpochMillis(task.updatedAt)}`;56+ const label = reportLabel(task.title?.trim() || task.prompt);57+ yield* engine.dispatch({58+ type: "message.dispatch",59+ commandId: CommandId.make(`server:delegated-task-held:${key}`),60+ threadId: task.threadId,61+ messageId: MessageId.make(`message:delegated-task-held:${key}`),62+ text: `Delegated task ${task.id} has not run: its message is held in the queue of thread ${childThreadId}. T3 holds a thread's queue when the thread is stopped (task_cancel on another of its tasks stops it too) or its provider fails. A person's message or Resume in that thread releases it. You can release it too: a t3_thread_send with mode "restart" to that thread runs ahead of the hold, and once that run completes the held agent messages follow, unless a person's own message is also held there. If the task is no longer needed, cancel it with task_cancel; otherwise restart the thread or tell the user it is waiting.`,
Pushedef6c60c
- @@ -0,0 +79,21 @@79+ return;80+ yield* wakeOwner(task);81+ },82+ (effect, event) =>83+ effect.pipe(84+ Effect.catchCause((cause) =>85+ Effect.logWarning("telling the owner of a held delegated task failed", {86+ threadId: event.threadId,87+ cause,88+ }),89+ ),90+ ),91+ );92+93+ const start: DelegatedTaskHoldReactor["Service"]["start"] = Effect.fn(94+ "DelegatedTaskHoldReactor.start",95+ )(function* () {96+ // Keyed by owner, so a busy owner's lock delays only its own wakes.97+ const ownerReactions = yield* KeyedLock.makeRunner<ThreadId>();98+ yield* forkParked(99+ Stream.runForEach(engine.streamDomainEvents, (event) =>
juliusmarminge added labelstakeover:in-progress
Pushedfaaa7e2
- @@ -0,0 +28,21 @@28+>()("t3/orchestration-v2/DelegatedTaskHoldReactor") {}29+30+const make = Effect.gen(function* () {31+ const engine = yield* Orchestrator.OrchestratorV2;32+ const projections = yield* ProjectionStore.ProjectionStoreV2;33+34+ const wakeOwner = Effect.fn("DelegatedTaskHoldReactor.wakeOwner")(function* (35+ task: OrchestrationV2Subagent,36+ ) {37+ const childThreadId = task.childThreadId;38+ if (childThreadId === null) return;39+ const { runs } = yield* projections.getThreadRecords(childThreadId, ["runs"], {40+ runStatuses: ["queued"],41+ });42+ const held = runs.find((run) => run.delegatedTaskId === task.id && run.queueHeld === true);43+ if (held === undefined) return;44+ const owner = yield* projections.getThread(task.threadId);45+ if (owner.archivedAt !== null || owner.deletedAt !== null) return;46+ // One notice per held run, whatever else updates the task (a replayed command is a no-op).47+ // The orchestrator drops it as stale once the run is no longer held.48+ const key = `${task.id}:${held.id}`;
- @@ -0,0 +81,21 @@81+ },82+ (effect, event) =>83+ effect.pipe(84+ Effect.catchCause((cause) =>85+ Effect.logWarning("telling the owner of a held delegated task failed", {86+ threadId: event.threadId,87+ cause,88+ }),89+ ),90+ ),91+ );92+93+ const start: DelegatedTaskHoldReactor["Service"]["start"] = Effect.fn(94+ "DelegatedTaskHoldReactor.start",95+ )(function* () {96+ // Keyed by owner, so a busy owner's lock delays only its own wakes.97+ const ownerReactions = yield* KeyedLock.makeRunner<ThreadId>();98+ // Read from before activation: startup recovery can hold a delegated run before then.99+ const events = yield* engine.domainEventsFromNow.pipe(Effect.orDie);100+ yield* forkParked(101+ Stream.runForEach(events, (event) =>@@ -2591,3 +2596,15 @@25912596 ["completed", "failed", "cancelled", "interrupted"].includes(task.status)25922597 )25932598 return true;2599+ // DelegatedTaskHoldReactor's notice holds only while the task's message does. The child2600+ // releases its queue before the parent's task leaves idle, so read the child's run.2601+ if (messageId.startsWith("message:delegated-task-held:")) {2602+ if (task.completionDelivery?.state === "disposed") return true;2603+ const { runs } = yield* projectionStore2604+ .getThreadRecords(childThreadId, ["runs"], { runStatuses: ["queued"] })2605+ .pipe(2606+ Effect.mapError(2607+ (cause) => new OrchestratorProjectionError({ threadId: childThreadId, cause }),2608+ ),2609+ );2610+ return !runs.some((run) => run.delegatedTaskId === task.id && run.queueHeld === true);
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
Alb11747(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Codex Review Summary: ✅ Completed
This comment shows the latest Codex review activity on this pull request.
faaa7e2ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.