Files
PaperClipAI/server/src/services/fast-responses.ts
T
DottaandPaperclip a7dd5284b5 feat: add company fast-response acknowledgements (#15748)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Human messages start agent work through task, chat, and
external-channel intake.
> - Agent startup can delay the first visible response.
> - A small direct model call can acknowledge the request while normal
work starts.
> - This response must remain separate from evidence that the agent
answered or completed work.
> - This pull request adds optional company-wide fast responses with
durable receipts, access checks, and cost accounting.
> - Users receive prompt acknowledgement without changing the agent
execution path.

## Linked Issues or Issue Description

**Subsystem affected**

Cross-cutting: database, shared contracts, server orchestration,
connections, and UI.

**Problem or motivation**

Users can wait for an agent to start before they receive a contextual
response. Generic progress messages do not confirm what the user asked.
An acknowledgement must not falsely complete work or satisfy an
unanswered turn.

**Proposed solution**

Add a disabled-by-default experimental fast-response model using an
existing shared API connection. Generate one short acknowledgement for
each accepted human turn. Use the existing agent identity and external
publication outboxes. Retain platform provenance and account for the
inference independently.

**Alternatives considered**

Running the acknowledgement through the agent runtime adds startup
latency. Restricting implementation to OpenRouter leaves other existing
API connections unsupported. This change supports all existing API
connections and uses OpenRouter for live verification.

**Roadmap alignment**

This extends existing agent communication and connection settings.
ROADMAP.md and open pull requests were checked. No duplicate
fast-response implementation was found. The maintainer supplied the
feature plan directly.

## What Changed

- Add Experimental fast responses to instance settings. Keep the feature
off by default. Enforce the flag at intake, dispatch, and publication.
- Move Decision model and Fast response setup to company Settings →
Connections when the experiment is enabled. Place Connections below
Environments. Separate the model sections with clear headings and a
divider. Keep Decision model in General when the experiment is off.
Route Costs setup links to the available settings page.
- Make the acknowledgement prompt a multiline template string. Ask for a
brief, positive response in the user’s language.

- Add settings, connection/model selection, sponsorship, fixed-sample
testing, and Costs history.
- Add a Vercel AI SDK text service for existing native, routed, Bedrock,
and local API connections.
- Add durable per-turn requests, a bounded worker, a three-second
admission deadline, and one inference attempt.
- Integrate task creation, task and agent chat, steering, queued
follow-ups, experimental board chat, shared chat intake, and email.
- Preserve receipt provenance and exclude receipts from completion,
recovery, interaction resolution, and agent wakeups.
- Recheck authorization, source validity, real replies, budgets, and
destination binding before publication. Preserve accounting for
suppressed or uncertain outcomes.
- Add provider contracts, database race tests, channel outbox tests, UI
tests, Stories, and documentation. Keep decision-model execution
unchanged.

## Verification

- Local `pnpm -r typecheck`, `pnpm build`, and UI token gates pass after
rebase. The UI typecheck and build also pass after the final navigation
fixes. All 37 focused navigation tests pass. The first full local `pnpm
test:run` invocation reported seven chat failures. All seven passed in
isolated reruns after fixture updates and host cleanup. This invocation
was stopped after the full final-head CI suite passed.
- Initial local database tests hit macOS shared-memory exhaustion. A
diagnostic confirmed `shmget: No space left on device` with the system
limit of 32 IDs. Three abandoned, unattached PostgreSQL IPC headers were
reclaimed. The isolated rerun passed all 31 fast-response and channel
tests. The two additional Slack regression cases also passed in a
separate rerun. No active database was stopped.
- UI token gates pass. Provider contract tests cover all supported API
connection types. Fast-response tests cover uniqueness, authorization,
budgets, expiry, cancellation, restart, real-reply races, and the
experimental flag.
- The test drive verified that disabling the experiment hides
Connections and redirects its direct URL. Enabling it restores the saved
OpenRouter setup. After restart on the final commit, the OpenRouter
settings sample returned “Sure, I’ll take care of fixing the border
around the settings panel.” in 428 ms. It used 260 input and 31 output
tokens and recorded $0.0000576 in reported cost.
- Earlier live OpenRouter verification produced a receipt in 1.013
seconds for a new task. The real agent then supplied its substantive
answer. A separate settings test took 1.34 seconds, used 246 input and
23 output tokens, and recorded $0.0000361.
- Live Slack verification on final commit
`24016fa525961ebc7a4acffdc5019ce123970549` passed for a fresh initial
task and a follow-up in a disposable bot DM. The initial acknowledgement
appeared 2.405 seconds after the Slack message and 1.413 seconds after
durable acceptance; the follow-up appeared in 2.322 seconds and 1.160
seconds respectively. The recorded generation/settlement durations were
898 ms and 697 ms. The initial call used 291 input / 39 output tokens
and cost $0.00006705; the follow-up used 281 / 47 and cost $0.00007035.
- The Slack acknowledgements retained the assigned agent identity and
`origin: fast_response`, source-turn references, and no fabricated run
ID. A status snapshot after the initial receipt showed the task still
`in_progress` with its real execution run. The agent then read the CSV
fixture and correctly reported two ready items, two blockers, and their
owners. Both replies stayed in the originating DM, and no duplicate
generic working message followed the delivered contextual
acknowledgements. The `new` command started a fresh conversation session
without generating a fast response.
- An earlier eligible Slack turn generated a response but suppressed
publication. Its reported cost was retained, generic progress remained,
and the real agent completed the requested result. This is recorded
separately from the two delivered acknowledgements; the history did not
expose a precise suppression reason.
- Slack verification used real signed provider events, the existing
intake/publication paths, and a user-approved temporary webhook-only
HTTPS tunnel. Subscriptions were limited to direct-message events. The
test bot was paused and the tunnel stopped afterward. Live
channel/group-thread and GitHub journeys remain unverified; their mocked
tests cover intake, destination binding, publication, and continued
underlying work.
- Greptile reviewed final commit
`24016fa525961ebc7a4acffdc5019ce123970549` at 5/5. Both new review
findings are fixed and all threads are resolved. [Final-head CI
run](https://github.com/paperclipai/paperclip/actions/runs/38060015730)
passed. This includes grouped server/chat/workspace tests, serialized
suites, runner checks, browser journeys, typecheck, build, release
registry, policy, and the canary dry run.
- Manual check: enable Experimental fast responses. Open company
Settings → Connections. Select a shared API connection and model. Save
and use Test response. Submit a task message and confirm that its
receipt does not complete the task.

## Risks

- The migration adds receipt and configuration tables plus accounting
references. It uses idempotent statements and follows the current
migration sequence.
- A slow provider, authorization change, or real-agent reply can
suppress an acknowledgement. Existing progress feedback remains
available.
- Unknown provider billing retains a reservation until reconciliation.
External deliveries with uncertain outcomes must not be blindly
repeated.
- Live Slack initial-task and follow-up DM journeys pass. Live Slack
channel/group-thread and GitHub journeys remain unverified. The PR
remains a draft and records these gaps. Final-head repository CI and
automated review pass.

## Model Used

OpenAI Codex, based on GPT-6, with reasoning, repository tools, code
execution, and browser interaction. The runtime does not expose an exact
model snapshot ID or context-window size; these are not asserted.

## 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-10-10 13:09:17 -05:00

1310 lines
41 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { sanitizeQuarantinedCommentForHigherTrust } from "./source-trust.js";
import {
and,
desc,
eq,
gt,
gte,
inArray,
isNull,
lt,
lte,
or,
sql,
} from "drizzle-orm";
import {
agents,
toolConnections,
assets,
issueAttachments,
projects,
authUsers,
budgetPolicies,
budgetReservations,
companies,
companyFastResponses,
companyMemberships,
companySecrets,
costEvents,
fastResponseRequests,
heartbeatRunEvents,
heartbeatRuns,
issueComments,
issues,
chatPublications,
type Db,
} from "@paperclipai/db";
import {
aiConnectionCatalogSlug,
aiConnectionMetadataSchema,
centsToUnits,
unitsToCents,
updateFastResponseSchema,
FAST_RESPONSE_DEADLINE_MS,
type AiProvider,
type FastResponseSettings,
type FastResponseHistoryEntry,
type UpdateFastResponse,
type FastResponseTestResult,
} from "@paperclipai/shared";
import { aiConnectionService } from "./ai-connections.js";
import { instanceSettingsService } from "./instance-settings.js";
import { isAutomaticRecoverySuppressedByPauseHold } from "./recovery/pause-hold-guard.js";
import { accessService } from "./access.js";
import type { AuthorizationActor } from "./authorization.js";
import { forbidden, unprocessable } from "../errors.js";
import {
logActivity,
publishActivity,
type ActivityPublication,
} from "./activity-log.js";
import { withAccountingTransaction } from "./accounting-transaction.js";
import {
budgetPoliciesForRun,
budgetService,
budgetServiceInTransaction,
computeObservedSpend,
type BudgetServiceHooks,
} from "./budgets.js";
import { createCostEventInTransaction } from "./costs.js";
import {
notifyDeliveryWork,
DELIVERY_QUEUES,
} from "./delivery-work-notifications.js";
import { projectSafeChatPublication } from "./chat-publication-projection.js";
import {
fastResponsePrompt,
fastResponseReceipt,
runFastResponseProvider,
type FastResponseOutcome,
type FastResponsePromptInput,
} from "./fast-response-provider.js";
type Request = typeof fastResponseRequests.$inferSelect;
export type FastResponseSource = Pick<
Request,
"companyId" | "issueId" | "agentId" | "responsibleUserId"
> &
Partial<
Pick<
Request,
| "sourceCommentId"
| "endpointId"
| "conversationId"
| "deliveryId"
| "sponsored"
| "sessionGeneration"
>
> & { sourceKey: string; acceptedAt: Date };
export type FastResponseExternalAuthorization = (
tx: Db,
request: Request,
) => Promise<FastResponsePromptInput>;
/** A run created for this turn is not older work that this turn is waiting on. */
export async function fastResponseTurnQueued(
db: Db,
request: Pick<
Request,
"companyId" | "agentId" | "issueId" | "sourceCommentId" | "acceptedAt"
>,
) {
if (!request.agentId) return false;
const runs = await db
.select({
status: heartbeatRuns.status,
createdAt: heartbeatRuns.createdAt,
context: heartbeatRuns.contextSnapshot,
})
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.companyId, request.companyId),
eq(heartbeatRuns.agentId, request.agentId),
inArray(heartbeatRuns.status, ["queued", "running"]),
),
);
return runs.some((run) => {
const sourceCommentId =
run.context?.wakeCommentId ?? run.context?.commentId;
const ownsTurn =
run.context?.issueId === request.issueId &&
(request.sourceCommentId
? sourceCommentId === request.sourceCommentId
: run.createdAt >= request.acceptedAt);
return (
run.status === "queued" ||
(!ownsTurn && run.createdAt < request.acceptedAt)
);
});
}
/** Board chat streams without a heartbeat run; fence its receipt when real text begins. */
export async function supersedeFastResponse(
db: Db,
companyId: string,
sourceCommentId: string,
) {
await db
.update(fastResponseRequests)
.set({
errorCode: "real_reply_started",
publicationStatus: "suppressed",
status: sql`case when ${fastResponseRequests.status} = 'pending' then 'skipped' else ${fastResponseRequests.status} end`,
})
.where(
and(
eq(fastResponseRequests.companyId, companyId),
eq(fastResponseRequests.sourceCommentId, sourceCommentId),
isNull(fastResponseRequests.commentId),
inArray(fastResponseRequests.status, ["pending", "running"]),
),
);
}
/** Called only at an authenticated, accepted human-turn boundary, on its transaction. */
export async function enqueueFastResponse(tx: Db, source: FastResponseSource) {
if (!(await instanceSettingsService(tx).getExperimental()).enableFastResponses) return;
const [config] = await tx
.select({ enabled: companyFastResponses.enabled })
.from(companyFastResponses)
.where(eq(companyFastResponses.companyId, source.companyId));
if (
!config?.enabled ||
!source.issueId ||
(!source.agentId && !source.sourceKey.startsWith("board:"))
)
return;
const [comment] = source.sourceCommentId
? await tx
.select()
.from(issueComments)
.where(
and(
eq(issueComments.companyId, source.companyId),
eq(issueComments.id, source.sourceCommentId),
),
)
: [];
if (
comment &&
(comment.origin === "fast_response" ||
/^\s*\//.test(comment.body) ||
comment.deletedAt)
)
return;
if (source.acceptedAt.getTime() + FAST_RESPONSE_DEADLINE_MS <= Date.now())
return;
await notifyDeliveryWork(tx, DELIVERY_QUEUES.fastResponse);
await tx
.insert(fastResponseRequests)
.values({
...source,
expiresAt: new Date(
source.acceptedAt.getTime() + FAST_RESPONSE_DEADLINE_MS,
),
})
.onConflictDoNothing();
}
/** Shared with external transport admission. Caller already checks destination authority. */
export async function fastResponseSourceCurrent(tx: Db, request: Request) {
if (!(await instanceSettingsService(tx).getExperimental()).enableFastResponses) return false;
if (!request.issueId || request.expiresAt.getTime() <= Date.now())
return false;
const [company] = await tx
.select({ status: companies.status })
.from(companies)
.where(eq(companies.id, request.companyId));
const [agent] = request.agentId
? await tx
.select({ status: agents.status })
.from(agents)
.where(
and(
eq(agents.id, request.agentId),
eq(agents.companyId, request.companyId),
),
)
: [];
if (
company?.status !== "active" ||
(request.agentId &&
(!agent || ["paused", "terminated"].includes(agent.status)))
)
return false;
const [issue] = await tx
.select()
.from(issues)
.where(
and(
eq(issues.id, request.issueId),
eq(issues.companyId, request.companyId),
),
)
.for("update");
if (
!issue ||
issue.hiddenAt ||
(request.agentId && issue.assigneeAgentId !== request.agentId) ||
["done", "cancelled"].includes(issue.status) ||
(request.sessionGeneration !== null &&
issue.conversationSessionGeneration !== request.sessionGeneration)
)
return false;
if (
await isAutomaticRecoverySuppressedByPauseHold(
tx,
request.companyId,
request.issueId,
)
)
return false;
const [source] = request.sourceCommentId
? await tx
.select()
.from(issueComments)
.where(
and(
eq(issueComments.companyId, request.companyId),
eq(issueComments.id, request.sourceCommentId),
isNull(issueComments.deletedAt),
),
)
: [];
if (
request.sourceCommentId &&
(!source || source.updatedAt.getTime() > request.acceptedAt.getTime() + 10)
)
return false;
const [answer] = await tx
.select({ id: issueComments.id })
.from(issueComments)
.where(
and(
eq(issueComments.companyId, request.companyId),
eq(issueComments.issueId, request.issueId),
eq(issueComments.origin, "comment"),
isNull(issueComments.deletedAt),
gt(issueComments.createdAt, source?.createdAt ?? request.acceptedAt),
request.agentId
? eq(issueComments.authorAgentId, request.agentId)
: eq(issueComments.authorUserId, "board-concierge"),
),
)
.limit(1);
const [stopped] = await tx
.select({ id: heartbeatRuns.id })
.from(heartbeatRuns)
.where(
and(
eq(heartbeatRuns.companyId, request.companyId),
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${request.issueId}`,
inArray(heartbeatRuns.status, ["cancelled", "failed", "succeeded"]),
request.sourceCommentId
? sql`coalesce(${heartbeatRuns.contextSnapshot}->>'wakeCommentId', ${heartbeatRuns.contextSnapshot}->>'commentId') = ${request.sourceCommentId}`
: gte(heartbeatRuns.createdAt, request.acceptedAt),
),
)
.limit(1);
const [streamed] = await tx
.select({ id: heartbeatRunEvents.id })
.from(heartbeatRunEvents)
.innerJoin(heartbeatRuns, eq(heartbeatRuns.id, heartbeatRunEvents.runId))
.where(
and(
eq(heartbeatRunEvents.companyId, request.companyId),
gt(heartbeatRunEvents.createdAt, request.acceptedAt),
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${request.issueId}`,
inArray(heartbeatRunEvents.eventType, ["item.delta", "item.completed"]),
sql`${heartbeatRunEvents.payload}->'prpEvent'->'payload'->>'kind' = 'agentMessage'`,
sql`length(coalesce(${heartbeatRunEvents.payload}->'prpEvent'->'payload'->>'text', '')) > 0`,
),
)
.limit(1);
return !answer && !stopped && !streamed;
}
export function fastResponseService(
db: Db,
options: {
provider?: typeof runFastResponseProvider;
budgetHooks?: BudgetServiceHooks;
authorizeExternal?: FastResponseExternalAuthorization;
publishExternal?: (
tx: Db,
request: Request,
commentId: string,
text: string,
) => Promise<boolean>;
} = {},
) {
const providerCall = options.provider ?? runFastResponseProvider;
async function settings(companyId: string): Promise<FastResponseSettings> {
const [row] = await db
.select()
.from(companyFastResponses)
.where(eq(companyFastResponses.companyId, companyId));
return row
? { ...row, provider: row.provider as AiProvider }
: {
companyId,
enabled: false,
connectionId: null,
grantId: null,
provider: null,
model: null,
allowSponsored: true,
};
}
async function choices(companyId: string, userId: string) {
return (await aiConnectionService(db).list(companyId, userId)).filter(
(c) => c.ownership === "shared" && c.method === "api_key",
);
}
async function configure(
companyId: string,
userId: string,
input: UpdateFastResponse,
) {
const data = updateFastResponseSchema.parse(input);
return withAccountingTransaction(
db,
companyId,
async (tx, publications) => {
const [previous] = await tx
.select()
.from(companyFastResponses)
.where(eq(companyFastResponses.companyId, companyId));
const disabling =
!data.enabled &&
previous?.connectionId === data.connectionId &&
previous?.grantId === data.grantId;
const connection =
!disabling && data.connectionId && data.grantId
? await aiConnectionService(tx).selectFastResponse({
companyId,
userId,
connectionId: data.connectionId,
grantId: data.grantId,
})
: null;
const values = {
...data,
companyId,
provider: disabling
? previous.provider
: (connection?.provider ?? null),
updatedAt: new Date(),
};
await tx
.insert(companyFastResponses)
.values(values)
.onConflictDoUpdate({
target: companyFastResponses.companyId,
set: values,
});
await logActivity(
tx,
{
companyId,
actorType: "user",
actorId: userId,
action: "fast_response.configured",
entityType: "company",
entityId: companyId,
details: data,
},
publications,
);
return { ...values, provider: values.provider as AiProvider | null };
},
);
}
async function resolve(
tx: Db,
request: Pick<
Request,
| "companyId"
| "issueId"
| "responsibleUserId"
| "agentId"
| "sponsored"
| "endpointId"
> &
Partial<Pick<Request, "connectionId" | "grantId" | "model">>,
) {
if (!(await instanceSettingsService(tx).getExperimental()).enableFastResponses)
throw unprocessable("disabled");
const [config] = await tx
.select()
.from(companyFastResponses)
.where(eq(companyFastResponses.companyId, request.companyId));
if (
!config?.enabled ||
!config.connectionId ||
!config.grantId ||
!config.model
)
throw unprocessable("disabled");
if (
request.connectionId &&
(request.connectionId !== config.connectionId ||
request.grantId !== config.grantId ||
request.model !== config.model)
)
throw forbidden("configuration_changed");
if (
request.sponsored &&
(!config.allowSponsored ||
request.responsibleUserId ||
!request.endpointId)
)
throw forbidden("sponsorship_disabled");
const memberships = request.responsibleUserId
? await tx
.select({
companyId: companyMemberships.companyId,
membershipRole: companyMemberships.membershipRole,
status: companyMemberships.status,
})
.from(companyMemberships)
.where(
and(
eq(companyMemberships.companyId, request.companyId),
eq(companyMemberships.principalType, "user"),
eq(companyMemberships.principalId, request.responsibleUserId),
),
)
: [];
if (!request.sponsored && !request.responsibleUserId)
throw forbidden("responsible_user_missing");
const [issue] = request.issueId
? await tx
.select()
.from(issues)
.where(
and(
eq(issues.companyId, request.companyId),
eq(issues.id, request.issueId),
),
)
: [];
if (request.issueId && !issue) throw forbidden();
if (!request.sponsored && issue) {
const actor: AuthorizationActor = {
type: "board",
userId: request.responsibleUserId!,
source:
request.responsibleUserId === "local-board"
? "local_implicit"
: "session",
companyIds: memberships.map((m) => m.companyId),
memberships,
};
if (
!(
await accessService(tx).decide({
actor,
action: "issue:read",
resource: {
type: "issue",
companyId: request.companyId,
issueId: issue.id,
projectId: issue.projectId,
parentIssueId: issue.parentId,
assigneeAgentId: issue.assigneeAgentId,
assigneeUserId: issue.assigneeUserId,
status: issue.status,
},
})
).allowed
)
throw forbidden();
}
const connection = await aiConnectionService(tx).selectFastResponse({
companyId: request.companyId,
connectionId: config.connectionId,
grantId: config.grantId,
userId: request.responsibleUserId,
agentId: request.agentId,
sponsoredBackground: request.sponsored,
});
return { config, connection, issue };
}
async function availability(companyId: string, userId: string) {
if (!(await instanceSettingsService(db).getExperimental()).enableFastResponses)
return { available: false as const, reason: "disabled" };
try {
await resolve(db, {
companyId,
responsibleUserId: userId,
issueId: null,
agentId: null,
sponsored: false,
endpointId: null,
});
return { available: true as const };
} catch {
return { available: false as const, reason: "connection_unavailable" };
}
}
async function skip(request: Request, reason: string) {
await db
.update(fastResponseRequests)
.set({
status: "skipped",
publicationStatus: "suppressed",
errorCode: reason,
finishedAt: new Date(),
})
.where(
and(
eq(fastResponseRequests.id, request.id),
eq(fastResponseRequests.status, "pending"),
),
);
}
async function settle(request: Request, outcome: FastResponseOutcome) {
await withAccountingTransaction(
db,
request.companyId,
async (tx, publications) => {
const [current] = await tx
.select()
.from(fastResponseRequests)
.where(eq(fastResponseRequests.id, request.id))
.for("update");
if (!current || current.status !== "running") return;
const receipt = outcome.receipt;
const links: {
agentId: string | null;
issueId: string | null;
projectId: string | null;
} = { agentId: null, issueId: null, projectId: null };
for (const [key, table] of [
["agentId", agents],
["issueId", issues],
["projectId", projects],
] as const) {
const id = request[key];
if (!id) continue;
const [row] = await tx
.select({ id: table.id })
.from(table)
.where(
and(eq(table.id, id), eq(table.companyId, request.companyId)),
)
.for("key share");
if (row) links[key] = row.id;
}
const event = await createCostEventInTransaction(
tx,
request.companyId,
{
usageKind: "fast_response",
responsibleUserId: request.responsibleUserId,
...links,
heartbeatRunId: null,
idempotencyKey: `fast-response:${request.id}`,
provider: request.provider!,
biller: request.provider!,
model: request.model!,
billingType: "metered_api",
costStatus: receipt.costStatus,
inputTokens: receipt.inputTokens ?? 0,
outputTokens: receipt.outputTokens ?? 0,
costCents: receipt.costCents ?? "0",
providerRequestId: receipt.providerRequestId,
pricingProvenance: receipt.pricingProvenance,
occurredAt: request.startedAt!,
},
publications,
{
actorType: request.responsibleUserId ? "user" : "system",
actorId: request.responsibleUserId ?? "paperclip-fast-response",
},
);
const finishedAt = new Date();
await tx
.update(fastResponseRequests)
.set({
status: outcome.text
? "succeeded"
: receipt.costStatus === "unpriced"
? "unknown"
: "failed",
errorCode: outcome.errorCode ?? null,
costEventId: event.id,
providerRequestId: receipt.providerRequestId,
inputTokens: receipt.inputTokens,
outputTokens: receipt.outputTokens,
finishedAt,
durationMs: finishedAt.getTime() - request.acceptedAt.getTime(),
...(outcome.text ? {} : { publicationStatus: "suppressed" }),
})
.where(eq(fastResponseRequests.id, request.id));
if (receipt.costStatus !== "unpriced")
await tx
.update(budgetReservations)
.set({
state: outcome.noProviderWork ? "released" : "settled",
settledAt: finishedAt,
})
.where(
and(
eq(budgetReservations.companyId, request.companyId),
eq(budgetReservations.fastResponseRequestId, request.id),
eq(budgetReservations.state, "held"),
),
);
},
);
await budgetService(db, options.budgetHooks).deliverPendingEnforcement(
request.companyId,
);
}
async function process(
request: Request,
signal?: AbortSignal,
): Promise<FastResponseTestResult> {
let selected: Awaited<ReturnType<typeof resolve>>,
credential: string,
prompt: string;
let secretId: string | undefined,
secret: typeof companySecrets.$inferSelect | undefined;
try {
selected = await resolve(db, request);
const [agent] = request.agentId
? await db
.select({ name: agents.name })
.from(agents)
.where(
and(
eq(agents.companyId, request.companyId),
eq(agents.id, request.agentId),
),
)
: [];
if (request.endpointId) {
if (!options.authorizeExternal) throw forbidden();
const context = await db.transaction((tx) =>
options.authorizeExternal!(tx as unknown as Db, request),
);
prompt = fastResponsePrompt(context);
} else if (request.issueId) {
const [source] = request.sourceCommentId
? await db
.select()
.from(issueComments)
.where(
and(
eq(issueComments.companyId, request.companyId),
eq(issueComments.id, request.sourceCommentId),
),
)
: [];
const recent = await db
.select({ body: issueComments.body, sourceTrust: issueComments.sourceTrust })
.from(issueComments)
.where(
and(
eq(issueComments.companyId, request.companyId),
eq(issueComments.issueId, request.issueId),
isNull(issueComments.deletedAt),
eq(issueComments.origin, "comment"),
lt(
issueComments.createdAt,
source?.createdAt ?? request.acceptedAt,
),
),
)
.orderBy(desc(issueComments.createdAt))
.limit(2);
const attachments = request.sourceCommentId
? await db
.select({
name: assets.originalFilename,
type: assets.contentType,
})
.from(issueAttachments)
.innerJoin(
assets,
and(
eq(assets.id, issueAttachments.assetId),
eq(assets.companyId, request.companyId),
),
)
.where(
and(
eq(issueAttachments.companyId, request.companyId),
eq(issueAttachments.issueCommentId, request.sourceCommentId),
),
)
.limit(5)
: [];
prompt = fastResponsePrompt({
attachments: attachments.map((a) => `${a.name} (${a.type})`),
agentName: agent?.name ?? "Board assistant",
message:
(source ? sanitizeQuarantinedCommentForHigherTrust(source).body : undefined) ??
selected.issue?.description ??
selected.issue?.title ??
"",
title: selected.issue?.title,
recent: recent.reverse().map((c) => sanitizeQuarantinedCommentForHigherTrust(c).body),
queued: await fastResponseTurnQueued(db, request),
});
} else
prompt = fastResponsePrompt({
agentName: "Assistant",
message: "Fix the border around the settings panel.",
});
secretId = selected.connection.grant.credentialSecretRefs.find(
(r) => r.configPath === "ai.credential",
)?.secretId;
[secret] = secretId
? await db
.select()
.from(companySecrets)
.where(
and(
eq(companySecrets.id, secretId),
eq(companySecrets.companyId, request.companyId),
),
)
: [];
credential = await aiConnectionService(db).credential(
selected.connection,
0,
{
responsibleUserId: request.responsibleUserId,
actorType: request.responsibleUserId ? "user" : "system",
actorId: request.responsibleUserId ?? "paperclip-fast-response",
issueId: request.issueId,
},
);
} catch (error) {
const reason = error instanceof Error && error.message === "disabled"
? "disabled"
: "connection_unavailable";
await skip(request, reason);
return { status: "unavailable", reason };
}
const metadata = aiConnectionMetadataSchema.parse(
selected.connection.connection.config.ai,
);
let admitted: Request | null = null;
try {
admitted = await withAccountingTransaction(
db,
request.companyId,
async (tx) => {
if (request.expiresAt.getTime() <= Date.now() || signal?.aborted)
return null;
const current = await resolve(tx, request);
if (
current.config.updatedAt.getTime() !==
selected.config.updatedAt.getTime() ||
current.connection.connection.updatedAt.getTime() !==
selected.connection.connection.updatedAt.getTime() ||
current.connection.grant.updatedAt.getTime() !==
selected.connection.grant.updatedAt.getTime()
)
return null;
if (secretId) {
const [now] = await tx
.select()
.from(companySecrets)
.where(eq(companySecrets.id, secretId))
.for("share");
if (
!now ||
now.status !== "active" ||
now.latestVersion !== secret?.latestVersion
)
return null;
}
if (
request.issueId &&
!(await fastResponseSourceCurrent(tx, request))
)
return null;
if (request.endpointId) await options.authorizeExternal!(tx, request);
const [company] = await tx
.select()
.from(companies)
.where(eq(companies.id, request.companyId));
const [agent] = request.agentId
? await tx
.select()
.from(agents)
.where(
and(
eq(agents.id, request.agentId),
eq(agents.companyId, request.companyId),
),
)
: [];
if (
company?.status !== "active" ||
(request.agentId &&
(!agent || ["paused", "terminated"].includes(agent.status)))
)
return null;
if (
await budgetServiceInTransaction(tx, []).getInvocationBlock(
request.companyId,
request.agentId,
{ projectId: current.issue?.projectId },
)
)
return null;
const policies = await tx
.select()
.from(budgetPolicies)
.where(
and(
eq(budgetPolicies.companyId, request.companyId),
eq(budgetPolicies.isActive, true),
eq(budgetPolicies.hardStopEnabled, true),
eq(budgetPolicies.metric, "billed_cents"),
sql`${budgetPolicies.amount} > 0`,
budgetPoliciesForRun(
request.companyId,
request.agentId,
current.issue?.projectId ?? null,
),
),
);
const amount = policies.reduce((max, p) => {
const n = centsToUnits(p.reservationCents);
return n > max ? n : max;
}, 0n);
for (const policy of policies) {
const [held] = await tx
.select({
total: sql<string>`coalesce(sum(${budgetReservations.amountCents}),0)::text`,
})
.from(budgetReservations)
.where(
and(
eq(budgetReservations.companyId, request.companyId),
eq(budgetReservations.state, "held"),
policy.scopeType === "agent"
? eq(budgetReservations.agentId, request.agentId!)
: undefined,
policy.scopeType === "project"
? eq(
budgetReservations.projectId,
current.issue!.projectId!,
)
: undefined,
),
);
const committed =
centsToUnits(
(await computeObservedSpend(tx, policy)).totalExact,
) + centsToUnits(held.total);
if (
committed >= centsToUnits(policy.amount) ||
committed + amount > centsToUnits(policy.amount)
)
return null;
}
const [claimed] = await tx
.update(fastResponseRequests)
.set({
status: "running",
startedAt: new Date(),
connectionId: current.config.connectionId,
grantId: current.config.grantId,
model: current.config.model,
provider: aiConnectionCatalogSlug(
current.connection.provider,
metadata.routing,
),
projectId: current.issue?.projectId ?? null,
})
.where(
and(
eq(fastResponseRequests.id, request.id),
eq(fastResponseRequests.status, "pending"),
),
)
.returning();
if (!claimed) return null;
await tx.insert(budgetReservations).values({
companyId: request.companyId,
fastResponseRequestId: request.id,
agentId: request.agentId,
projectId: current.issue?.projectId,
amountCents: unitsToCents(amount),
providerStartedAt: claimed.startedAt,
});
return claimed;
},
);
} catch {
/* An optional receipt must never fail the accepted turn. */
}
if (!admitted) {
await skip(request, "not_admitted");
return { status: "unavailable", reason: "not_admitted" };
}
const timeout = AbortSignal.timeout(
Math.max(1, request.expiresAt.getTime() - Date.now()),
);
let outcome: FastResponseOutcome;
try {
outcome = await providerCall({
metadata,
credential,
model: admitted.model!,
prompt,
signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
});
} catch {
outcome = {
errorCode: "provider_failed",
receipt: fastResponseReceipt(),
};
}
// Commit the visible receipt first: accounting settlement must not spend its display deadline.
if (outcome.text && request.issueId) {
const text = outcome.text;
try {
const publications: ActivityPublication[] = [];
await db.transaction(async (tx) => {
if (!(await fastResponseSourceCurrent(tx as unknown as Db, request)))
return;
await resolve(tx as unknown as Db, admitted!);
if (request.endpointId)
await options.authorizeExternal!(tx as unknown as Db, request);
const [locked] = await tx
.select()
.from(fastResponseRequests)
.where(eq(fastResponseRequests.id, request.id))
.for("update");
if (
!locked ||
locked.commentId ||
locked.status !== "running" ||
locked.errorCode === "real_reply_started"
)
return;
if (request.expiresAt.getTime() <= Date.now()) return;
const safe = projectSafeChatPublication({
classification: "external",
source: "agent_comment",
text,
});
const [source] = request.sourceCommentId
? await tx
.select({ sourceTrust: issueComments.sourceTrust })
.from(issueComments)
.where(
and(
eq(issueComments.companyId, request.companyId),
eq(issueComments.id, request.sourceCommentId),
),
)
: [];
const [comment] = await tx
.insert(issueComments)
.values({
companyId: request.companyId,
issueId: request.issueId!,
authorAgentId: request.agentId,
authorUserId: request.agentId ? null : "board-concierge",
authorType: request.agentId ? "agent" : "system",
body: safe.text,
sourceTrust: source?.sourceTrust ?? null,
conversationSessionGeneration: request.sessionGeneration,
origin: "fast_response",
fastResponseRequestId: request.id,
})
.returning();
if (
request.endpointId &&
request.conversationId &&
!(await options.publishExternal?.(
tx as unknown as Db,
request,
comment.id,
safe.text,
))
)
await tx.insert(chatPublications).values({
companyId: request.companyId,
issueId: request.issueId!,
endpointId: request.endpointId,
conversationId: request.conversationId,
commentId: comment.id,
idempotencyKey: `fast-response:${request.id}`,
payload: safe,
});
await tx
.update(fastResponseRequests)
.set({
commentId: comment.id,
publicationStatus: request.endpointId ? "pending" : "published",
})
.where(eq(fastResponseRequests.id, request.id));
await logActivity(
tx as unknown as Db,
{
companyId: request.companyId,
actorType: "system",
actorId: "paperclip-fast-response",
action: "issue.comment_added",
entityType: "issue",
entityId: request.issueId!,
details: { commentId: comment.id, origin: "fast_response" },
},
publications,
);
});
for (const publication of publications) publishActivity(publication);
} catch {
/* Never substitute an unsafe or obsolete acknowledgement. */
}
}
await settle(admitted, outcome);
await db
.update(fastResponseRequests)
.set({ publicationStatus: request.issueId ? "suppressed" : "test" })
.where(
and(
eq(fastResponseRequests.id, request.id),
isNull(fastResponseRequests.commentId),
),
);
return outcome.text
? {
status: "succeeded",
text: outcome.text,
durationMs: Date.now() - request.acceptedAt.getTime(),
invocationId: request.id,
usage: outcome.receipt,
}
: {
status: "failed",
reason: outcome.errorCode ?? "provider_failed",
invocationId: request.id,
};
}
async function test(companyId: string, userId: string) {
const now = new Date();
const [request] = await db
.insert(fastResponseRequests)
.values({
companyId,
sourceKey: `test:${randomUUID()}`,
responsibleUserId: userId,
acceptedAt: now,
expiresAt: new Date(now.getTime() + FAST_RESPONSE_DEADLINE_MS),
})
.returning();
return process(request);
}
async function sweepPending(signal?: AbortSignal) {
const stale = await db
.select()
.from(fastResponseRequests)
.where(
and(
eq(fastResponseRequests.status, "running"),
lt(fastResponseRequests.expiresAt, new Date(Date.now() - 30_000)),
),
)
.limit(50);
for (const row of stale)
await settle(row, {
errorCode: "interrupted",
receipt: fastResponseReceipt(),
});
await db
.update(fastResponseRequests)
.set({
status: "skipped",
publicationStatus: "suppressed",
errorCode: "expired",
finishedAt: new Date(),
})
.where(
and(
eq(fastResponseRequests.status, "pending"),
lte(fastResponseRequests.expiresAt, new Date()),
),
);
const pending = await db
.select()
.from(fastResponseRequests)
.where(
and(
eq(fastResponseRequests.status, "pending"),
sql`${fastResponseRequests.sourceKey} not like 'test:%'`,
gt(fastResponseRequests.expiresAt, new Date()),
),
)
.orderBy(fastResponseRequests.acceptedAt)
.limit(8);
await Promise.allSettled(pending.map((row) => process(row, signal)));
}
async function hasPending() {
const [row] = await db
.select({ id: fastResponseRequests.id })
.from(fastResponseRequests)
.where(inArray(fastResponseRequests.status, ["pending", "running"]))
.limit(1);
return Boolean(row);
}
async function history(
companyId: string,
actor: AuthorizationActor,
range: { from?: Date; to?: Date; limit?: number } = {},
): Promise<FastResponseHistoryEntry[]> {
if (
!(
await accessService(db).decide({
actor,
action: "company_scope:read",
resource: { type: "company", companyId },
})
).allowed
)
throw forbidden();
const rows = await db
.select({
request: fastResponseRequests,
userName: authUsers.name,
cost: costEvents,
issue: issues,
publicationState: chatPublications.state,
connectionName: toolConnections.name,
})
.from(fastResponseRequests)
.leftJoin(
costEvents,
and(
eq(costEvents.companyId, companyId),
eq(costEvents.id, fastResponseRequests.costEventId),
),
)
.leftJoin(
authUsers,
eq(authUsers.id, fastResponseRequests.responsibleUserId),
)
.leftJoin(
toolConnections,
and(
eq(toolConnections.companyId, companyId),
eq(toolConnections.id, fastResponseRequests.connectionId),
),
)
.leftJoin(
chatPublications,
and(
eq(chatPublications.companyId, companyId),
eq(
chatPublications.idempotencyKey,
sql`'fast-response:' || ${fastResponseRequests.id}::text`,
),
),
)
.leftJoin(
issues,
and(
eq(issues.companyId, companyId),
eq(issues.id, fastResponseRequests.issueId),
),
)
.where(
and(
eq(fastResponseRequests.companyId, companyId),
range.from
? gte(fastResponseRequests.acceptedAt, range.from)
: undefined,
range.to ? lte(fastResponseRequests.acceptedAt, range.to) : undefined,
),
)
.orderBy(desc(fastResponseRequests.acceptedAt))
.limit(Math.min(range.limit ?? 100, 500));
return Promise.all(
rows.map(
async ({
request: r,
userName,
cost,
issue,
publicationState,
connectionName,
}) => {
const visible =
issue &&
(
await accessService(db).decide({
actor,
action: "issue:read",
resource: {
type: "issue",
companyId,
issueId: issue.id,
projectId: issue.projectId,
parentIssueId: issue.parentId,
assigneeAgentId: issue.assigneeAgentId,
assigneeUserId: issue.assigneeUserId,
status: issue.status,
},
})
).allowed;
return {
id: r.id,
feature: r.sourceKey.startsWith("test:")
? "settings.test"
: "message",
actorType: r.sponsored ? "system" : "user",
responsibleUserId: r.responsibleUserId,
userName,
issueId: visible ? issue!.id : null,
issueIdentifier: visible ? issue!.identifier : null,
agentId: r.agentId,
runId: null,
connectionId: r.connectionId ?? "",
provider: r.provider ?? "",
model: r.model ?? "",
status: r.status,
connectionName,
publicationStatus: publicationState ?? r.publicationStatus,
errorCode: r.errorCode,
startedAt: r.acceptedAt.toISOString(),
finishedAt: r.finishedAt?.toISOString() ?? null,
durationMs: r.durationMs,
inputTokens: r.inputTokens,
outputTokens: r.outputTokens,
costCents:
cost && cost.costStatus !== "unpriced"
? String(cost.costCents)
: null,
costStatus: cost?.costStatus ?? null,
};
},
),
);
}
return {
settings,
choices,
configure,
availability,
test,
history,
process,
sweepPending,
hasPending,
authorize: (tx: Db, request: Request) => resolve(tx, request),
};
}