mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-04 19:28:46 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Questions and review decisions must work in the conversation where a task started. > - Slack omitted question context and did not publish several review kinds. > - Some answers saved in Paperclip also lost their authorized continuation or their final Slack reply. > - A settled card and a successful run did not prove that the user received the result. > - This pull request preserves complete questions and follows the existing answer and delivery receipts through continuation. > - Users can read the request, submit an authorized decision, and receive the resumed task's result in the same conversation. ## Linked Issues or Issue Description Refs #17045 and #17218. **What happened?** Slack question cards could omit instructions and editable starting text. Checkbox reviews, item verdicts, task suggestions, and connection requests had no publication path. Complex question answers submitted in Paperclip could fail continuation authorization. Several review continuations completed without delivering their result to Slack. **Expected behavior** Show the actual question and context. Use native Slack controls when they preserve the form contract. Otherwise provide an explicit Paperclip review link. Persist one authorized response, resume through the existing delivery path, settle the card, and deliver the useful result to its originating conversation. **Steps to reproduce** 1. Start a task in Slack and ask a text or choice question with instructions and help text. 2. Submit native text and choice responses, then submit a complex form through Paperclip. 3. Accept a checklist, submit item decisions, select suggested tasks, or decline a connection request. 4. Inspect the saved answer, continuation run, original Slack card, and final Slack reply independently. ## What Changed - Preserve full prompts, context, descriptions, recommendations, help, and editable starting text. Use direct buttons for short choices, inline searchable selects for longer lists, and native checkboxes with explicit submission for multiple choices. - Add native custom-answer and validated-text modals, ordinary confirmation buttons, checklist decisions, per-item verdicts with reasons, suggested-task creation/dismissal, and connection decline. Keep governed approvals, permission grants, and forms that cannot be represented faithfully in the full Paperclip review surface. - Keep Slack-specific canonical-validation feedback in its owning platform module. Shared callbacks delegate without interpreting provider presentation. - Reuse the existing canonical interaction transactions, callback receipts, publication queue, and continuation delivery. Share ordinary confirmation handling across Telegram and Slack. Recheck current authorization and commit response receipts atomically. - Settle cards with the saved answer and remove stale controls. Ignore passive Slack link callbacks so opening a task does not display a false answer error. - Validate exact multi-select receipt mappings during native continuation. Recover automation-origin connection replies from their published prompt after validating the durable connection receipt. - Keep oversized multi-select handoffs free of unused native tokens, and retain the complete Paperclip form for 101–128 choices plus Other. Make the original-conversation audience explicit before submitting a Slack modal. - Add rollback, provider serialization, callback, continuation, and negative authorization regressions. Update the connector playbook and feature/access maps. No React or application UI changes are included. - Add a durable Q01–Q11 qualification index and provider coverage matrix. Require distinct functional, good-experience, and best-available-control assessments; real unanswered/answering/settled evidence; saved results, continuation and navigation proof; and retained failures. Mark other providers unaudited rather than assuming Slack parity. ## Verification Candidate: `e0017d446b1016a38d65382366cd1408cce33039`. The eleven-core-format live Slack gallery is pinned to `c3d458b2a06b660448940fd8cf33c5906f0058d5`; subsequent boundary fixes and the sharing notice have automated verification, not new live screenshots. - Real Slack UI testing covered all 11 formats: text, closed choice, long single select, multiple choices, choices with a custom answer, pattern-validated text, confirmation, checklist, item verdicts, suggested tasks, and connection requests. Each has unanswered/answering/answered screenshots as applicable, an inspected saved result, one continuation, and an individual experience review in the local HTML gallery. - The dedicated private QA app/channel used signed callbacks to the local candidate. A separately approved password-protected, read-only preview proved connection-review, suggested-task review, and created-task navigation. Anonymous access, writes, and credential routes were denied. No OAuth grant was attempted. - Real testing exposed and repaired a multi-select continuation failure and a misleading error on link-button clicks. Failed attempts remain in the evidence. The final navigation replay opened the exact pending connection request and declined it in Slack without granting access. Suggested-task creation produced the selected child with the correct parent and description. - Final review-fix verification: **32 form tests**, **28 provider/form-rendering tests**, **30 publication tests**, and **171 native continuation/authorization tests** passed. Earlier **3 atomic-resolution rollback**, **9 targeted callback/publication/Telegram**, and **28 review-continuation** cases passed. The final Slack/Teams modal subset also passed. The automation-origin connection cases, 101/128 custom-choice cases, and oversized multi-select continuation reproduced before their fixes and passed afterward; failing logs remain retained. These counts describe relevant runs, not a reliability percentage. - CI on `b5885b85fd` exposed a test fixture that assumed every Slack modal block was an input; the new sharing notice correctly renders a non-input block. The fixture now selects input blocks safely. The exact signed-modal correction/one-continuation test passed locally, together with 14 Slack form/modal tests. The failing CI log and its specific cause remain recorded. - `pnpm -r typecheck` and `pnpm build` passed. Server typechecking was repeated after the final source changes. Map and module-boundary checks and `git diff --check` passed. - The full `pnpm test:run` was attempted and stopped after repeated embedded PostgreSQL semaphore exhaustion on this host. Its 1,395-test chat suite found two stale native-control assertions; both were corrected and re-tested in a focused run. Focused database suites used separate disposable databases on existing local PostgreSQL. The full suite is not claimed green; new-head CI must complete before merge. - Final-head CI passed on `e0017d446b1016a38d65382366cd1408cce33039`: all test/build jobs and required `ci / verify` and `ci / e2e` gates are green ([run](https://github.com/paperclipai/paperclip/actions/runs/40115430194)). Greptile and Ponytail both reviewed this exact head at 5/5, the security scan passed, and all review threads are resolved. - All three QA app callbacks were restored to staging. Both temporary tunnels and local proxies were closed. The local endpoint was archived and its local credential copies removed. The candidate app was stopped. Limits: provider execution used a deterministic Claude protocol fixture, not an LLM. This is local real-provider UX proof, not deployed staging or broader Codie/Claude qualification. The public preview intentionally blocked ancillary artifact routes, so its destination pages could show a partial-history warning. The complete permission-grant flow and every oversized/mixed-form branch were not driven manually. Earlier fixture, test, and provider failures remain recorded. ## Risks - New reply routes require durable origin proof. Incorrect receipt matching could lose a reply or route it incorrectly. Tests cover missing, forged, mismatched, and revoked authority. - Governed forms keep their Paperclip resolution checks. This change does not grant new approval authority to Slack. - Existing completed review results can become deliverable through the repaired publication path without another provider run. - No schema, lockfile, workflow, public task-status model, or runtime-instruction change is included. This branch is not deployed. ## Model Used OpenAI Codex, GPT-6 family, with code execution and tool use. The exact backend model ID and context-window size are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either linked existing issues or described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal Paperclip ticket id - [x] I have run focused tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green on the new head - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups on the new head - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
953 lines
37 KiB
TypeScript
953 lines
37 KiB
TypeScript
import { imessageProgressSupersessionReason } from "./imessage-photon/publications.js";
|
|
import { fastResponseRequests } from "@paperclipai/db";
|
|
|
|
import {
|
|
and,
|
|
desc,
|
|
eq,
|
|
gt,
|
|
inArray,
|
|
isNotNull,
|
|
isNull,
|
|
like,
|
|
ne,
|
|
or,
|
|
sql,
|
|
} from "drizzle-orm";
|
|
import { alias } from "drizzle-orm/pg-core";
|
|
|
|
import type { Db } from "@paperclipai/db";
|
|
|
|
import {
|
|
agentWakeupRequests,
|
|
chatEndpoints,
|
|
chatMessageLinks,
|
|
chatPublications,
|
|
heartbeatRuns,
|
|
issueComments,
|
|
issueThreadInteractions,
|
|
issues,
|
|
} from "@paperclipai/db";
|
|
import type {
|
|
SafeChatPublicationPayload,
|
|
} from "@paperclipai/shared";
|
|
import {
|
|
isUuidLike,
|
|
} from "@paperclipai/shared";
|
|
|
|
import {
|
|
NativeChatReviewPresentationContentionError,
|
|
} from "../native-runtime/native-chat-review-presentation.js";
|
|
import { isExternalChatWaitAuthorizationContention } from "../native-runtime/chat-attachment-reuse.js";
|
|
|
|
import {
|
|
inboundWakePublicationKey,
|
|
parseInboundWakePublicationKey,
|
|
} from "../chat-inbound-wakeup-publications.js";
|
|
|
|
type EndpointRow = typeof chatEndpoints.$inferSelect;
|
|
type DbOrTransaction = Db | Parameters<Parameters<Db["transaction"]>[0]>[0];
|
|
type ChatPublicationReplacementOptions = {
|
|
endpointRecord: (endpointId: string) => Promise<{ endpoint: EndpointRow } | null>;
|
|
authorizeRetainedChatSourcePublication: (database: DbOrTransaction, publication: typeof chatPublications.$inferSelect) => Promise<boolean>;
|
|
};
|
|
|
|
export function createChatPublicationReplacement(db: Db, options: ChatPublicationReplacementOptions) {
|
|
const { endpointRecord, authorizeRetainedChatSourcePublication } = options;
|
|
|
|
|
|
function runIdFromMilestonePublication(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
): string | null {
|
|
if (!publication.payload.progressState) return null;
|
|
const match =
|
|
/^run:([^:]+):(?:queued|working|waiting_for_input|completed|failed):/.exec(
|
|
publication.idempotencyKey,
|
|
);
|
|
const runId = match?.[1] ?? null;
|
|
// This identifier is also compared against a UUID column below. Old or
|
|
// manually repaired rows must not be allowed to turn the global
|
|
// publication sweep into a PostgreSQL cast error.
|
|
return isUuidLike(runId) ? runId : null;
|
|
}
|
|
|
|
async function runOwnershipMilestoneSupersessionReason(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
): Promise<string | null> {
|
|
const progress = publication.payload.progressState;
|
|
const typingReason = imessageProgressSupersessionReason(progress);
|
|
if (typingReason) {
|
|
const endpoint = await endpointRecord(publication.endpointId);
|
|
if (endpoint?.endpoint.provider === "imessage-photon") return typingReason;
|
|
}
|
|
if (
|
|
!progress ||
|
|
!["queued", "working", "waiting_for_input"].includes(progress)
|
|
) {
|
|
return null;
|
|
}
|
|
const runId = runIdFromMilestonePublication(publication);
|
|
if (!runId) return null;
|
|
const run = await db.transaction(async (tx) => {
|
|
// Use native finalization's issue -> run lock order. If terminalization
|
|
// won, the current status suppresses this obsolete provider update. If
|
|
// this short authorization snapshot wins, the progress was still true
|
|
// at its durable send boundary and the later final remains authoritative.
|
|
const currentIssue = await tx
|
|
.select({ id: issues.id })
|
|
.from(issues)
|
|
.where(
|
|
and(
|
|
eq(issues.id, publication.issueId),
|
|
eq(issues.companyId, publication.companyId),
|
|
),
|
|
)
|
|
.for("update")
|
|
.then((rows) => rows[0] ?? null);
|
|
if (!currentIssue) return null;
|
|
return tx
|
|
.select({
|
|
status: heartbeatRuns.status,
|
|
errorCode: heartbeatRuns.errorCode,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.where(
|
|
and(
|
|
eq(heartbeatRuns.id, runId),
|
|
eq(heartbeatRuns.companyId, publication.companyId),
|
|
sql`${heartbeatRuns.contextSnapshot} ->> 'issueId' = ${publication.issueId}`,
|
|
),
|
|
)
|
|
.for("update")
|
|
.then((rows) => rows[0] ?? null);
|
|
});
|
|
const baseProgressKey = `run:${runId}:${progress}:${publication.endpointId}`;
|
|
const nativeProgressPrefix = `run:${runId}:working:${publication.endpointId}:native:`;
|
|
const isTerminalizableProgress =
|
|
(progress === "queued" || progress === "working") &&
|
|
(publication.idempotencyKey === baseProgressKey ||
|
|
publication.idempotencyKey.startsWith(nativeProgressPrefix));
|
|
if (
|
|
isTerminalizableProgress &&
|
|
run &&
|
|
["succeeded", "interrupted", "failed", "cancelled", "timed_out"].includes(
|
|
run.status,
|
|
)
|
|
) {
|
|
return "Run reached a terminal state before progress delivery";
|
|
}
|
|
// Match only the closed ownership-attention projection. Historical progress
|
|
// without an extant run retains its existing delivery semantics.
|
|
const ownershipBlocked =
|
|
run?.status === "running" &&
|
|
[
|
|
"native_execution_ownership_unverified",
|
|
"native_adopted_runner_authentication_timeout",
|
|
].includes(run.errorCode ?? "");
|
|
if (progress === "waiting_for_input") {
|
|
return ownershipBlocked
|
|
? null
|
|
: "Run ownership attention no longer applies to the bound task";
|
|
}
|
|
return ownershipBlocked
|
|
? "Run progress was superseded by ownership recovery attention"
|
|
: null;
|
|
}
|
|
|
|
async function providerProgressLaneConsumed(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
providerMessageId: string,
|
|
): Promise<boolean> {
|
|
// Published progress rows retain their old provider ID after an edit.
|
|
// Consult that exact message's current outbound link, not the conversation
|
|
// tail: an authored answer or failure has consumed the lane, while an
|
|
// interleaved task-status update may still be replaced normally.
|
|
const [consumed] = await db
|
|
.select({ id: chatPublications.id })
|
|
.from(chatMessageLinks)
|
|
.innerJoin(
|
|
chatPublications,
|
|
and(
|
|
eq(chatPublications.id, chatMessageLinks.publicationId),
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(chatPublications.providerMessageId, providerMessageId),
|
|
eq(chatPublications.state, "published"),
|
|
),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(chatMessageLinks.companyId, publication.companyId),
|
|
eq(chatMessageLinks.endpointId, publication.endpointId),
|
|
eq(chatMessageLinks.conversationId, publication.conversationId),
|
|
eq(chatMessageLinks.direction, "outbound"),
|
|
eq(chatMessageLinks.providerMessageId, providerMessageId),
|
|
or(
|
|
and(
|
|
isNotNull(chatPublications.commentId),
|
|
sql`${chatPublications.payload}->>'progressState' is null`,
|
|
),
|
|
sql`${chatPublications.payload}->>'progressState' = 'failed'`,
|
|
),
|
|
),
|
|
)
|
|
.limit(1);
|
|
return Boolean(consumed);
|
|
}
|
|
|
|
async function committedResponseMayReplaceFailure(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
runId: string,
|
|
providerMessageId: string,
|
|
): Promise<boolean> {
|
|
if (
|
|
!publication.commentId ||
|
|
publication.payload.progressState !== undefined
|
|
)
|
|
return false;
|
|
return db
|
|
.transaction(async (tx) => {
|
|
const [run] = await tx
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(
|
|
and(
|
|
eq(heartbeatRuns.id, runId),
|
|
eq(heartbeatRuns.companyId, publication.companyId),
|
|
eq(heartbeatRuns.nativeIssueId, publication.issueId),
|
|
eq(heartbeatRuns.runtimeMode, "native"),
|
|
eq(heartbeatRuns.status, "succeeded"),
|
|
isNotNull(heartbeatRuns.finishedAt),
|
|
),
|
|
)
|
|
.for("share", { noWait: true });
|
|
const presentation = run?.resultJson?.presentationDecision as
|
|
Record<string, unknown> | undefined;
|
|
if (
|
|
!run?.resultJson?.nativeCommittedChatResponse ||
|
|
presentation?.commentId !== publication.commentId
|
|
)
|
|
return false;
|
|
const [comment] = await tx
|
|
.select({ id: issueComments.id })
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.id, publication.commentId!),
|
|
eq(issueComments.companyId, publication.companyId),
|
|
eq(issueComments.issueId, publication.issueId),
|
|
eq(issueComments.createdByRunId, runId),
|
|
eq(issueComments.authorAgentId, run.agentId),
|
|
isNull(issueComments.deletedAt),
|
|
),
|
|
)
|
|
.for("share", { noWait: true });
|
|
if (!comment) return false;
|
|
const [priorAnswer] = await tx
|
|
.select({ id: chatPublications.id })
|
|
.from(chatPublications)
|
|
.innerJoin(
|
|
issueComments,
|
|
and(
|
|
eq(issueComments.id, chatPublications.commentId),
|
|
eq(issueComments.companyId, publication.companyId),
|
|
eq(issueComments.createdByRunId, runId),
|
|
),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
ne(chatPublications.id, publication.id),
|
|
inArray(chatPublications.state, [
|
|
"published",
|
|
"streaming",
|
|
"delivery_unknown",
|
|
]),
|
|
sql`${chatPublications.payload}->>'progressState' is null`,
|
|
),
|
|
)
|
|
.limit(1);
|
|
if (priorAnswer) return false;
|
|
// A historical progress row can retain this ID after a later edit.
|
|
// Only its CURRENT outbound link may grant this narrow exception; an
|
|
// authored answer or another run's failure must remain consumed.
|
|
const [failure] = await tx
|
|
.select({ id: chatPublications.id })
|
|
.from(chatMessageLinks)
|
|
.innerJoin(
|
|
chatPublications,
|
|
and(
|
|
eq(chatPublications.id, chatMessageLinks.publicationId),
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(chatPublications.providerMessageId, providerMessageId),
|
|
eq(
|
|
chatPublications.idempotencyKey,
|
|
`run:${runId}:failed:${publication.endpointId}`,
|
|
),
|
|
eq(chatPublications.state, "published"),
|
|
isNull(chatPublications.commentId),
|
|
sql`${chatPublications.payload}->>'progressState' = 'failed'`,
|
|
),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(chatMessageLinks.companyId, publication.companyId),
|
|
eq(chatMessageLinks.endpointId, publication.endpointId),
|
|
eq(chatMessageLinks.conversationId, publication.conversationId),
|
|
eq(chatMessageLinks.providerMessageId, providerMessageId),
|
|
eq(chatMessageLinks.direction, "outbound"),
|
|
),
|
|
);
|
|
if (!failure) return false;
|
|
// The marker is read from the authoritative run, never the publication
|
|
// payload. Recheck accepted result/decision plus the complete current
|
|
// source batch, actor, access and epoch before borrowing its failure lane.
|
|
// The transport claim repeats those checks at the I/O boundary.
|
|
return authorizeRetainedChatSourcePublication(tx, publication);
|
|
})
|
|
.catch((error: unknown) => {
|
|
if (isExternalChatWaitAuthorizationContention(error))
|
|
throw new NativeChatReviewPresentationContentionError();
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
async function runPublicationToReplace(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
payload: SafeChatPublicationPayload,
|
|
): Promise<string | null> {
|
|
if (payload.attachmentIds?.length) return null;
|
|
const currentRunId =
|
|
runIdFromMilestonePublication(publication) ??
|
|
(publication.commentId
|
|
? await db
|
|
.select({ runId: issueComments.createdByRunId })
|
|
.from(issueComments)
|
|
.where(eq(issueComments.id, publication.commentId))
|
|
.then((rows) => rows[0]?.runId ?? null)
|
|
: null);
|
|
if (!currentRunId) return null;
|
|
return db
|
|
.select({
|
|
id: chatPublications.id,
|
|
commentId: chatPublications.commentId,
|
|
providerMessageId: chatPublications.providerMessageId,
|
|
payload: chatPublications.payload,
|
|
})
|
|
.from(chatPublications)
|
|
.leftJoin(issueComments, eq(issueComments.id, chatPublications.commentId))
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.state, "published"),
|
|
isNotNull(chatPublications.providerMessageId),
|
|
or(
|
|
like(chatPublications.idempotencyKey, `run:${currentRunId}:%`),
|
|
eq(issueComments.createdByRunId, currentRunId),
|
|
),
|
|
),
|
|
)
|
|
.orderBy(desc(chatPublications.createdAt), desc(chatPublications.id))
|
|
.then(async (rows) => {
|
|
// Progress updates are one replaceable provider-message lane per run.
|
|
// The first durable agent comment may turn that placeholder into the
|
|
// terminal response, but later comments from the same run are distinct
|
|
// user-visible outputs and must be posted separately. Re-editing the
|
|
// placeholder for each comment silently erases the earlier replies.
|
|
if (
|
|
publication.commentId &&
|
|
rows.some(
|
|
(row) =>
|
|
row.commentId !== null && row.payload.progressState === undefined,
|
|
)
|
|
)
|
|
return null;
|
|
const replacement = rows.find(
|
|
(row) =>
|
|
Boolean(row.providerMessageId) &&
|
|
row.payload.progressState !== undefined,
|
|
);
|
|
if (!replacement?.providerMessageId) return null;
|
|
if (
|
|
(await providerProgressLaneConsumed(
|
|
publication,
|
|
replacement.providerMessageId,
|
|
)) &&
|
|
!(await committedResponseMayReplaceFailure(
|
|
publication,
|
|
currentRunId,
|
|
replacement.providerMessageId,
|
|
))
|
|
)
|
|
return null;
|
|
// Replacement identity belongs to the run, not to the provider-visible
|
|
// tail. A status/control reply may legitimately interleave while the run
|
|
// is active; making the tail the edit candidate would strand this run's
|
|
// working placeholder forever. The query is bounded by endpoint,
|
|
// conversation (the task generation), and run id, so an interleaved
|
|
// control or another run can never donate its provider message here.
|
|
return replacement.providerMessageId;
|
|
});
|
|
}
|
|
|
|
async function receiptReactionCompletionRunId(
|
|
tx: Db,
|
|
publication: typeof chatPublications.$inferSelect,
|
|
payload: SafeChatPublicationPayload,
|
|
): Promise<string | null> {
|
|
if (
|
|
payload.progressState === "completed" ||
|
|
payload.progressState === "failed"
|
|
) {
|
|
return runIdFromMilestonePublication(publication);
|
|
}
|
|
if (publication.commentId) {
|
|
const runId = await tx
|
|
.select({ runId: issueComments.createdByRunId })
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.id, publication.commentId),
|
|
eq(issueComments.companyId, publication.companyId),
|
|
),
|
|
)
|
|
.then((rows) => rows[0]?.runId ?? null);
|
|
return isUuidLike(runId) ? runId : null;
|
|
}
|
|
if (
|
|
payload.interactionId &&
|
|
publication.idempotencyKey ===
|
|
`interaction:${payload.interactionId}:${publication.endpointId}`
|
|
) {
|
|
const runId = await tx
|
|
.select({ runId: issueThreadInteractions.sourceRunId })
|
|
.from(issueThreadInteractions)
|
|
.where(
|
|
and(
|
|
eq(issueThreadInteractions.id, payload.interactionId),
|
|
eq(issueThreadInteractions.companyId, publication.companyId),
|
|
eq(issueThreadInteractions.issueId, publication.issueId),
|
|
),
|
|
)
|
|
.then((rows) => rows[0]?.runId ?? null);
|
|
return isUuidLike(runId) ? runId : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function inboundWakePublicationToReplace(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
payload: SafeChatPublicationPayload,
|
|
): Promise<string | null> {
|
|
if (payload.attachmentIds?.length) return null;
|
|
const notice = parseInboundWakePublicationKey(publication.idempotencyKey);
|
|
let wakeId: string | null =
|
|
notice && notice.state !== "queued" ? notice.wakeId : null;
|
|
let runId = runIdFromMilestonePublication(publication);
|
|
if (
|
|
!notice &&
|
|
!runId &&
|
|
payload.interactionId &&
|
|
publication.idempotencyKey ===
|
|
`interaction:${payload.interactionId}:${publication.endpointId}`
|
|
) {
|
|
const [interaction] = await db
|
|
.select({ runId: issueThreadInteractions.sourceRunId })
|
|
.from(issueThreadInteractions)
|
|
.where(
|
|
and(
|
|
eq(issueThreadInteractions.id, payload.interactionId),
|
|
eq(issueThreadInteractions.companyId, publication.companyId),
|
|
eq(issueThreadInteractions.issueId, publication.issueId),
|
|
),
|
|
);
|
|
runId = interaction?.runId ?? null;
|
|
}
|
|
if (!notice && !runId && publication.commentId) {
|
|
const [comment] = await db
|
|
.select({ runId: issueComments.createdByRunId })
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.id, publication.commentId),
|
|
eq(issueComments.companyId, publication.companyId),
|
|
eq(issueComments.issueId, publication.issueId),
|
|
),
|
|
);
|
|
runId = comment?.runId ?? null;
|
|
}
|
|
let runContext: Record<string, unknown> | null = null;
|
|
if (runId) {
|
|
if (publication.commentId) {
|
|
const [priorAnswer] = await db
|
|
.select({ id: chatPublications.id })
|
|
.from(chatPublications)
|
|
.innerJoin(
|
|
issueComments,
|
|
eq(issueComments.id, chatPublications.commentId),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.state, "published"),
|
|
eq(issueComments.createdByRunId, runId),
|
|
sql`${chatPublications.payload}->>'progressState' is null`,
|
|
),
|
|
)
|
|
.limit(1);
|
|
if (priorAnswer) return null;
|
|
}
|
|
const [run] = await db
|
|
.select({
|
|
wakeId: heartbeatRuns.wakeupRequestId,
|
|
context: heartbeatRuns.contextSnapshot,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.innerJoin(
|
|
agentWakeupRequests,
|
|
and(
|
|
eq(agentWakeupRequests.id, heartbeatRuns.wakeupRequestId),
|
|
eq(agentWakeupRequests.runId, heartbeatRuns.id),
|
|
eq(agentWakeupRequests.companyId, heartbeatRuns.companyId),
|
|
eq(agentWakeupRequests.agentId, heartbeatRuns.agentId),
|
|
// Failed/cancelled is the admitted run's outcome, not loss of
|
|
// its original admission. Its terminal milestone still owns
|
|
// this exact lane even if no working update was sent first.
|
|
ne(agentWakeupRequests.status, "skipped"),
|
|
),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(heartbeatRuns.id, runId),
|
|
eq(heartbeatRuns.companyId, publication.companyId),
|
|
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${publication.issueId}`,
|
|
),
|
|
);
|
|
wakeId = run?.wakeId ?? null;
|
|
runContext = run?.context ?? null;
|
|
}
|
|
if (!wakeId) return null;
|
|
const [queued] = await db
|
|
.select({
|
|
commentId: chatPublications.commentId,
|
|
providerMessageId: chatPublications.providerMessageId,
|
|
})
|
|
.from(chatPublications)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(chatPublications.state, "published"),
|
|
isNotNull(chatPublications.providerMessageId),
|
|
eq(
|
|
chatPublications.idempotencyKey,
|
|
inboundWakePublicationKey(
|
|
wakeId,
|
|
"queued",
|
|
publication.endpointId,
|
|
publication.conversationId,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
if (!queued?.commentId || !queued.providerMessageId) return null;
|
|
// The run helper may have rejected this same provider ID because its
|
|
// queued/working placeholder is already an answer or terminal failure.
|
|
// Falling back to the older wake row must not erase that newer output.
|
|
if (
|
|
await providerProgressLaneConsumed(publication, queued.providerMessageId)
|
|
)
|
|
return null;
|
|
if (notice && notice.state !== "queued")
|
|
return queued.commentId === publication.commentId
|
|
? queued.providerMessageId
|
|
: null;
|
|
// A deferred owner can be a Board wake into which the exact external
|
|
// comment coalesced. The run must actually contain that source comment;
|
|
// wake ID alone cannot authorize a different successor's edit lane.
|
|
return runContext &&
|
|
(runContext.wakeCommentId === queued.commentId ||
|
|
runContext.commentId === queued.commentId ||
|
|
(Array.isArray(runContext.wakeCommentIds) &&
|
|
runContext.wakeCommentIds.includes(queued.commentId)))
|
|
? queued.providerMessageId
|
|
: null;
|
|
}
|
|
|
|
async function interactionResolutionPublicationToReplace(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
payload: SafeChatPublicationPayload,
|
|
): Promise<string | null> {
|
|
if (
|
|
!publication.idempotencyKey.startsWith("interaction-resolution:") ||
|
|
!payload.interactionId
|
|
)
|
|
return null;
|
|
return db
|
|
.select({ providerMessageId: chatPublications.providerMessageId })
|
|
.from(chatPublications)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(
|
|
chatPublications.idempotencyKey,
|
|
`interaction:${payload.interactionId}:${publication.endpointId}`,
|
|
),
|
|
eq(chatPublications.state, "published"),
|
|
isNotNull(chatPublications.providerMessageId),
|
|
),
|
|
)
|
|
.then((rows) => rows[0]?.providerMessageId ?? null);
|
|
}
|
|
|
|
async function interactionPromptPublicationToReplace(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
payload: SafeChatPublicationPayload,
|
|
): Promise<string | null> {
|
|
if (
|
|
!payload.interactionId ||
|
|
publication.idempotencyKey !==
|
|
`interaction:${payload.interactionId}:${publication.endpointId}`
|
|
) {
|
|
return null;
|
|
}
|
|
const interaction = await db
|
|
.select({
|
|
kind: issueThreadInteractions.kind,
|
|
sourceRunId: issueThreadInteractions.sourceRunId,
|
|
})
|
|
.from(issueThreadInteractions)
|
|
.where(
|
|
and(
|
|
eq(issueThreadInteractions.id, payload.interactionId),
|
|
eq(issueThreadInteractions.companyId, publication.companyId),
|
|
eq(issueThreadInteractions.issueId, publication.issueId),
|
|
),
|
|
)
|
|
.then((rows) => rows[0] ?? null);
|
|
if (!interaction?.sourceRunId) return null;
|
|
|
|
// A provider-visible interaction is this run's response, not an additional
|
|
// message beside its progress indicator. Move the durable interaction
|
|
// publication onto the exact source run's provider-message lane. This
|
|
// retires both the normal working placeholder and the queued placeholder
|
|
// when a very fast run asks before the working update is published.
|
|
for (const progressState of ["working", "queued"] as const) {
|
|
const providerMessageId = await db
|
|
.select({ providerMessageId: chatPublications.providerMessageId })
|
|
.from(chatPublications)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(
|
|
chatPublications.idempotencyKey,
|
|
`run:${interaction.sourceRunId}:${progressState}:${publication.endpointId}`,
|
|
),
|
|
eq(chatPublications.state, "published"),
|
|
isNotNull(chatPublications.providerMessageId),
|
|
),
|
|
)
|
|
.then((rows) => rows[0]?.providerMessageId ?? null);
|
|
if (providerMessageId) return providerMessageId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function progressSupersededByFastResponse(publication: typeof chatPublications.$inferSelect): Promise<boolean> {
|
|
if (!["queued", "working"].includes(publication.payload.progressState ?? "")) return false;
|
|
const runId = runIdFromMilestonePublication(publication);
|
|
const [run] = runId ? await db.select({ context: heartbeatRuns.contextSnapshot }).from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, publication.companyId), eq(heartbeatRuns.id, runId))) : [];
|
|
const sourceCommentId = typeof run?.context?.wakeCommentId === "string" ? run.context.wakeCommentId : typeof run?.context?.commentId === "string" ? run.context.commentId : publication.commentId;
|
|
if (!sourceCommentId) return false;
|
|
const [receipt] = await db.select({ id: fastResponseRequests.id }).from(fastResponseRequests).innerJoin(chatPublications,
|
|
and(eq(chatPublications.companyId, publication.companyId), eq(chatPublications.commentId, fastResponseRequests.commentId), eq(chatPublications.state, "published")))
|
|
.where(and(eq(fastResponseRequests.companyId, publication.companyId), eq(fastResponseRequests.sourceCommentId, sourceCommentId), eq(fastResponseRequests.endpointId, publication.endpointId), eq(fastResponseRequests.conversationId, publication.conversationId))).limit(1);
|
|
return Boolean(receipt);
|
|
}
|
|
|
|
async function runProgressSupersededByPublishedInteraction(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
): Promise<boolean> {
|
|
if (
|
|
!["queued", "working"].includes(publication.payload.progressState ?? "")
|
|
) {
|
|
return false;
|
|
}
|
|
const sourceRunId = runIdFromMilestonePublication(publication);
|
|
if (!sourceRunId) return false;
|
|
const interactionIds = await db
|
|
.select({ id: issueThreadInteractions.id })
|
|
.from(issueThreadInteractions)
|
|
.where(
|
|
and(
|
|
eq(issueThreadInteractions.companyId, publication.companyId),
|
|
eq(issueThreadInteractions.issueId, publication.issueId),
|
|
eq(issueThreadInteractions.sourceRunId, sourceRunId),
|
|
),
|
|
)
|
|
.then((rows) => rows.map((row) => row.id));
|
|
if (interactionIds.length === 0) return false;
|
|
return db
|
|
.select({ id: chatPublications.id })
|
|
.from(chatPublications)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(chatPublications.state, "published"),
|
|
inArray(
|
|
chatPublications.idempotencyKey,
|
|
interactionIds.map(
|
|
(interactionId) =>
|
|
`interaction:${interactionId}:${publication.endpointId}`,
|
|
),
|
|
),
|
|
),
|
|
)
|
|
.limit(1)
|
|
.then((rows) => rows.length > 0);
|
|
}
|
|
|
|
async function taskStatusPublicationToReplace(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
payload: SafeChatPublicationPayload,
|
|
): Promise<string | null> {
|
|
if (
|
|
!publication.idempotencyKey.startsWith("control:status:") ||
|
|
payload.attachmentIds?.length
|
|
)
|
|
return null;
|
|
const rows = await db
|
|
.select({
|
|
commentId: chatPublications.commentId,
|
|
commentRunId: issueComments.createdByRunId,
|
|
idempotencyKey: chatPublications.idempotencyKey,
|
|
payload: chatPublications.payload,
|
|
providerMessageId: chatPublications.providerMessageId,
|
|
})
|
|
.from(chatPublications)
|
|
.leftJoin(issueComments, eq(issueComments.id, chatPublications.commentId))
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.state, "published"),
|
|
isNotNull(chatPublications.providerMessageId),
|
|
),
|
|
)
|
|
.orderBy(desc(chatPublications.createdAt), desc(chatPublications.id));
|
|
const rowRunId = (row: (typeof rows)[number]) => {
|
|
const milestoneMatch =
|
|
/^run:([^:]+):(?:queued|working|waiting_for_input|completed|failed):/.exec(
|
|
row.idempotencyKey,
|
|
);
|
|
return milestoneMatch?.[1] ?? row.commentRunId ?? null;
|
|
};
|
|
for (const candidate of rows) {
|
|
if (
|
|
!candidate.providerMessageId ||
|
|
!["queued", "working"].includes(candidate.payload.progressState ?? "")
|
|
) {
|
|
continue;
|
|
}
|
|
const runId = rowRunId(candidate);
|
|
if (!runId) continue;
|
|
const laneClosed = rows.some((row) => {
|
|
if (rowRunId(row) !== runId) return false;
|
|
return (
|
|
["waiting_for_input", "completed", "failed"].includes(
|
|
row.payload.progressState ?? "",
|
|
) ||
|
|
(row.commentId !== null && row.payload.progressState === undefined)
|
|
);
|
|
});
|
|
if (!laneClosed) return candidate.providerMessageId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async function closeProgressToReplace(
|
|
publication: typeof chatPublications.$inferSelect,
|
|
endpoint: EndpointRow,
|
|
): Promise<{ providerMessageId: string; runId: string } | null> {
|
|
if (
|
|
!["telegram", "slack", "discord", "microsoft-teams"].includes(
|
|
endpoint.provider,
|
|
) ||
|
|
!publication.idempotencyKey.startsWith("control:close:")
|
|
)
|
|
return null;
|
|
const current = alias(chatPublications, "close_current_progress");
|
|
const plainText = (table: typeof chatPublications | typeof current) =>
|
|
and(
|
|
sql`jsonb_typeof(${table.payload}->'text') = 'string'`,
|
|
sql`${table.payload}->'card' is null`,
|
|
sql`${table.payload}->'interactionId' is null`,
|
|
sql`${table.payload}->'transportPart' is null`,
|
|
sql`(${table.payload}->'attachmentIds' is null or ${table.payload}->'attachmentIds' = '[]'::jsonb)`,
|
|
);
|
|
const ownedMilestone = (table: typeof chatPublications | typeof current) =>
|
|
or(
|
|
sql`${table.idempotencyKey} = 'run:' || ${heartbeatRuns.id}::text || ':queued:' || ${endpoint.id}`,
|
|
sql`${table.idempotencyKey} = 'run:' || ${heartbeatRuns.id}::text || ':working:' || ${endpoint.id}`,
|
|
sql`${table.idempotencyKey} like 'run:' || ${heartbeatRuns.id}::text || ':working:' || ${endpoint.id} || ':native:%'`,
|
|
);
|
|
// A historical progress row alone is not edit authority: its exact current
|
|
// outbound link must still belong to that run's progress (or an interleaved
|
|
// status control). Read under the same credential lease as final delivery.
|
|
const lanes = await db
|
|
.selectDistinct({
|
|
providerMessageId: chatMessageLinks.providerMessageId,
|
|
runId: heartbeatRuns.id,
|
|
})
|
|
.from(chatPublications)
|
|
.innerJoin(
|
|
heartbeatRuns,
|
|
and(
|
|
eq(heartbeatRuns.companyId, publication.companyId),
|
|
eq(heartbeatRuns.agentId, endpoint.assignedAgentId),
|
|
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${publication.issueId}`,
|
|
sql`split_part(${chatPublications.idempotencyKey}, ':', 2) = ${heartbeatRuns.id}::text`,
|
|
),
|
|
)
|
|
.innerJoin(
|
|
chatMessageLinks,
|
|
and(
|
|
eq(chatMessageLinks.companyId, publication.companyId),
|
|
eq(chatMessageLinks.endpointId, publication.endpointId),
|
|
eq(chatMessageLinks.conversationId, publication.conversationId),
|
|
eq(chatMessageLinks.direction, "outbound"),
|
|
eq(
|
|
chatMessageLinks.providerMessageId,
|
|
chatPublications.providerMessageId,
|
|
),
|
|
),
|
|
)
|
|
.innerJoin(
|
|
current,
|
|
and(
|
|
eq(current.id, chatMessageLinks.publicationId),
|
|
eq(current.companyId, publication.companyId),
|
|
eq(current.endpointId, publication.endpointId),
|
|
eq(current.conversationId, publication.conversationId),
|
|
eq(current.issueId, publication.issueId),
|
|
eq(current.state, "published"),
|
|
eq(current.providerMessageId, chatMessageLinks.providerMessageId),
|
|
isNull(current.commentId),
|
|
plainText(current),
|
|
or(
|
|
and(
|
|
sql`${current.payload}->>'progressState' in ('queued', 'working')`,
|
|
ownedMilestone(current),
|
|
),
|
|
like(current.idempotencyKey, "control:status:%"),
|
|
),
|
|
),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
eq(chatPublications.state, "published"),
|
|
isNull(chatPublications.commentId),
|
|
plainText(chatPublications),
|
|
sql`${chatPublications.payload}->>'progressState' in ('queued', 'working')`,
|
|
ownedMilestone(chatPublications),
|
|
),
|
|
)
|
|
.limit(2);
|
|
// One close receipt owns one edit. Multiple independent lanes require a
|
|
// separate durable multi-effect protocol, never an arbitrary latest guess.
|
|
if (lanes.length !== 1) return null;
|
|
const lane = lanes[0]!;
|
|
const [possiblyConsumed] = await db
|
|
.select({ id: chatPublications.id })
|
|
.from(chatPublications)
|
|
.leftJoin(
|
|
issueComments,
|
|
and(
|
|
eq(issueComments.id, chatPublications.commentId),
|
|
eq(issueComments.companyId, publication.companyId),
|
|
eq(issueComments.issueId, publication.issueId),
|
|
),
|
|
)
|
|
.where(
|
|
and(
|
|
eq(chatPublications.companyId, publication.companyId),
|
|
eq(chatPublications.endpointId, publication.endpointId),
|
|
eq(chatPublications.conversationId, publication.conversationId),
|
|
eq(chatPublications.issueId, publication.issueId),
|
|
or(
|
|
inArray(chatPublications.state, [
|
|
"pending",
|
|
"retry",
|
|
"streaming",
|
|
"delivery_unknown",
|
|
"published",
|
|
]),
|
|
// Stopping retries (including an operator's unknown-delivery
|
|
// cancellation) does not prove an attempted edit never arrived.
|
|
and(
|
|
inArray(chatPublications.state, ["cancelled", "failed"]),
|
|
gt(chatPublications.attempts, 0),
|
|
),
|
|
),
|
|
or(
|
|
eq(issueComments.createdByRunId, lane.runId),
|
|
// Question/confirmation prompts consume this same run's progress
|
|
// ID through interactionPromptPublicationToReplace, but have no
|
|
// commentId. A stale working link is not proof that their edit
|
|
// never reached the provider (including an unconfirmed receipt).
|
|
sql`exists (
|
|
select 1 from issue_thread_interactions consuming_interaction
|
|
where consuming_interaction.company_id = ${publication.companyId}::uuid
|
|
and consuming_interaction.issue_id = ${publication.issueId}::uuid
|
|
and consuming_interaction.source_run_id = ${lane.runId}::uuid
|
|
and ${chatPublications.payload}->>'interactionId' = consuming_interaction.id::text
|
|
and ${chatPublications.idempotencyKey} = 'interaction:' || consuming_interaction.id::text || ':' || ${publication.endpointId}
|
|
)`,
|
|
eq(
|
|
chatPublications.idempotencyKey,
|
|
`run:${lane.runId}:completed:${endpoint.id}`,
|
|
),
|
|
eq(
|
|
chatPublications.idempotencyKey,
|
|
`run:${lane.runId}:failed:${endpoint.id}`,
|
|
),
|
|
),
|
|
),
|
|
)
|
|
.limit(1);
|
|
// A final may have reached the provider before its database receipt. Even an
|
|
// unchanged working link cannot disprove that ambiguous provider effect.
|
|
return possiblyConsumed ? null : lane;
|
|
}
|
|
|
|
return { runIdFromMilestonePublication, runOwnershipMilestoneSupersessionReason, runPublicationToReplace, receiptReactionCompletionRunId, inboundWakePublicationToReplace, interactionResolutionPublicationToReplace, interactionPromptPublicationToReplace, progressSupersededByFastResponse, runProgressSupersededByPublishedInteraction, taskStatusPublicationToReplace, closeProgressToReplace };
|
|
}
|