Files
PaperClipAI/server/src/services/slack-conversation-lifecycle.ts
T
ba07f85cad perf: schedule agent timers from durable deadlines (#17876)
<!-- entire-trail-link-start -->
https://entire.com/gh/paperclipai/-/trails/58/changes/paperclip/58
<!-- entire-trail-link-end -->

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Agent timers must start due work and preserve queue admission rules.
> - The server woke their scan on every heartbeat interval, even with no
eligible timer.
> - Idle scans keep the database and instance active.
> - This pull request derives the next deadline from saved policy and
work records.
> - Supported producer transactions notify the shared coordinator when
eligibility changes.
> - An idle timer worker stops scanning and resumes when real work or a
deadline requires it.

## Linked Issues or Issue Description

Refs #17863. This is slice 63 of the recurring-work migration and
follows the merged issue-monitor deadline PR.

Related: #16632 changes timer issue scope; #16881 changes live-run
eligibility. This PR preserves the current admission policy. Those
policy changes are separate work. A future time-sensitive predicate must
expose its next transition deadline.

**What existing behavior does this improve?**

Agent timer heartbeat scheduling in the server.

**Current behavior**

A recurring heartbeat wakes the timer scan even when companies are
paused, timers are disabled, or no actionable task exists.

**Proposed behavior**

Register the earliest eligible interval or positive daily-cap reset
deadline. Re-read on supported producer commits and startup. Leave no
recurring database scan when no timer obligation remains.

## What Changed

- Keep policy and deadline calculation in the heartbeat queue.
Scheduling retains company, reporting-chain, worktree, and CAS gates.
- Use the existing deadline coordinator. Remove only the timer wake from
the server heartbeat loop.
- Notify before existing transaction writes that change eligibility or
deadlines. Cover configuration, lifecycle, company budgets, task
ownership, recovery, conversations, accounting corrections, and
completion.
- Use the lifecycle status-change hook for saved hire rejection. Cover
checkout's second adoption transaction after an initial failed CAS.
- Preserve pending work through idle and maintenance drains. Check
cancellation between due agents. Reconstruct on restart.
- Document the supported writer model and producer inventory. Add no
schema, table, or provider branch.

## Verification

- **1,710 passing cases across 49 complete files**, no skips, on
accepted source `32db86c1974dcbf794f01a8c5e42024e2ad071fc`. Coverage
includes real timer policy/CAS/producers, monitor and shared deadline
consumers, native reconstruction, Slack lifecycle,
skills/settings/configuration, queue isolation and agent lifecycle.
- Database build, production server typechecking/build, module
boundaries, target migration order and Runner profile identity pass.
Twenty-nine fresh PostgreSQL 17 schemas plus per-file fixtures applied
all 412 journal entries through index 413; all selected source hashes
and temporary fixture imports were restored exactly.
- Four new deadline composition cases verify future/overdue timing,
minimum scan spacing and disarming. A controlled mutation dropping only
the inherited continuation option fails; the composed source passes with
identical assertions and timers.
- Normal parent reconciliation preserves task-drain expiry and both
deadline contracts. The final monitor-parent publication changes only
its audit, preserving every qualified runtime and selected test source.
- Historical failures and review corrections remain in [the timer
audit](doc/idle-agent-timers.md). No full local repository-suite, live
provider, measured suspend/wake, external-alarm or fleet-savings claim.
Final-head CI/security, exact-head Greptile 5/5 and actionable Ponytail
findings remain separate merge gates.

## Risks

- Process-local notifications cover supported service/API writers in one
server process. Direct SQL and independent replicas need an explicit
ingress contract. Startup reconstruction is not live cross-process
notification.
- Notifications can batch for up to the configured heartbeat interval.
Known deadlines retain their scheduler path; failed reconciliation
retains bounded retry.
- Enabled timers without an actionable-work requirement remain real
recurring obligations. Zero caps require policy changes; positive
reached caps use UTC midnight.
- The timer fixture substitutes adapter enqueue. Existing admission
tests also pass, but this is not deployed provider or sleep
qualification.
- Cloud does not gain legacy agent timer alarms in this PR. Existing
sleep-safety blockers remain. No database/host suspension or fleet
savings claim is made.
- Parent-stack CI and review must pass before merge. This PR does not
authorize merging its dependencies.

## Model Used

OpenAI Codex (GPT-6-based coding agent), using repository inspection,
normal Git merges, composition proofs, a controlled red/green regression
and complete PostgreSQL integration suites. No subagents were used in
this reconciliation.

## 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
- [ ] All Paperclip CI gates are green
- [ ] 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>
Co-authored-by: Codex <noreply@openai.com>
2026-10-29 08:58:27 -05:00

152 lines
11 KiB
TypeScript

import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notifications.js";
import { notifyUnsafeWorkspaceExportRecovery } from "./unsafe-workspace-export-notifications.js";
import { and, asc, desc, eq, gt, inArray, isNull, sql } from "drizzle-orm";
import { chatConversations, chatEndpoints, heartbeatRuns, issueComments, issues, type Db } from "@paperclipai/db";
import { persistActivity, publishActivity, type ActivityPublication } from "./activity-log.js";
export type ExternalConversationSettlementScope = { companyId: string; issueId?: string };
async function readSettlement(db: Pick<Db, "select" | "execute">, companyId: string, issueId: string, lock = false) {
const bindingQuery = db.select({ conversation: { id: chatConversations.id, state: chatConversations.state },
endpoint: { provider: chatEndpoints.provider, assignedAgentId: chatEndpoints.assignedAgentId } })
.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);
const [binding] = await (lock ? bindingQuery.for("update", { of: chatEndpoints }) : bindingQuery);
if (!binding || binding.conversation.state !== "active") return null;
const issueQuery = db.select({ id: issues.id, title: issues.title, identifier: issues.identifier,
originKind: issues.originKind, conversationAgentId: issues.conversationAgentId, hiddenAt: issues.hiddenAt,
assigneeUserId: issues.assigneeUserId, assigneeAgentId: issues.assigneeAgentId, status: issues.status,
executionRunId: issues.executionRunId, monitorNextCheckAt: issues.monitorNextCheckAt,
executionState: issues.executionState }).from(issues)
.where(and(eq(issues.companyId, companyId), eq(issues.id, issueId)));
const [issue] = await (lock ? issueQuery.for("update") : issueQuery);
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 null;
const monitor = issue.executionState?.monitor;
if (monitor && typeof monitor === "object" && "status" in monitor &&
["scheduled", "triggered"].includes(String(monitor.status))) return null;
const [run] = await db.select({ id: heartbeatRuns.id, status: heartbeatRuns.status, agentId: heartbeatRuns.agentId,
finishedAt: heartbeatRuns.finishedAt, scheduledRetryAt: heartbeatRuns.scheduledRetryAt, livenessState: heartbeatRuns.livenessState,
runtimeMode: heartbeatRuns.runtimeMode, resultJson: heartbeatRuns.resultJson, contextSnapshot: heartbeatRuns.contextSnapshot }).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 null;
if (run.runtimeMode === "native" && run.resultJson?.finalizationReasonCode !== "external_chat_response_waiting") return null;
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 null;
const [wake] = await db.select({ id: issueComments.id }).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 null;
const [evidence] = await db.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 null;
return { binding, issue, run };
}
export async function pendingExternalConversationSettlement(db: Db, companyId: string, issueId: string) {
return Boolean(await readSettlement(db, companyId, issueId));
}
export function externalConversationSettlementCandidates(db: Db, scope?: ExternalConversationSettlementScope, after?: string) {
return db.select({ id: issues.id, companyId: issues.companyId }).from(issues).where(and(
eq(issues.originKind, "chat_channel"), eq(issues.status, "in_progress"),
isNull(issues.hiddenAt), isNull(issues.conversationAgentId), isNull(issues.assigneeUserId),
scope ? eq(issues.companyId, scope.companyId) : undefined,
scope?.issueId ? eq(issues.id, scope.issueId) : undefined,
after ? gt(issues.id, after) : undefined,
sql`exists (select 1 from chat_conversations c where c.company_id = ${issues.companyId}
and c.issue_id = ${issues.id} and c.state = 'active')`,
)).orderBy(asc(issues.id)).limit(100);
}
/** 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) => {
const ready = await readSettlement(tx, companyId, issueId, true);
if (!ready) return false;
const { binding, issue, run } = ready;
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 notifyUnsafeWorkspaceExportRecovery(tx, { companyId, issueId });
await notifyDeliveryWork(tx, DELIVERY_QUEUES.issueMonitors);
await notifyDeliveryWork(tx, DELIVERY_QUEUES.agentTimers);
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;