Files
PaperClipAI/server/src/services/chat-interaction-publications.ts
T
DottaandPaperclip 4d317274ce feat(channels): add experimental iMessage Photon (#13299)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Channels connect external conversations to company tasks and agent
execution.
> - Slack, Discord, and AgentMail already provide durable delivery and
access controls.
> - People also need to reach an agent from Apple Messages and send
photos.
> - Photon provides shared Pro DMs, dedicated numbers, and authenticated
event recovery.
> - This pull request connects Photon to the existing channel services.
> - People can message an agent while Paperclip retains task ownership
and approval authority.

## Linked Issues or Issue Description

**Subsystem affected**

Cross-cutting: channel services, shared contracts, database constraints,
Apps, and agent Channels UI.

**Problem or motivation**

Paperclip has no iMessage channel. A person cannot use Apple Messages to
start a task, send a photo, or answer an agent's pending question.

**Proposed solution**

Add experimental **iMessage Photon** with Pro-compatible shared DMs or a
dedicated Photon Cloud number per agent channel. Reuse channel
admission, identity links, task generations, publication, and
interaction continuation. Keep groups disabled for shared allocation.
Dedicated lines support groups that an operator explicitly enables.
Require a fresh linked message and a published agent response before
setup completes.

**Alternatives considered**

Shared allocation has no owned phone number, so it reserves one project
and allows DMs only. Dedicated allocation reserves one stable number.
Local Mac access needs a separate deployment model. The upstream Photon
Chat SDK adapter does not persist the poll mappings and send receipts
required here. This change uses the lower-level SDK without adding
another agent runtime.

**Roadmap alignment**

This extends Connected Apps and agent communication through the existing
channel subsystem. It does not add a parallel tool connection or agent
loop. GitHub searches for Photon and iMessage found no matching provider
implementation.

**Additional context**

This ships behind the existing experimental channel gate. Dedicated-line
release qualification remains incomplete. Real Photon Pro DMs passed
task/reply, native poll, text answers, confirmation rejection, media,
restart, pause, reconnect, revocation, and removal tests. An
operator-supplied iPhone camera HEIC also passed the full round trip.
Dedicated groups remain unqualified. See [the verification
record](doc/connections/IMESSAGE-PHOTON-VERIFICATION.md) and [the
implementation plan](doc/plans/2026-09-11-imessage-photon.md).

## What Changed

- Add the provider catalog entry, shared setup contracts, and a forward
migration. A global partial index reserves the dedicated number or
shared project until its endpoint is archived.
- Add Cloud project inspection, vaulted project credentials,
selected-line token renewal, and a leased receiver. Persist checkpoint
updates under the receiver lease. Shared project replay accepts sparse
increasing sequences only after a complete recovery barrier.
- Connect DMs and enabled groups to existing task generations, sender
authorization, ordered delivery, and publication services. Keep each
iMessage conversation on its task after completion; only explicit `/new`
or `/close` releases the binding. Publish committed inbound comments
live and label their human bubbles “Sent from iMessage” in both
task-chat renderers.
- Persist immutable text/file send identities, upload receipts, poll
IDs, option IDs, per-person drafts, and canonical interaction
continuation proofs.
- Add source-bound file recovery, bounded HEIC/HEIF conversion, JPEG
previews, and related Live Photo companion video retention.
- Add the three-step setup flow and channel management surfaces with
official branding. Preserve the experimental gate and existing
pause/disconnect behavior.
- Add interactive production-component Storybooks for setup, access,
recovery, and ongoing conversations. Add provider, integration, catalog,
and browser regression coverage. Document setup, recovery, supported
boundaries, and qualification gaps.

## Verification

- Live Photon Pro, SDK 2.1.0: linked iPhone messages create a task and
receive native Codex replies in Apple Messages. Unlinked senders cannot
start work.
- Three real follow-ups each reopened the same completed task. Incoming
bubbles appeared on its open page without reload and showed “Sent from
iMessage.” The third follow-up ran after restarting the server on
`4d7222110`; the agent correctly repeated its previous reply from before
the restart.
- Native polls after restart, sequential text drafts, required-field
correction, explicit submission, approval rejection with a required
reason, and native continuation passed against Photon.
- PNG, text documents, synthetic HEIC, and a real iPhone camera HEIC
passed in both directions. The camera photo produced a 3024×4032 JPEG
preview. The native agent described it and returned the received HEIC
byte-for-byte.
- Pause/resume, reconnect, identity revocation, removal, `/status`,
`/new`, `/close`, and stale answers after close passed live. Messages
suppressed by pause did not become work on resume. Removal stopped
intake and removed credential bindings.
- All 304 focused tests passed on `4d7222110`. These cover Photon
unit/integration behavior, both task-chat renderers, live comment
hydration, completed-task continuity after restart, enabled groups,
duplicate delivery, and explicit reset/close. The selected Teams
completion-boundary regression also passed. Full workspace
typecheck/build and token gates passed for the conversation fix; the
final UI changes passed their affected typecheck/build and tests.
- All 26 new Photon Storybook Playwright cases passed in light and dark
themes, including the complete shared-DM setup journey and 390px mobile
follow-ups. UI typecheck and the Storybook build passed. These stories
use simulated Photon responses and do not replace the live evidence
above.
- The full chat-adapters browser suite previously passed all 39 cases.
Migration checks passed, and migration 0275 applied to the isolated live
instance with the earlier Photon migration already applied.
- The local full Vitest run was previously interrupted by the host's
embedded-Postgres shared-memory limit; it is not a full-suite pass. All
30 applicable CI checks passed on preceding head `7a5419cac`, with two
skipped checks and Greptile 5/5. Head `24f8e1aae` adds an explicit
required-story discovery guard to the 26 passing Storybook cases.
Greptile rates this final head 5/5 with no unresolved review threads.
All 30 applicable CI checks passed, with two optional checks skipped.
- A repeated live send key suppressed the duplicate but returned gRPC 6
/ SDK `internalError` without an original receipt. Paperclip keeps
unknown delivery unresolved. This provider behavior is covered by a
regression test.
- See [the verification
record](doc/connections/IMESSAGE-PHOTON-VERIFICATION.md) for package
versions, redacted live evidence, deterministic coverage, and remaining
qualification gaps.

## Risks

- Dedicated group qualification remains unrun; groups are disabled for
the approved Pro scope. Real iPhone camera HEIC passed transport,
preview generation, agent inspection, and return. Keep the channel
experimental; the dedicated-line release matrix remains incomplete.
- Shared recovery and attachment aliases were verified against the live
gateway. Duplicate writes currently return an error without the original
receipt; unresolved sends require operator resolution. The
implementation fails visibly on invalid replay ordering, a reset cursor,
or changed identity.
- The HEIF converter passed on macOS arm64 and in Linux CI. Windows HEIF
binaries have not been executed in this work. Linux musl has no packaged
converter. Unsupported conversion retains the original and reports the
missing preview.
- The migration adds a global reservation across companies for Photon
numbers and shared projects. Paused and revoked endpoints keep that
reservation until removal.
- Integration touches shared channel services. Existing provider browser
coverage passes; broad repository verification is recorded above.
- `pnpm-lock.yaml` is intentionally excluded under repository policy.
The repository bot owns lockfile updates. The additional Superagent
supply-chain scan is neutral/inconclusive because these new dependencies
are not yet in the committed lockfile. Its security scan passed; all
required CI checks pass.

## Model Used

OpenAI Codex, GPT-6 family, with reasoning, repository inspection, code
execution, browser testing, and tool use. The exact served model
identifier and context-window size are not exposed in this session. No
sub-agents were used.

## 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 (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run 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
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-12 15:23:50 -05:00

804 lines
27 KiB
TypeScript

import { nativePhotonInteraction } from "./photon/interactions.js";
import { randomBytes } from "node:crypto";
import { and, eq, inArray, sql } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import {
chatActions,
chatConversations,
chatEndpoints,
chatPublications,
heartbeatRuns,
issues,
} from "@paperclipai/db";
import type {
AskUserQuestionsInteraction,
AskUserQuestionsQuestion,
IssueThreadInteraction,
RequestConfirmationInteraction,
SafeExternalChatCardAction,
} from "@paperclipai/shared";
import { readConfigFile } from "../config-file.js";
import { projectSafeChatPublication } from "./chat-publication-projection.js";
import { safeChatTaskUrl } from "./chat-task-url.js";
import {
chatQuestionFormActionRecords,
createChatQuestionFormDraft,
} from "./chat-question-forms.js";
const MAX_NATIVE_QUESTION_OPTIONS = 12;
const QUESTION_ACTION_PREFIX = "pcq:";
const QUESTION_ACTION_TOKEN_BYTES = 16;
export const CHAT_QUESTION_ACTION_TOKEN_TTL_MS = 7 * 24 * 60 * 60 * 1_000;
export const TELEGRAM_CALLBACK_DATA_LIMIT_BYTES = 64;
type ChatPublicationDb = Pick<Db, "select" | "insert" | "update">;
function terminalNativeInteractionCopy(
interaction: IssueThreadInteraction,
): { body: string; text: string } | null {
if (interaction.kind === "request_confirmation") {
if (interaction.status === "accepted")
return { body: "Accepted", text: "Accepted." };
if (interaction.status === "rejected")
return { body: "Rejected", text: "Rejected." };
if (interaction.status === "cancelled") {
const outcome = interaction.result?.outcome;
const body =
outcome === "skipped"
? "Skipped in Paperclip"
: outcome === "withdrawn"
? "Withdrawn in Paperclip"
: outcome === "addressee_deleted"
? "Cancelled: addressed agent was removed"
: "Cancelled in Paperclip";
return { body, text: `${body}.` };
}
if (interaction.status === "expired") {
const body =
interaction.result?.outcome === "superseded_by_comment"
? "Expired: superseded by a newer reply"
: interaction.result?.outcome === "superseded_by_newer_request"
? "Expired: replaced by a newer request"
: interaction.result?.outcome === "stale_target"
? "Expired: target is no longer current"
: interaction.result?.outcome === "issue_closed"
? "Expired: task is closed"
: "Expired in Paperclip";
return { body, text: `${body}.` };
}
return null;
}
if (interaction.kind !== "ask_user_questions") return null;
if (interaction.status === "answered") {
const question = nativeChatQuestion(interaction);
const answer = question
? interaction.result?.answers.find(
(candidate) => candidate.questionId === question.id,
)
: null;
const option =
answer?.optionIds.length === 1 && !answer.otherText
? question?.options.find(
(candidate) => candidate.id === answer.optionIds[0],
)
: null;
const body = option ? `Answered: ${option.label}.` : "Answered.";
return { body, text: body };
}
if (interaction.status === "cancelled") {
const outcome = interaction.result?.outcome;
const body =
outcome === "skipped"
? "Skipped in Paperclip."
: outcome === "withdrawn"
? "Withdrawn in Paperclip."
: "Cancelled in Paperclip.";
return { body, text: body };
}
if (interaction.status === "expired") {
const body =
interaction.result?.expirationReason === "superseded_by_comment"
? "Expired: superseded by a newer reply"
: interaction.result?.expirationReason ===
"superseded_by_newer_interaction"
? "Expired: replaced by a newer request"
: interaction.result?.outcome === "issue_closed"
? "Expired: task is closed"
: "Expired in Paperclip";
return { body, text: `${body}.` };
}
return null;
}
export function publicChatInteractionTaskUrl(issueId: string): string | null {
const configured =
process.env.PAPERCLIP_AUTH_PUBLIC_BASE_URL?.trim() ||
process.env.BETTER_AUTH_URL?.trim() ||
process.env.BETTER_AUTH_BASE_URL?.trim() ||
process.env.PAPERCLIP_PUBLIC_URL?.trim() ||
readConfigFile()?.auth?.publicBaseUrl?.trim() ||
process.env.PAPERCLIP_MANAGED_RUNTIME_PUBLIC_URL?.trim();
return safeChatTaskUrl(configured, issueId);
}
/**
* Provider callbacks carry this compact, cryptographically random action id.
* Its durable chat_actions row is the only mapping back to an interaction,
* question, and option. Keeping the token opaque avoids exposing canonical ids
* and fits Telegram's strict 64-byte callback_data envelope.
*/
export function createChatQuestionOptionActionToken(): string {
return `${QUESTION_ACTION_PREFIX}${randomBytes(QUESTION_ACTION_TOKEN_BYTES).toString("base64url")}`;
}
export function createChatConfirmationActionToken(): string {
return createChatQuestionOptionActionToken();
}
/** Mirrors the pinned Chat SDK Telegram adapter's exact wire envelope. */
export function telegramChatSdkCallbackData(
actionId: string,
value?: string,
): string {
return `chat:${JSON.stringify({
a: actionId,
...(typeof value === "string" ? { v: value } : {}),
})}`;
}
export function telegramCallbackDataByteLength(
actionId: string,
value?: string,
): number {
return Buffer.byteLength(
telegramChatSdkCallbackData(actionId, value),
"utf8",
);
}
export function nativeChatQuestion(
interaction: AskUserQuestionsInteraction,
): AskUserQuestionsQuestion | null {
if (interaction.payload.questions.length !== 1) return null;
const question = interaction.payload.questions[0];
if (
question.selectionMode !== "single" ||
question.allowOther === true ||
question.options.length > MAX_NATIVE_QUESTION_OPTIONS ||
question.options.some((option) => option.freeText === true)
) {
return null;
}
return question;
}
/**
* Telegram can safely render ordinary binary confirmations as inline buttons.
* Confirmations that collect a rejection reason or authorize a credential,
* connection, or tool side effect stay in Paperclip, where the complete
* governed review UI and permission checks are available.
*/
export function nativeTelegramConfirmation(
interaction: IssueThreadInteraction,
): RequestConfirmationInteraction | null {
if (interaction.kind !== "request_confirmation") return null;
if (
interaction.payload.rejectRequiresReason === true ||
interaction.payload.toolAction !== undefined ||
interaction.payload.secretProposal !== undefined ||
interaction.payload.connectionAuthorization !== undefined ||
interaction.payload.target?.type === "issue_document"
) {
return null;
}
return interaction;
}
function textForQuestionInteraction(
interaction: AskUserQuestionsInteraction,
taskUrl: string | null,
): string {
const lines = [
interaction.payload.title ?? interaction.title ?? "Input needed",
"",
];
for (const question of interaction.payload.questions) {
lines.push(question.prompt);
for (const option of question.options) lines.push(`- ${option.label}`);
lines.push("");
}
lines.push(
taskUrl
? `Open the task in Paperclip to respond: ${taskUrl}`
: "Open the task in Paperclip to respond.",
);
return lines.join("\n");
}
function genericInteractionText(taskUrl: string | null): string {
return [
"This task needs an authorized response in Paperclip.",
taskUrl
? `Open the task in Paperclip to respond: ${taskUrl}`
: "Open the task in Paperclip to respond.",
]
.filter((value): value is string => Boolean(value))
.join("\n\n");
}
/**
* Enqueues one immutable outbox item per live external task binding. A lone
* closed single-select question receives compact executable buttons. Slack and
* Teams can also receive an opaque modal opener for safe text and closed
* single-select forms. Unsupported question shapes stay link-only.
*/
export async function enqueueIssueInteractionChatPublications(
db: ChatPublicationDb,
interaction: IssueThreadInteraction,
) {
if (interaction.status !== "pending") return [];
// The first native-chat wave intentionally externalizes only questions and
// confirmations. Other governance interactions have richer partial and
// terminal semantics that are authoritative in Paperclip; projecting a
// generic link card without complete settlement/recovery would leave stale
// provider prompts after a board decision.
if (
interaction.kind !== "ask_user_questions" &&
interaction.kind !== "request_confirmation"
) {
return [];
}
// An endpoint is one immutable provider bot identity. Never externalize a
// user/system-authored interaction, or let one agent speak through another
// agent's endpoint. If the interaction names a source run, verify that run's
// company and agent instead of trusting the denormalized creator alone.
if (!interaction.createdByAgentId) return [];
if (interaction.sourceRunId) {
const sourceRun = await db
.select({ agentId: heartbeatRuns.agentId })
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.id, interaction.sourceRunId),
eq(heartbeatRuns.companyId, interaction.companyId),
eq(heartbeatRuns.agentId, interaction.createdByAgentId),
),
)
.then((rows) => rows[0] ?? null);
if (!sourceRun) return [];
}
const bindings = await db
.select({
conversation: chatConversations,
endpoint: chatEndpoints,
})
.from(chatConversations)
.innerJoin(
chatEndpoints,
and(
eq(chatEndpoints.companyId, chatConversations.companyId),
eq(chatEndpoints.id, chatConversations.endpointId),
eq(chatEndpoints.publicationMode, "automatic"),
),
)
.where(
and(
eq(chatConversations.companyId, interaction.companyId),
eq(chatConversations.issueId, interaction.issueId),
inArray(chatConversations.state, ["active", "waiting"]),
inArray(chatEndpoints.status, ["active", "verifying"]),
),
);
if (bindings.length === 0) return [];
const taskUrl = publicChatInteractionTaskUrl(interaction.issueId);
const question =
interaction.kind === "ask_user_questions"
? nativeChatQuestion(interaction)
: null;
const inserted: Array<typeof chatPublications.$inferSelect> = [];
for (const { conversation, endpoint } of bindings) {
if (endpoint.assignedAgentId !== interaction.createdByAgentId) continue;
const formDraft =
interaction.kind === "ask_user_questions" &&
(endpoint.provider === "slack" ||
endpoint.provider === "discord" ||
endpoint.provider === "microsoft-teams") &&
endpoint.capabilities.actions === true &&
endpoint.capabilities.modals === true
? createChatQuestionFormDraft(
interaction,
endpoint.provider === "discord"
? { nativeProvider: "discord" }
: {},
)
: null;
const supportsCallbacks =
endpoint.provider !== "imessage-photon" &&
formDraft === null &&
question !== null &&
endpoint.capabilities.actions === true;
const questionActionTokens = supportsCallbacks
? question.options.map((option) => ({
actionId: createChatQuestionOptionActionToken(),
option,
}))
: [];
const confirmation =
endpoint.provider === "telegram" && endpoint.capabilities.actions === true
? nativeTelegramConfirmation(interaction)
: null;
const confirmationActionTokens = confirmation
? (["accept", "reject"] as const).map((decision) => ({
actionId: createChatConfirmationActionToken(),
decision,
}))
: [];
if (
endpoint.provider === "telegram" &&
[...questionActionTokens, ...confirmationActionTokens].some(
({ actionId }) =>
telegramCallbackDataByteLength(actionId) >
TELEGRAM_CALLBACK_DATA_LIMIT_BYTES,
)
) {
throw new Error("Generated Telegram question action exceeds 64 bytes");
}
const actions: SafeExternalChatCardAction[] = formDraft
? [
{
type: "callback" as const,
actionId: formDraft.openActionId,
label: "Respond",
style: "primary" as const,
},
]
: supportsCallbacks
? questionActionTokens.map(({ actionId, option }) => ({
type: "callback" as const,
actionId,
label: option.label,
}))
: confirmation
? confirmationActionTokens.map(({ actionId, decision }) => ({
type: "callback" as const,
actionId,
label:
decision === "accept"
? (confirmation.payload.acceptLabel ?? "Accept")
: (confirmation.payload.rejectLabel ?? "Reject"),
style:
decision === "accept"
? ("primary" as const)
: ("danger" as const),
}))
: taskUrl
? [
{
type: "link" as const,
label: "Open in Paperclip",
url: taskUrl,
},
]
: [];
const text =
interaction.kind === "ask_user_questions"
? textForQuestionInteraction(interaction, taskUrl)
: genericInteractionText(taskUrl);
const payload = projectSafeChatPublication({
classification: "external",
source: "issue_interaction",
text,
progressState: "waiting_for_input",
interaction: {
id: interaction.id,
card: {
kind:
interaction.kind === "ask_user_questions"
? "question"
: interaction.kind === "request_confirmation"
? "confirmation"
: "status",
title:
interaction.kind === "ask_user_questions"
? (question?.prompt ??
interaction.payload.title ??
interaction.title ??
"Input needed")
: interaction.kind === "request_confirmation"
? interaction.payload.prompt
: "Response needed in Paperclip",
body:
interaction.kind === "ask_user_questions"
? (question?.helpText ?? undefined)
: interaction.kind === "request_confirmation"
? (interaction.payload.detailsMarkdown ?? undefined)
: "Open the task in Paperclip to review and respond.",
actions,
},
},
});
const rows = await db
.insert(chatPublications)
.values({
companyId: interaction.companyId,
endpointId: endpoint.id,
conversationId: conversation.id,
issueId: interaction.issueId,
idempotencyKey: `interaction:${interaction.id}:${endpoint.id}`,
payload,
state: "pending",
})
.onConflictDoNothing()
.returning();
const publication = rows[0];
if (publication && endpoint.provider === "imessage-photon" && nativePhotonInteraction(interaction)) {
const reference = randomBytes(9).toString("base64url");
await db.insert(chatActions).values({ companyId: interaction.companyId, endpointId: endpoint.id, conversationId: conversation.id,
kind: "photon_interaction", providerActionId: `photon:${reference}`,
payload: { version: 1, reference, interactionId: interaction.id, publicationId: publication.id, sessionGeneration: conversation.sessionGeneration,
expiresAt: new Date(publication.createdAt.getTime() + CHAT_QUESTION_ACTION_TOKEN_TTL_MS).toISOString() }, status: "issued" });
} else if (publication && formDraft) {
await db.insert(chatActions).values(
chatQuestionFormActionRecords(formDraft, {
companyId: interaction.companyId,
endpointId: endpoint.id,
conversationId: conversation.id,
publicationId: publication.id,
}),
);
} else if (publication && question && questionActionTokens.length > 0) {
const expiresAt = new Date(
publication.createdAt.getTime() + CHAT_QUESTION_ACTION_TOKEN_TTL_MS,
).toISOString();
await db.insert(chatActions).values(
questionActionTokens.map(({ actionId, option }) => ({
companyId: interaction.companyId,
endpointId: endpoint.id,
conversationId: conversation.id,
kind: "question_answer",
providerActionId: actionId,
payload: {
version: 1,
publicationId: publication.id,
interactionId: interaction.id,
questionId: question.id,
optionId: option.id,
expiresAt,
},
status: "issued",
})),
);
} else if (
publication &&
confirmation &&
confirmationActionTokens.length > 0
) {
const expiresAt = new Date(
publication.createdAt.getTime() + CHAT_QUESTION_ACTION_TOKEN_TTL_MS,
).toISOString();
await db.insert(chatActions).values(
confirmationActionTokens.map(({ actionId, decision }) => ({
companyId: interaction.companyId,
endpointId: endpoint.id,
conversationId: conversation.id,
kind: "confirmation_response",
providerActionId: actionId,
payload: {
version: 1,
publicationId: publication.id,
interactionId: interaction.id,
decision,
expiresAt,
},
status: "issued",
})),
);
}
inserted.push(...rows);
}
return inserted;
}
/**
* Settles every delivered question or confirmation card. Providers without a
* native callback receive the same actionless terminal edit/follow-up as
* providers with buttons, so an "Open in Paperclip" prompt never remains
* visibly pending after the authoritative board decision.
*
* The terminal publication shares the provider callback idempotency key. If a
* provider click wins the race, its handler converges on the same row; if the
* Paperclip UI wins, all still-issued callback tokens expire in this same
* authoritative resolution transaction.
*/
export async function enqueueTerminalIssueInteractionChatPublications(
db: ChatPublicationDb,
interaction: IssueThreadInteraction,
) {
if (interaction.status === "pending") return [];
const originals = (
await db
.select()
.from(chatPublications)
.where(
and(
eq(chatPublications.companyId, interaction.companyId),
eq(chatPublications.issueId, interaction.issueId),
eq(
sql<string>`${chatPublications.payload}->>'interactionId'`,
interaction.id,
),
),
)
).filter(
(publication) =>
publication.idempotencyKey ===
`interaction:${interaction.id}:${publication.endpointId}`,
);
if (originals.length === 0) return [];
await db
.update(chatActions)
.set({
status: "expired",
result: { code: "interaction_resolved_elsewhere" },
updatedAt: new Date(),
})
.where(
and(
eq(chatActions.companyId, interaction.companyId),
inArray(chatActions.kind, [
"photon_interaction",
"question_answer",
"question_form_open",
"question_form_submit",
"confirmation_response",
"photon_interaction",
]),
eq(chatActions.status, "issued"),
eq(
sql<string>`${chatActions.payload}->>'interactionId'`,
interaction.id,
),
),
);
const unsentIds = originals
.filter(
(publication) =>
publication.state === "pending" || publication.state === "retry",
)
.map((publication) => publication.id);
if (unsentIds.length > 0) {
await db
.update(chatPublications)
.set({
state: "cancelled",
nextAttemptAt: null,
redactedError: "Interaction was resolved before provider publication",
updatedAt: new Date(),
})
.where(
and(
inArray(chatPublications.id, unsentIds),
inArray(chatPublications.state, ["pending", "retry"]),
),
);
}
const originalIds = originals.map((publication) => publication.id);
// The dispatcher may claim pending -> streaming after the first read but
// before the cancellation CAS. Re-read every executable original after that
// CAS so a loser is treated as potentially provider-visible and receives a
// terminal replacement instead of being decided from the stale snapshot.
// A published row without a provider id is also potentially visible: that
// is the durable result of an operator choosing `mark_delivered` after an
// ambiguous send, so resolution must post a terminal follow-up rather than
// leaving the external card looking actionable forever.
const currentOriginals =
originalIds.length > 0
? await db
.select()
.from(chatPublications)
.where(inArray(chatPublications.id, originalIds))
: [];
const providerVisibleOriginals = currentOriginals.filter(
(original) =>
original.state === "streaming" ||
original.state === "delivery_unknown" ||
original.state === "published",
);
const planTarget =
interaction.kind === "request_confirmation" &&
interaction.payload.target?.type === "issue_document" &&
interaction.payload.target.issueId === interaction.issueId &&
interaction.payload.target.key === "plan"
? interaction.payload.target
: null;
const rejectedPlanNeedsRevision =
interaction.status === "rejected" && planTarget !== null;
const resolutionOutcome = (interaction.result as { outcome?: unknown } | null)
?.outcome;
const continuationWakeRequired =
interaction.status !== "expired" &&
resolutionOutcome !== "skipped" &&
!(
interaction.kind === "ask_user_questions" &&
interaction.status === "answered"
) &&
(interaction.continuationPolicy === "wake_assignee" ||
(interaction.continuationPolicy === "wake_assignee_on_accept" &&
(interaction.status === "accepted" ||
interaction.status === "answered")) ||
rejectedPlanNeedsRevision);
if (continuationWakeRequired && currentOriginals.length > 0) {
const issue = await db
.select({
assigneeAgentId: issues.assigneeAgentId,
status: issues.status,
})
.from(issues)
.where(
and(
eq(issues.companyId, interaction.companyId),
eq(issues.id, interaction.issueId),
),
)
.then((rows) => rows[0] ?? null);
const requestedByActorType = interaction.resolvedByUserId
? "user"
: interaction.resolvedByAgentId
? "agent"
: "system";
const requestedByActorId =
interaction.resolvedByUserId ??
interaction.resolvedByAgentId ??
"system:interaction-resolution";
const wakeBinding = currentOriginals[0]!;
if (
issue?.assigneeAgentId &&
issue.status !== "done" &&
issue.status !== "cancelled" &&
!(
resolutionOutcome === "withdrawn" &&
interaction.resolvedByAgentId === issue.assigneeAgentId
)
) {
await db
.insert(chatActions)
.values({
companyId: interaction.companyId,
endpointId: wakeBinding.endpointId,
conversationId: wakeBinding.conversationId,
kind: "interaction_wakeup",
providerActionId: `interaction_wakeup:${interaction.id}`,
payload: {
version: 1,
interactionId: interaction.id,
interactionKind: interaction.kind,
interactionStatus: interaction.status,
issueId: interaction.issueId,
agentId: issue.assigneeAgentId,
sourceCommentId: interaction.sourceCommentId ?? null,
sourceRunId: interaction.sourceRunId ?? null,
requestedByActorType,
requestedByActorId,
...(planTarget
? {
planReviewInteraction: {
id: interaction.id,
kind: interaction.kind,
status: interaction.status,
target: planTarget,
acceptedTargetRevision:
interaction.status === "accepted" ? planTarget : null,
result: interaction.result,
},
}
: {}),
...(interaction.status === "accepted" && planTarget
? {
forceFreshSession: true,
workspaceRefreshReason: "accepted_plan_confirmation",
}
: {}),
...(interaction.resolvedByUserId
? { requestedByUserId: interaction.resolvedByUserId }
: {}),
},
status: "issued",
})
.onConflictDoNothing();
}
}
const copy = terminalNativeInteractionCopy(interaction);
if (!copy) return [];
const inserted: Array<typeof chatPublications.$inferSelect> = [];
for (const original of providerVisibleOriginals) {
if (!original.payload.card) continue;
const rows = await db
.insert(chatPublications)
.values({
companyId: interaction.companyId,
endpointId: original.endpointId,
conversationId: original.conversationId,
issueId: interaction.issueId,
idempotencyKey: `interaction-resolution:${interaction.id}:${original.endpointId}`,
payload: projectSafeChatPublication({
classification: "external",
source: "issue_interaction",
text: copy.text,
interaction: {
id: interaction.id,
card: {
kind: original.payload.card.kind,
title: original.payload.card.title,
body: copy.body,
actions: [],
},
},
}),
state: "pending",
})
.onConflictDoNothing()
.returning();
inserted.push(...rows);
}
return inserted;
}
/** Prevents an unstarted stale question card from being sent after replacement. */
export async function cancelPendingIssueInteractionChatPublications(
db: ChatPublicationDb,
input: {
companyId: string;
issueId: string;
interactionIds: readonly string[];
},
) {
if (input.interactionIds.length === 0) return [];
await db
.update(chatActions)
.set({
status: "expired",
result: { code: "interaction_superseded_before_publication" },
updatedAt: new Date(),
})
.where(
and(
eq(chatActions.companyId, input.companyId),
inArray(chatActions.kind, [
"question_answer",
"question_form_open",
"question_form_submit",
"confirmation_response",
"photon_interaction",
]),
eq(chatActions.status, "issued"),
inArray(sql<string>`${chatActions.payload}->>'interactionId'`, [
...input.interactionIds,
]),
),
);
return db
.update(chatPublications)
.set({
state: "cancelled",
nextAttemptAt: null,
redactedError: "Interaction was superseded before publication",
updatedAt: new Date(),
})
.where(
and(
eq(chatPublications.companyId, input.companyId),
eq(chatPublications.issueId, input.issueId),
inArray(chatPublications.state, ["pending", "retry"]),
inArray(sql<string>`${chatPublications.payload}->>'interactionId'`, [
...input.interactionIds,
]),
),
)
.returning();
}