fix(runtime): post the agent's blocker reply when a retried finalization blocks the task

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-12 12:44:33 -06:00
1 parent b8207c4ea9
commit 5eb8cdab95
3 files changed
+155 -5

No files matched your search

@@ -1,8 +1,10 @@
import { QueryBuilder } from "drizzle-orm/pg-core";
import { alias, QueryBuilder } from "drizzle-orm/pg-core";
import { and, asc, eq, exists, gt, inArray, isNotNull, isNull, ne, notExists, or, sql } from "drizzle-orm";
import { chatActions, heartbeatRunEvents, heartbeatRuns, issues, nativeRunFinalizations, nativeRunResults, statusDecisions, statusDecisionEffects } from "@paperclipai/db";
import type { NativeResponsePresentationScope } from "../native-response-presentation-notifications.js";
const laterDecisions = alias(statusDecisions, "later_status_decisions");
export function nativeResponsePresentationCandidates(scope?: NativeResponsePresentationScope, after?: string, runId?: string) {
const db = new QueryBuilder();
return db.select({ id: heartbeatRuns.id, companyId: heartbeatRuns.companyId,
@@ -11,7 +13,7 @@ export function nativeResponsePresentationCandidates(scope?: NativeResponsePrese
// Durable publication retry schedule for an ordinary final task reply.
publicationNextAttemptAt: sql<string | null>`${heartbeatRuns.resultJson} #>> '{responsePublication,nextAttemptAt}'`.as("publication_next_attempt_at"),
// Agent Chat conversations keep their completion outbox as the bounded owner.
taskReply: sql<boolean>`coalesce(${nativeRunResults.resultJson} #>> '{result,reportedWorkDisposition}' in ('done', 'needs_review')
taskReply: sql<boolean>`coalesce(${nativeRunResults.resultJson} #>> '{result,reportedWorkDisposition}' in ('done', 'needs_review', 'blocked')
and (coalesce(${issues.conversationAgentId}::text, '') = '' or coalesce(${issues.conversationUserId}, '') = ''), false)`.as("task_reply"),
}).from(nativeRunFinalizations)
.innerJoin(heartbeatRuns, and(eq(heartbeatRuns.id, nativeRunFinalizations.runId),
@@ -67,6 +69,14 @@ export function nativeResponsePresentationCandidates(scope?: NativeResponsePrese
and(sql`(coalesce(${issues.conversationAgentId}::text, '') = '' or coalesce(${issues.conversationUserId}, '') = '')`,
sql`${nativeRunResults.resultJson} #>> '{result,reportedWorkDisposition}' = 'needs_review'`,
sql`${nativeRunResults.resultJson} #>> '{terminal,reportedWorkDisposition}' = 'needs_review'`),
and(sql`(coalesce(${issues.conversationAgentId}::text, '') = '' or coalesce(${issues.conversationUserId}, '') = '')`,
sql`${nativeRunResults.resultJson} #>> '{result,reportedWorkDisposition}' = 'blocked'`,
sql`${nativeRunResults.resultJson} #>> '{terminal,reportedWorkDisposition}' = 'blocked'`,
eq(issues.status, "blocked"),
notExists(db.select({ id: laterDecisions.id }).from(laterDecisions).where(and(
eq(laterDecisions.companyId, statusDecisions.companyId), eq(laterDecisions.issueId, statusDecisions.issueId),
eq(laterDecisions.applicationState, "applied"),
gt(laterDecisions.decisionVersion, statusDecisions.decisionVersion))))),
and(sql`(coalesce(${issues.conversationAgentId}::text, '') <> '' and coalesce(${issues.conversationUserId}, '') <> '')`,
sql`${nativeRunResults.resultJson} #>> '{result,reportedWorkDisposition}' = 'yielded'`,
sql`${nativeRunResults.resultJson} #>> '{terminal,reportedWorkDisposition}' = 'yielded'`),