mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 18:07:00 +01:00
fix: preserve voice reply ordering and reject unavailable work
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
a7002737b1
commit
04a69ea6a8
2 files changed
+20
-3
No files matched your search
@@ -83,6 +83,14 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
await expect(restarted.runTool({ ...request, fingerprint: "changed" })).rejects.toMatchObject({ status: 409 });
|
||||
});
|
||||
|
||||
it("rejects Backlog voice work before claiming that it was accepted", async () => {
|
||||
const f = await fixture();
|
||||
await db.update(issues).set({status: "backlog"}).where(eq(issues.id, f.issueId));
|
||||
await expect(f.store.runTool(f.toolInput("submit_request", {text: "Inspect disk space"}))).rejects.toMatchObject({status: 409, message: "Move this task to Todo in Paperclip before requesting voice work."});
|
||||
expect(await db.select().from(chatDeliveries).where(eq(chatDeliveries.endpointId, f.endpointId))).toHaveLength(0);
|
||||
expect(await db.select().from(chatVoiceToolCalls).where(eq(chatVoiceToolCalls.sessionId, f.sessionId))).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("delivers urgent live-call guidance only after accepted work and retires it after hangup or revocation", async () => {
|
||||
const f = await fixture(), scope = { companyId: f.companyId, issueId: f.issueId, agentId: f.agentId };
|
||||
expect(await liveVoiceExecutionGuidance(db, scope)).toBeNull();
|
||||
@@ -504,6 +512,8 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
it("sends long approved phone answers as durable ordered parts and resumes after a rate limit", async () => {
|
||||
const f = await pushFixture(), text = "😀 answer ".repeat(4000) + "FINAL";
|
||||
await db.update(chatPublications).set({payload: {text}}).where(eq(chatPublications.id, f.publication.id));
|
||||
const [later] = await db.insert(chatPublications).values({companyId: f.companyId, endpointId: f.endpointId, conversationId: f.conversationId, issueId: f.issueId, idempotencyKey: randomUUID(), state: "published", payload: {text: "Later answer B"}}).returning();
|
||||
await f.store.enqueuePublication(f.companyId, later.id);
|
||||
f.transport.sendCallMessage.mockResolvedValueOnce({messageId: "part-0"}).mockRejectedValueOnce(new SpekoProviderError("provider_unavailable", false, 429));
|
||||
await f.push();
|
||||
const [reply] = await db.select().from(chatVoiceReplies).where(eq(chatVoiceReplies.publicationId, f.publication.id));
|
||||
@@ -521,7 +531,7 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
await voiceSessionService(db, {allowLocalBoard: false, credentials: f.credentials, provider: () => f.transport, onQuestionAnswered: vi.fn()}).pushReplies(25, f.companyId, f.endpointId);
|
||||
await f.push();
|
||||
const calls = f.transport.sendCallMessage.mock.calls;
|
||||
expect(calls).toHaveLength(4); expect(calls[1]?.[1]).toBe(calls[2]?.[1]);
|
||||
expect(calls).toHaveLength(5); expect(calls[4]?.[1]).toBe("Later answer B"); expect(calls[1]?.[1]).toBe(calls[2]?.[1]);
|
||||
expect([calls[0]![1], calls[2]![1], calls[3]![1]].join("")).toBe(text);
|
||||
for (const call of calls) {expect(call[1].length).toBeLessThanOrEqual(16_000); expect(/^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/.test(call[1])).toBe(false);}
|
||||
const [delivered] = await db.select().from(chatVoiceReplies).where(eq(chatVoiceReplies.id, reply.id));
|
||||
@@ -530,6 +540,8 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
it("does not retry an uncertain long-answer part or send its suffix", async () => {
|
||||
const f = await pushFixture();
|
||||
await db.update(chatPublications).set({payload: {text: "answer ".repeat(5000)}}).where(eq(chatPublications.id, f.publication.id));
|
||||
const [later] = await db.insert(chatPublications).values({companyId: f.companyId, endpointId: f.endpointId, conversationId: f.conversationId, issueId: f.issueId, idempotencyKey: randomUUID(), state: "published", payload: {text: "Later answer B"}}).returning();
|
||||
await f.store.enqueuePublication(f.companyId, later.id);
|
||||
f.transport.sendCallMessage.mockResolvedValueOnce({messageId: "part-0"}).mockRejectedValueOnce(new SpekoProviderError("provider_unavailable", true));
|
||||
await f.push(); await f.push();
|
||||
expect(f.transport.sendCallMessage).toHaveBeenCalledTimes(2);
|
||||
|
||||
@@ -317,6 +317,7 @@ export function voiceSessionStore(db: Db, options: { allowLocalBoard: boolean; n
|
||||
if (replay) throw conflict("Voice webhook id was already used for a different tool call");
|
||||
let deliveryId: string | null = null, response: Record<string, unknown>;
|
||||
if (input.envelope.tool === "submit_request") {
|
||||
if (context.task.status === "backlog") throw conflict("Move this task to Todo in Paperclip before requesting voice work.");
|
||||
if (session.callerAuthority === "guest_intake") {
|
||||
const requests = await tx.select({id: chatVoiceToolCalls.id}).from(chatVoiceToolCalls).where(and(eq(chatVoiceToolCalls.sessionId, session.id), eq(chatVoiceToolCalls.tool, "submit_request"))).limit(20);
|
||||
if (requests.length >= 20) throw conflict("This call has reached its message limit. Continue in Paperclip.");
|
||||
@@ -347,9 +348,13 @@ export function voiceSessionStore(db: Db, options: { allowLocalBoard: boolean; n
|
||||
// The durable delivered marker is authoritative. A later publication
|
||||
// can finish before an earlier stream; a caller cursor must not hide
|
||||
// that older, still-unclaimed answer when it eventually publishes.
|
||||
const available = await tx.select({ reply: chatVoiceReplies, payload: chatPublications.payload }).from(chatVoiceReplies).innerJoin(chatPublications, and(eq(chatPublications.id, chatVoiceReplies.publicationId), eq(chatPublications.companyId, session.companyId)))
|
||||
.where(and(eq(chatVoiceReplies.sessionId, session.id), eq(chatVoiceReplies.companyId, session.companyId), repeat ? sql`${chatVoiceReplies.deliveredAt} is not null` : and(isNull(chatVoiceReplies.deliveredAt), sql`not exists (select 1 from chat_actions a where a.company_id = ${session.companyId} and a.endpoint_id = ${session.endpointId} and a.kind = 'speko_voice_reply_push' and a.payload->>'replyId' = ${chatVoiceReplies.id}::text and (a.status in ('dispatching', 'unknown') or (a.status = 'completed' and a.payload->>'questionNotification' = 'false')))`), eq(chatPublications.endpointId, session.endpointId), eq(chatPublications.conversationId, session.conversationId), eq(chatPublications.issueId, session.issueId), eq(chatPublications.state, "published")))
|
||||
const available = await tx.select({ reply: chatVoiceReplies, payload: chatPublications.payload, pushBlocked: sql<boolean>`exists (select 1 from chat_actions a where a.company_id = ${session.companyId} and a.endpoint_id = ${session.endpointId} and a.kind = 'speko_voice_reply_push' and a.payload->>'replyId' = ${chatVoiceReplies.id}::text and (a.status in ('dispatching', 'unknown') or (a.status = 'completed' and a.payload->>'questionNotification' = 'false')))` }).from(chatVoiceReplies).innerJoin(chatPublications, and(eq(chatPublications.id, chatVoiceReplies.publicationId), eq(chatPublications.companyId, session.companyId)))
|
||||
.where(and(eq(chatVoiceReplies.sessionId, session.id), eq(chatVoiceReplies.companyId, session.companyId), repeat ? sql`${chatVoiceReplies.deliveredAt} is not null` : isNull(chatVoiceReplies.deliveredAt), eq(chatPublications.endpointId, session.endpointId), eq(chatPublications.conversationId, session.conversationId), eq(chatPublications.issueId, session.issueId), eq(chatPublications.state, "published")))
|
||||
.orderBy(repeat ? desc(chatVoiceReplies.deliveredAt) : asc(chatVoiceReplies.cursor), desc(chatVoiceReplies.cursor)).limit(repeat ? 1 : 8);
|
||||
// Never skip a partly pushed answer to retrieve later replies. The
|
||||
// accepted prefix cannot be replayed, and its suffix remains ordered.
|
||||
const pushBoundary = available.findIndex(row => row.pushBlocked);
|
||||
if (!repeat && pushBoundary >= 0) available.splice(pushBoundary);
|
||||
// Deliver a bounded burst in one voice turn. Separate typed hints would
|
||||
// interrupt the preceding answer. Stop at a question so later answers
|
||||
// cannot displace the clarification the caller must respond to.
|
||||
|
||||
Reference in new issue
Block a user