mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-30 05:16:50 +01:00
First PR: runtime. UI: #15728. Qualification: #15729.
## Thinking Path
> - Paperclip manages agent work through tasks and governed connections.
> - Chat connections already bind external conversations to those tasks.
> - Speko handles voice and telephony, while the selected Paperclip
agent does the work.
> - A call needs durable session authority, ordered follow-ups, and safe
result delivery.
> - This PR adds an explicit voice transport to the existing connection
runtime.
> - The UI and qualification suites follow in linked PRs. Speko stays
experimental.
## Linked Issues or Issue Description
**Problem or motivation**
Phone and browser voice conversations cannot use the chat connection
task lifecycle. A separate voice runtime is needed to preserve task
authority, retries, and reply history.
**Proposed solution**
Add company-scoped voice sessions, signed callbacks, durable replies,
inbound intake, and an agent-facing outbound-call tool. Reuse existing
tasks, queues, secrets, and publications.
**Roadmap alignment**
This extends the experimental chat connector entry points under Agent
Chat and Connected Apps. The project owner requested this feature.
Refs #11418 for the original provider catalog proposal. This
implementation uses the newer chat connection infrastructure rather than
an MCP catalog-only integration. Refs #15721 for remote OpenCode
startup.
## What Changed
- Add additive voice schema, shared contracts, session APIs, and
provider callbacks.
- Bind browser, inbound, and outbound sessions to an endpoint, task, and
selected agent.
- Add low-trust guest intake in an explicitly selected sandbox and keep
ordinary approval rules.
- Submit task work through session-scoped tools and push approved
replies through Speko POST /v1/calls/{id}/messages.
- Distinguish provider acceptance from confirmed spoken playback.
Reconcile reports without downloading recordings.
- Add protocol, migration, persistence, provider, and
delivery-diagnostic tests.
## Verification
- Master 3389bea708 is integrated. Database/shared builds, server
typecheck, migration upgrade/replay checks, and 46
communication-guidance/provider/protocol tests passed.
- The final persistence suite passed 85 real-database tests. Five
focused native continuation/authorization regressions passed. Five
callback ownership/rotation tests passed after the last runtime repair.
- Review repairs cover reconnect ownership, current caller authority,
duplicate delivery, multipart answers, task-page answers, guest question
handling, and saved sandbox recovery.
- A real OpenCode/OpenRouter DeepSeek task ran in Daytona and saved a
correct disk-space answer. This did not establish live-call playback.
- Full local typecheck/build passed before the final repairs; focused
typechecks passed after them. Latest-head CI runs the repository gates.
- The separate integrated browser qualification passed two journeys and
failed browser answer retrieval. The feature remains experimental. The
report in #15729 records this failure and outstanding real-provider
acceptance.
- No lockfiles are included. Speko migrations follow master as
0324–0330.
## Risks
- Voice sessions add migrations and provider-dependent lifecycle
behavior. Migrations preserve existing chat and AI transports.
- Callback origins must be reachable HTTPS. Provider keys and signing
secrets remain vaulted.
- Unverified callers receive task-scoped low-trust authority. Caller ID
does not grant private task access.
- Outbound telephony incurs provider charges. Provider acceptance is not
proof that the caller heard an answer.
- Keep the feature behind the existing experimental chat setting until
the real browser, inbound, outbound, and delayed-result acceptance
matrix passes.
## Model Used
OpenAI Codex, GPT-6, with tool use and code execution. The exact serving
model ID and context window 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 (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
- [ ] 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>
109 lines
8.6 KiB
TypeScript
109 lines
8.6 KiB
TypeScript
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
|
import { chatConversations, chatEndpoints, heartbeatRuns, issueComments, issues, type Db } from "@paperclipai/db";
|
|
import { persistActivity, publishActivity, type ActivityPublication } from "./activity-log.js";
|
|
|
|
/** Both run finalization and provider publication can arrive first. Re-read
|
|
* durable evidence under the task lock; never infer completion from prose. */
|
|
export async function settleExternalConversation(db: Db, companyId: string, issueId: string): Promise<boolean> {
|
|
let activity: ActivityPublication | null = null;
|
|
const settled = await db.transaction(async (tx) => {
|
|
// Match chat admission's endpoint -> task lock order. A closed/revoked
|
|
// connection must not be resurrected by a late finalizer.
|
|
const [binding] = await tx.select({ conversation: chatConversations, endpoint: chatEndpoints })
|
|
.from(chatConversations)
|
|
.innerJoin(chatEndpoints, and(eq(chatEndpoints.id, chatConversations.endpointId), eq(chatEndpoints.companyId, chatConversations.companyId)))
|
|
.where(and(eq(chatConversations.companyId, companyId), eq(chatConversations.issueId, issueId),
|
|
sql`(${chatEndpoints.provider} = 'slack' or (${chatEndpoints.provider} = 'speko' and exists
|
|
(select 1 from issues owned where owned.id = ${chatConversations.issueId} and owned.company_id = ${chatConversations.companyId}
|
|
and owned.origin_id = ${chatEndpoints.id}::text)))`, inArray(chatConversations.state, ["active", "waiting"]),
|
|
inArray(chatEndpoints.status, ["active", "verifying"])))
|
|
.orderBy(desc(chatConversations.createdAt)).limit(1)
|
|
.for("update", { of: chatEndpoints });
|
|
if (!binding) return false;
|
|
const [issue] = await tx.select().from(issues)
|
|
.where(and(eq(issues.companyId, companyId), eq(issues.id, issueId))).for("update");
|
|
if (!issue || issue.originKind !== "chat_channel" || issue.conversationAgentId || issue.hiddenAt ||
|
|
issue.assigneeUserId || issue.assigneeAgentId !== binding.endpoint.assignedAgentId ||
|
|
issue.status !== "in_progress" || issue.executionRunId || issue.monitorNextCheckAt ||
|
|
["pending", "changes_requested"].includes(String(issue.executionState?.status))) return false;
|
|
const monitor = issue.executionState?.monitor;
|
|
if (monitor && typeof monitor === "object" && "status" in monitor &&
|
|
["scheduled", "triggered"].includes(String(monitor.status))) return false;
|
|
const [run] = await tx.select().from(heartbeatRuns)
|
|
.where(and(eq(heartbeatRuns.companyId, companyId),
|
|
sql`coalesce(${heartbeatRuns.contextSnapshot}->>'issueId', ${heartbeatRuns.contextSnapshot}->>'taskId', ${heartbeatRuns.nativeIssueId}::text) = ${issueId}`))
|
|
.orderBy(desc(heartbeatRuns.createdAt), desc(heartbeatRuns.id)).limit(1);
|
|
if (!run || run.status !== "succeeded" || run.agentId !== issue.assigneeAgentId ||
|
|
!run.finishedAt ||
|
|
run.scheduledRetryAt || ["plan_only", "empty_response"].includes(run.livenessState ?? "")) return false;
|
|
if (run.runtimeMode === "native" && run.resultJson?.finalizationReasonCode !== "external_chat_response_waiting") return false;
|
|
// The published, server-selected response is the binding proof. Resolved
|
|
// questions can use an interaction continuation source and a board comment
|
|
// instead of an inbound Slack link; publication already enforced access.
|
|
const context = run.contextSnapshot ?? {};
|
|
const commentIds = [...new Set([
|
|
context.wakeCommentId, context.commentId,
|
|
...(Array.isArray(context.wakeCommentIds) ? context.wakeCommentIds : []),
|
|
].filter((id): id is string => typeof id === "string" && /^[a-f0-9-]{36}$/i.test(id)))];
|
|
if (!commentIds.length) return false;
|
|
const [wake] = await tx.select().from(issueComments)
|
|
.where(and(eq(issueComments.companyId, companyId), eq(issueComments.issueId, issueId), inArray(issueComments.id, commentIds)))
|
|
.orderBy(desc(issueComments.createdAt), desc(issueComments.id)).limit(1);
|
|
if (!wake) return false;
|
|
const [evidence] = await tx.execute<{ ready: boolean }>(sql`select
|
|
exists (select 1 from chat_publications p join issue_comments response
|
|
on response.id = p.comment_id and response.company_id = p.company_id and response.issue_id = p.issue_id
|
|
where p.company_id = ${companyId} and p.issue_id = ${issueId} and p.conversation_id = ${binding.conversation.id}
|
|
and p.state = 'published' and p.provider_message_id is not null
|
|
and response.created_by_run_id = ${run.id} and response.author_agent_id = ${run.agentId}
|
|
and response.deleted_at is null
|
|
and response.created_at >= (select original.created_at from issue_comments original
|
|
where original.company_id = ${companyId} and original.id = ${wake.id})
|
|
and response.metadata->>'authorizationReason' = 'allow_chat_run_presentation')
|
|
and (${run.runtimeMode} <> 'native' or exists (select 1 from native_run_finalizations f
|
|
where f.company_id = ${companyId} and f.issue_id = ${issueId} and f.run_id = ${run.id} and f.phase = 'committed'))
|
|
and not exists (select 1 from agent_task_sessions session where session.company_id = ${companyId}
|
|
and session.task_key = ${issueId} and session.goal_status is not null and session.goal_status <> 'complete')
|
|
and not exists (select 1 from issue_comments newer where newer.company_id = ${companyId} and newer.issue_id = ${issueId}
|
|
and newer.deleted_at is null and newer.author_agent_id is null
|
|
and (coalesce(newer.author_type, 'user') <> 'system' or exists (select 1 from chat_message_links inbound
|
|
where inbound.company_id = newer.company_id and inbound.comment_id = newer.id
|
|
and inbound.conversation_id = ${binding.conversation.id} and inbound.direction = 'inbound'))
|
|
and (newer.created_at, newer.id) > (select original.created_at, original.id from issue_comments original
|
|
where original.company_id = ${companyId} and original.id = ${wake.id}))
|
|
and not exists (select 1 from heartbeat_runs r where r.company_id = ${companyId}
|
|
and coalesce(r.context_snapshot->>'issueId', r.context_snapshot->>'taskId', r.native_issue_id::text) = ${issueId}
|
|
and r.status in ('queued', 'running', 'scheduled_retry'))
|
|
and not exists (select 1 from agent_wakeup_requests w where w.company_id = ${companyId}
|
|
and coalesce(w.payload->>'issueId', w.payload->>'taskId', w.payload->'_paperclipWakeContext'->>'issueId') = ${issueId}
|
|
and w.status in ('queued', 'claimed', 'deferred_issue_execution') and w.run_id is distinct from ${run.id}::uuid)
|
|
and not exists (select 1 from issue_thread_interactions i where i.company_id = ${companyId} and i.issue_id = ${issueId} and i.status = 'pending')
|
|
and not exists (select 1 from issue_approvals ia join approvals a on a.id = ia.approval_id and a.company_id = ia.company_id
|
|
where ia.company_id = ${companyId} and ia.issue_id = ${issueId} and a.status in ('pending', 'revision_requested'))
|
|
and not exists (select 1 from issue_relations edge join issues blocker on blocker.id = edge.issue_id and blocker.company_id = edge.company_id
|
|
where edge.company_id = ${companyId} and edge.related_issue_id = ${issueId} and edge.type = 'blocks' and blocker.status <> 'done')
|
|
and not exists (select 1 from chat_publications p where p.company_id = ${companyId} and p.conversation_id = ${binding.conversation.id}
|
|
and p.state in ('pending', 'streaming', 'retry', 'delivery_unknown', 'failed', 'awaiting_consent'))
|
|
as ready`);
|
|
if (!evidence?.ready) return false;
|
|
const changed = await tx.update(chatConversations).set({ state: "waiting", updatedAt: new Date() })
|
|
.where(and(eq(chatConversations.id, binding.conversation.id), eq(chatConversations.companyId, companyId), eq(chatConversations.state, "active")))
|
|
.returning({ id: chatConversations.id });
|
|
if (!changed.length) return false;
|
|
await tx.update(issues).set({ status: "in_review", updatedAt: new Date() })
|
|
.where(and(eq(issues.id, issueId), eq(issues.companyId, companyId)));
|
|
activity = (await persistActivity(tx as unknown as Db, {
|
|
companyId, actorType: "system", actorId: `${binding.endpoint.provider}-conversation`, action: "issue.updated",
|
|
entityType: "issue", entityId: issueId, runId: run.id,
|
|
details: { status: "in_review", externalConversationState: "waiting", conversationId: binding.conversation.id,
|
|
issueTitle: issue.title, issueIdentifier: issue.identifier },
|
|
})).publication;
|
|
return true;
|
|
});
|
|
if (activity) publishActivity(activity);
|
|
return settled;
|
|
}
|
|
|
|
/** Compatibility name for existing Slack callers. */
|
|
export const settleSlackConversation = settleExternalConversation;
|