mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-17 10:06:42 +02:00
## 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>
630 lines
20 KiB
TypeScript
630 lines
20 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||
import { randomUUID } from "node:crypto";
|
||
import { mkdtemp, realpath, rm } from "node:fs/promises";
|
||
import os from "node:os";
|
||
import path from "node:path";
|
||
import { and, eq } from "drizzle-orm";
|
||
import {
|
||
createDb,
|
||
companies,
|
||
agents,
|
||
companyMemberships,
|
||
companyFastResponses,
|
||
costEvents,
|
||
fastResponseRequests,
|
||
connectionGrants,
|
||
connectionGrantMembers,
|
||
budgetPolicies,
|
||
budgetReservations,
|
||
heartbeatRunEvents,
|
||
heartbeatRuns,
|
||
issues,
|
||
issueComments,
|
||
} from "@paperclipai/db";
|
||
import { startEmbeddedPostgresTestDatabase } from "@paperclipai/db/test-embedded-postgres";
|
||
import { aiConnectionService } from "../services/ai-connections.js";
|
||
import {
|
||
enqueueFastResponse,
|
||
supersedeFastResponse,
|
||
fastResponseTurnQueued,
|
||
fastResponseService,
|
||
} from "../services/fast-responses.js";
|
||
import {
|
||
fastResponseReceipt,
|
||
type FastResponseOutcome,
|
||
} from "../services/fast-response-provider.js";
|
||
import { instanceSettingsService } from "../services/instance-settings.js";
|
||
import { settleConversationTurn } from "../services/agent-conversations.js";
|
||
import { buildLowTrustSourceTrust, LOW_TRUST_QUARANTINED_BODY } from "../services/source-trust.js";
|
||
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>,
|
||
db: ReturnType<typeof createDb>,
|
||
home: string;
|
||
beforeAll(async () => {
|
||
home = await realpath(
|
||
await mkdtemp(path.join(os.tmpdir(), "paperclip-fast-response-")),
|
||
);
|
||
vi.stubEnv("PAPERCLIP_HOME", home);
|
||
vi.stubEnv("PAPERCLIP_INSTANCE_ID", "fast-response-fixture");
|
||
database = await startEmbeddedPostgresTestDatabase(
|
||
"paperclip-fast-response-db-",
|
||
);
|
||
db = createDb(database.connectionString);
|
||
}, 90000);
|
||
afterAll(async () => {
|
||
await database?.cleanup();
|
||
vi.unstubAllEnvs();
|
||
if (home) await rm(home, { recursive: true, force: true });
|
||
});
|
||
const outcome: FastResponseOutcome = {
|
||
text: "I’ll check the settings panel’s border styling and work on the fix.",
|
||
receipt: fastResponseReceipt({
|
||
usage: { inputTokens: 80, outputTokens: 16 },
|
||
response: { id: "response-1", body: { usage: { cost: 0.00001 } } },
|
||
}),
|
||
};
|
||
let sequence = 0;
|
||
async function fixture() {
|
||
await instanceSettingsService(db).updateExperimental({ enableFastResponses: true });
|
||
const companyId = randomUUID(),
|
||
agentId = randomUUID(),
|
||
issueId = randomUUID();
|
||
await db.insert(companies).values({
|
||
id: companyId,
|
||
name: "Fast response tests",
|
||
issuePrefix: `FR${++sequence}`,
|
||
});
|
||
await db.insert(agents).values({
|
||
id: agentId,
|
||
companyId,
|
||
name: "Alex",
|
||
adapterType: "codex_local",
|
||
});
|
||
await db.insert(companyMemberships).values(
|
||
["alice", "bob"].map((principalId) => ({
|
||
companyId,
|
||
principalId,
|
||
principalType: "user",
|
||
status: "active",
|
||
membershipRole: "owner",
|
||
})),
|
||
);
|
||
const binding = await aiConnectionService(db).save(
|
||
companyId,
|
||
"alice",
|
||
{
|
||
provider: "openrouter",
|
||
method: "api_key",
|
||
name: "Fast key",
|
||
ownership: "shared",
|
||
apiKey: "fixture-key",
|
||
agentIds: [],
|
||
allAgents: true,
|
||
},
|
||
"fixture-key",
|
||
);
|
||
const provider = vi.fn().mockResolvedValue(structuredClone(outcome)),
|
||
service = fastResponseService(db, { provider });
|
||
const config = {
|
||
connectionId: binding.connectionId,
|
||
grantId: binding.grantId,
|
||
enabled: true,
|
||
model: "openai/gpt-oss-120b",
|
||
allowSponsored: true,
|
||
};
|
||
await service.configure(companyId, "alice", config);
|
||
await db.insert(issues).values({
|
||
id: issueId,
|
||
companyId,
|
||
title: "Settings panel",
|
||
status: "in_progress",
|
||
assigneeAgentId: agentId,
|
||
});
|
||
const [comment] = await db
|
||
.insert(issueComments)
|
||
.values({
|
||
companyId,
|
||
issueId,
|
||
authorUserId: "alice",
|
||
body: "Fix the border styling",
|
||
})
|
||
.returning();
|
||
const source = {
|
||
companyId,
|
||
agentId,
|
||
issueId,
|
||
responsibleUserId: "alice",
|
||
sourceCommentId: comment.id,
|
||
sourceKey: `comment:${comment.id}`,
|
||
acceptedAt: comment.createdAt,
|
||
};
|
||
const enqueue = async () => {
|
||
await db.transaction((tx) =>
|
||
enqueueFastResponse(tx as unknown as typeof db, source),
|
||
);
|
||
return (
|
||
await db
|
||
.select()
|
||
.from(fastResponseRequests)
|
||
.where(eq(fastResponseRequests.companyId, companyId))
|
||
)[0];
|
||
};
|
||
return {
|
||
companyId,
|
||
agentId,
|
||
issueId,
|
||
comment,
|
||
source,
|
||
config,
|
||
binding,
|
||
provider,
|
||
service,
|
||
enqueue,
|
||
};
|
||
}
|
||
describe("fast response accepted turns", () => {
|
||
it("does not enqueue or infer while the experimental setting is disabled", async () => {
|
||
const f = await fixture();
|
||
await instanceSettingsService(db).updateExperimental({ enableFastResponses: false });
|
||
expect(await f.enqueue()).toBeUndefined();
|
||
expect(await f.service.availability(f.companyId, "alice")).toMatchObject({ available: false });
|
||
expect(await f.service.test(f.companyId, "alice")).toMatchObject({ status: "unavailable", reason: "disabled" });
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("suppresses pending work when the experimental setting is switched off", async () => {
|
||
const f = await fixture();
|
||
const request = await f.enqueue();
|
||
await instanceSettingsService(db).updateExperimental({ enableFastResponses: false });
|
||
await f.service.process(request);
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
const [row] = await db.select().from(fastResponseRequests).where(eq(fastResponseRequests.id, request.id));
|
||
expect(row).toMatchObject({ status: "skipped", publicationStatus: "suppressed", commentId: null });
|
||
});
|
||
|
||
it("retains charges but suppresses a response when disabled during inference", async () => {
|
||
const f = await fixture();
|
||
const request = await f.enqueue();
|
||
f.provider.mockImplementationOnce(async () => {
|
||
await instanceSettingsService(db).updateExperimental({ enableFastResponses: false });
|
||
return structuredClone(outcome);
|
||
});
|
||
await f.service.process(request);
|
||
const [row] = await db.select().from(fastResponseRequests).where(eq(fastResponseRequests.id, request.id));
|
||
expect(row).toMatchObject({ status: "succeeded", publicationStatus: "suppressed", commentId: null });
|
||
expect(row.costEventId).toBeTruthy();
|
||
});
|
||
|
||
it.each(["recent", "source"] as const)("keeps quarantined %s text out of the acknowledgement prompt", async location => {
|
||
const f = await fixture();
|
||
const untrusted = "QUARANTINED_PRIVATE_FINDING_DO_NOT_PROMOTE";
|
||
const sourceTrust = buildLowTrustSourceTrust({ issueId: f.issueId, agentId: f.agentId });
|
||
if (location === "source") {
|
||
await db.update(issueComments).set({ body: untrusted, sourceTrust }).where(eq(issueComments.id, f.comment.id));
|
||
} else {
|
||
await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issueId, authorAgentId: f.agentId,
|
||
body: untrusted, sourceTrust, createdAt: new Date(f.source.acceptedAt.getTime() - 1) });
|
||
}
|
||
await f.service.process(await f.enqueue());
|
||
expect(f.provider).toHaveBeenCalledTimes(1);
|
||
const prompt = f.provider.mock.calls[0][0].prompt as string;
|
||
expect(prompt).not.toContain(untrusted);
|
||
expect(prompt).toContain(LOW_TRUST_QUARANTINED_BODY);
|
||
const [receipt] = await db.select().from(issueComments).where(and(eq(issueComments.issueId, f.issueId), eq(issueComments.origin, "fast_response")));
|
||
expect(receipt.body).not.toContain(untrusted);
|
||
expect(receipt.sourceTrust).toEqual(location === "source" ? sourceTrust : null);
|
||
});
|
||
it("distinguishes the current turn's running agent from older or queued work", async () => {
|
||
const f = await fixture();
|
||
expect(await fastResponseTurnQueued(db, f.source)).toBe(false);
|
||
const [run] = await db
|
||
.insert(heartbeatRuns)
|
||
.values({
|
||
companyId: f.companyId,
|
||
agentId: f.agentId,
|
||
status: "running",
|
||
contextSnapshot: { issueId: f.issueId, wakeCommentId: f.comment.id },
|
||
createdAt: new Date(f.source.acceptedAt.getTime() - 1),
|
||
})
|
||
.returning();
|
||
expect(await fastResponseTurnQueued(db, f.source)).toBe(false);
|
||
await db
|
||
.update(heartbeatRuns)
|
||
.set({
|
||
contextSnapshot: { issueId: f.issueId, wakeCommentId: randomUUID() },
|
||
})
|
||
.where(eq(heartbeatRuns.id, run.id));
|
||
expect(await fastResponseTurnQueued(db, f.source)).toBe(true);
|
||
await db
|
||
.update(heartbeatRuns)
|
||
.set({
|
||
status: "queued",
|
||
createdAt: new Date(f.source.acceptedAt.getTime() + 1),
|
||
})
|
||
.where(eq(heartbeatRuns.id, run.id));
|
||
expect(await fastResponseTurnQueued(db, f.source)).toBe(true);
|
||
});
|
||
it("starts disabled, and disabled turns have no request or cost", async () => {
|
||
const f = await fixture();
|
||
await db
|
||
.delete(companyFastResponses)
|
||
.where(eq(companyFastResponses.companyId, f.companyId));
|
||
expect(await f.service.settings(f.companyId)).toMatchObject({
|
||
enabled: false,
|
||
allowSponsored: true,
|
||
});
|
||
expect(await f.enqueue()).toBeUndefined();
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
});
|
||
it("deduplicates concurrent workers and persists provenance without fabricating a run", async () => {
|
||
const f = await fixture();
|
||
const row = await f.enqueue();
|
||
await f.enqueue();
|
||
await Promise.all([f.service.process(row), f.service.process(row)]);
|
||
expect(f.provider).toHaveBeenCalledTimes(1);
|
||
const receipts = await db
|
||
.select()
|
||
.from(issueComments)
|
||
.where(
|
||
and(
|
||
eq(issueComments.companyId, f.companyId),
|
||
eq(issueComments.origin, "fast_response"),
|
||
),
|
||
);
|
||
expect(receipts).toHaveLength(1);
|
||
expect(receipts[0]).toMatchObject({
|
||
authorAgentId: f.agentId,
|
||
createdByRunId: null,
|
||
fastResponseRequestId: row.id,
|
||
});
|
||
expect(
|
||
await db
|
||
.select()
|
||
.from(heartbeatRuns)
|
||
.where(eq(heartbeatRuns.companyId, f.companyId)),
|
||
).toHaveLength(0);
|
||
await db
|
||
.update(issues)
|
||
.set({
|
||
conversationAgentId: f.agentId,
|
||
conversationUserId: "alice",
|
||
conversationState: "active",
|
||
})
|
||
.where(eq(issues.id, f.issueId));
|
||
const [run] = await db
|
||
.insert(heartbeatRuns)
|
||
.values({
|
||
companyId: f.companyId,
|
||
agentId: f.agentId,
|
||
status: "succeeded",
|
||
invocationSource: "on_demand",
|
||
contextSnapshot: { issueId: f.issueId, wakeCommentId: f.comment.id },
|
||
})
|
||
.returning();
|
||
expect(await settleConversationTurn(db, run)).toBe(false);
|
||
const [event] = await db
|
||
.select()
|
||
.from(costEvents)
|
||
.where(eq(costEvents.companyId, f.companyId));
|
||
expect(event).toMatchObject({
|
||
usageKind: "fast_response",
|
||
heartbeatRunId: null,
|
||
costCents: 0.001,
|
||
responsibleUserId: "alice",
|
||
});
|
||
expect(
|
||
(await db.select().from(issues).where(eq(issues.id, f.issueId)))[0]
|
||
.status,
|
||
).toBe("in_progress");
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(budgetReservations)
|
||
.where(eq(budgetReservations.fastResponseRequestId, row.id))
|
||
)[0].state,
|
||
).toBe("settled");
|
||
});
|
||
it.each([
|
||
"reply",
|
||
"cancel",
|
||
"delete",
|
||
"reassign",
|
||
"edit",
|
||
"revoke",
|
||
"reset",
|
||
"stream",
|
||
"board_stream",
|
||
])(
|
||
"suppresses output after %s but retains its actual cost",
|
||
async (change) => {
|
||
const f = await fixture(),
|
||
row = await f.enqueue();
|
||
if (change === "reset") {
|
||
await db
|
||
.update(fastResponseRequests)
|
||
.set({ sessionGeneration: 0 })
|
||
.where(eq(fastResponseRequests.id, row.id));
|
||
row.sessionGeneration = 0;
|
||
}
|
||
f.provider.mockImplementationOnce(async () => {
|
||
if (change === "reset")
|
||
await db
|
||
.update(issues)
|
||
.set({ conversationSessionGeneration: 1 })
|
||
.where(eq(issues.id, f.issueId));
|
||
if (change === "board_stream")
|
||
await supersedeFastResponse(db, f.companyId, f.comment.id);
|
||
if (change === "stream") {
|
||
const [run] = await db
|
||
.insert(heartbeatRuns)
|
||
.values({
|
||
companyId: f.companyId,
|
||
agentId: f.agentId,
|
||
status: "running",
|
||
invocationSource: "on_demand",
|
||
contextSnapshot: {
|
||
issueId: f.issueId,
|
||
wakeCommentId: f.comment.id,
|
||
},
|
||
})
|
||
.returning();
|
||
await db.insert(heartbeatRunEvents).values({
|
||
companyId: f.companyId,
|
||
agentId: f.agentId,
|
||
runId: run.id,
|
||
seq: 1,
|
||
eventType: "item.delta",
|
||
payload: {
|
||
prpEvent: {
|
||
payload: {
|
||
kind: "agentMessage",
|
||
text: "Here is the recommendation",
|
||
},
|
||
},
|
||
},
|
||
});
|
||
}
|
||
if (change === "reply")
|
||
await db.insert(issueComments).values({
|
||
companyId: f.companyId,
|
||
issueId: f.issueId,
|
||
authorAgentId: f.agentId,
|
||
body: "I fixed the border.",
|
||
});
|
||
if (change === "cancel")
|
||
await db
|
||
.update(issues)
|
||
.set({ status: "cancelled" })
|
||
.where(eq(issues.id, f.issueId));
|
||
if (change === "delete")
|
||
await db.delete(issues).where(eq(issues.id, f.issueId));
|
||
if (change === "reassign")
|
||
await db
|
||
.update(issues)
|
||
.set({ assigneeAgentId: null })
|
||
.where(eq(issues.id, f.issueId));
|
||
if (change === "edit")
|
||
await db
|
||
.update(issueComments)
|
||
.set({ body: "Never mind", updatedAt: new Date(Date.now() + 100) })
|
||
.where(eq(issueComments.id, f.comment.id));
|
||
if (change === "revoke")
|
||
await db
|
||
.update(connectionGrants)
|
||
.set({ status: "revoked" })
|
||
.where(eq(connectionGrants.id, f.binding.grantId));
|
||
return structuredClone(outcome);
|
||
});
|
||
await f.service.process(row);
|
||
const [result] = await db
|
||
.select()
|
||
.from(fastResponseRequests)
|
||
.where(eq(fastResponseRequests.id, row.id));
|
||
expect(result).toMatchObject({
|
||
status: "succeeded",
|
||
publicationStatus: "suppressed",
|
||
commentId: null,
|
||
});
|
||
expect(
|
||
await db
|
||
.select()
|
||
.from(costEvents)
|
||
.where(eq(costEvents.companyId, f.companyId)),
|
||
).toHaveLength(1);
|
||
},
|
||
);
|
||
it("does not call a provider for expired or denied linked-user turns", async () => {
|
||
const f = await fixture(),
|
||
row = await f.enqueue();
|
||
await db.insert(connectionGrantMembers).values({
|
||
companyId: f.companyId,
|
||
grantId: f.binding.grantId,
|
||
subjectType: "user",
|
||
subjectId: "bob",
|
||
});
|
||
expect(await f.service.process(row)).toMatchObject({
|
||
status: "unavailable",
|
||
});
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
const g = await fixture(),
|
||
expired = await g.enqueue();
|
||
await g.service.process({ ...expired, expiresAt: new Date(0) });
|
||
expect(g.provider).not.toHaveBeenCalled();
|
||
});
|
||
it("never regenerates interrupted attempts and keeps unknown charges reserved", async () => {
|
||
const f = await fixture(),
|
||
row = await f.enqueue();
|
||
f.provider.mockResolvedValueOnce({
|
||
errorCode: "timeout",
|
||
receipt: fastResponseReceipt(),
|
||
});
|
||
await f.service.process(row);
|
||
await f.service.sweepPending();
|
||
expect(f.provider).toHaveBeenCalledTimes(1);
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(fastResponseRequests)
|
||
.where(eq(fastResponseRequests.id, row.id))
|
||
)[0].status,
|
||
).toBe("unknown");
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(budgetReservations)
|
||
.where(eq(budgetReservations.fastResponseRequestId, row.id))
|
||
)[0].state,
|
||
).toBe("held");
|
||
});
|
||
it("sponsors accepted unlinked senders using only destination-authorized context", async () => {
|
||
const f = await fixture();
|
||
await db
|
||
.update(issues)
|
||
.set({ title: "PRIVATE INTERNAL TITLE" })
|
||
.where(eq(issues.id, f.issueId));
|
||
const external = {
|
||
...f.source,
|
||
responsibleUserId: null,
|
||
sponsored: true,
|
||
endpointId: randomUUID(),
|
||
};
|
||
await db.transaction((tx) =>
|
||
enqueueFastResponse(tx as unknown as typeof db, external),
|
||
);
|
||
const [row] = await db
|
||
.select()
|
||
.from(fastResponseRequests)
|
||
.where(eq(fastResponseRequests.companyId, f.companyId));
|
||
const authorizeExternal = vi.fn().mockResolvedValue({
|
||
agentName: "Alex",
|
||
message: "Review this border",
|
||
queued: true,
|
||
});
|
||
const service = fastResponseService(db, {
|
||
provider: f.provider,
|
||
authorizeExternal,
|
||
});
|
||
await service.process(row);
|
||
expect(authorizeExternal).toHaveBeenCalled();
|
||
expect(f.provider).toHaveBeenCalledTimes(1);
|
||
const prompt = f.provider.mock.calls[0][0].prompt;
|
||
expect(prompt).toContain("Review this border");
|
||
expect(prompt).toContain('"state":"queued"');
|
||
expect(prompt).not.toContain("PRIVATE INTERNAL TITLE");
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(costEvents)
|
||
.where(eq(costEvents.companyId, f.companyId))
|
||
)[0],
|
||
).toMatchObject({ responsibleUserId: null, usageKind: "fast_response" });
|
||
});
|
||
it("never treats a denied linked user as company sponsored", async () => {
|
||
const f = await fixture();
|
||
await db.transaction((tx) =>
|
||
enqueueFastResponse(tx as unknown as typeof db, {
|
||
...f.source,
|
||
sponsored: true,
|
||
endpointId: randomUUID(),
|
||
}),
|
||
);
|
||
const [row] = await db
|
||
.select()
|
||
.from(fastResponseRequests)
|
||
.where(eq(fastResponseRequests.companyId, f.companyId));
|
||
await f.service.process(row);
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
});
|
||
it("expires unstarted backlog without an inference attempt", async () => {
|
||
const f = await fixture(),
|
||
row = await f.enqueue();
|
||
await db
|
||
.update(fastResponseRequests)
|
||
.set({ expiresAt: new Date(0) })
|
||
.where(eq(fastResponseRequests.id, row.id));
|
||
await f.service.sweepPending();
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(fastResponseRequests)
|
||
.where(eq(fastResponseRequests.id, row.id))
|
||
)[0],
|
||
).toMatchObject({ status: "skipped", errorCode: "expired" });
|
||
});
|
||
it("honors the company reservation budget before dispatch", async () => {
|
||
const f = await fixture();
|
||
await db
|
||
.insert(budgetPolicies)
|
||
.values({
|
||
companyId: f.companyId,
|
||
scopeType: "company",
|
||
scopeId: f.companyId,
|
||
windowKind: "calendar_month_utc",
|
||
amount: 1,
|
||
reservationCents: "2",
|
||
notifyEnabled: false,
|
||
});
|
||
const row = await f.enqueue();
|
||
expect(await f.service.process(row)).toMatchObject({
|
||
status: "unavailable",
|
||
});
|
||
expect(f.provider).not.toHaveBeenCalled();
|
||
expect(
|
||
await db
|
||
.select()
|
||
.from(costEvents)
|
||
.where(eq(costEvents.companyId, f.companyId)),
|
||
).toHaveLength(0);
|
||
});
|
||
it("releases the reservation after a known provider rejection", async () => {
|
||
const f = await fixture(),
|
||
row = await f.enqueue();
|
||
f.provider.mockResolvedValueOnce({
|
||
errorCode: "provider_auth_failed",
|
||
noProviderWork: true,
|
||
receipt: {
|
||
inputTokens: 0,
|
||
outputTokens: 0,
|
||
costCents: "0",
|
||
costStatus: "estimated",
|
||
providerRequestId: null,
|
||
pricingProvenance: { source: "unknown" },
|
||
},
|
||
});
|
||
await f.service.process(row);
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(budgetReservations)
|
||
.where(eq(budgetReservations.fastResponseRequestId, row.id))
|
||
)[0].state,
|
||
).toBe("released");
|
||
expect(
|
||
(
|
||
await db
|
||
.select()
|
||
.from(costEvents)
|
||
.where(eq(costEvents.companyId, f.companyId))
|
||
)[0].costCents,
|
||
).toBe(0);
|
||
});
|
||
it("tests the fixed sample without posting a conversation message", async () => {
|
||
const f = await fixture();
|
||
const result = await f.service.test(f.companyId, "alice");
|
||
expect(result.status).toBe("succeeded");
|
||
expect(f.provider.mock.calls[0][0].prompt).toContain("settings panel");
|
||
expect(
|
||
await db
|
||
.select()
|
||
.from(issueComments)
|
||
.where(eq(issueComments.companyId, f.companyId)),
|
||
).toHaveLength(1);
|
||
});
|
||
});
|