A chat-wake reply is delivered by the system, not by prompt compliance

A wake run whose model answered in plain final text (the coding-tuned
Kimi family does this instead of calling send_message) completed
'successfully', closed its vehicle task, and left the founder staring
at a silent conversation — the reply lived only in provider_requests.
deliverChatReplyIfMissing now runs before a chat-wake run may complete:
when the run never posted into the source thread (run-start watermark,
so a send_message it did make suppresses the fallback), its final text
is delivered as the agent's reply. A delivery failure fails the run
loudly instead of closing it on silence.
This commit is contained in:
ZacharyZhang-NY committed 2026-07-16 11:46:53 -04:00
1 parent 75c73ce6f0
commit 7de378b41c
4 files changed
+161

No files matched your search

@@ -4,6 +4,7 @@ import { RISK_LEVEL, TASK_PRIORITY, TASK_STATUS } from "../../shared/domain";
import { createId } from "../../shared/ids";
import type { ResolvedMention } from "./chat-mentions";
import type { SlashInvocation } from "./chat-service-internal";
import type { ChatService } from "./chat-service";
import { OrganizationService } from "./organization-service";
interface RunnableAgentRow {
@@ -63,6 +64,51 @@ export function expireStaleChatWakes(db: DatabaseSync, now: string): number {
return stale.length;
}
/**
* A chat wake's deliverable is a reply VISIBLE in the source thread. The model
* is asked to post it with send_message, but a run that answers in plain final
* text has still produced the reply — delivery is the system's contract, not
* the model's prompt compliance. When the run never posted into the thread
* (checked by the run's start watermark, so any send_message it did make
* counts), its final text is delivered as the agent's message; otherwise the
* asker watches the run "complete" while the conversation stays silent.
* Returns true when a fallback message was posted. A delivery failure throws,
* so the run fails loudly instead of completing with nothing said.
*/
export function deliverChatReplyIfMissing(
db: DatabaseSync,
chat: ChatService,
input: {
orgId: string;
agentId: string;
spaceId: string;
threadId: string;
finalText: string;
/** ISO timestamp captured before the run's first provider turn. */
runStartedAt: string;
},
): boolean {
const body = input.finalText.trim();
if (!body) return false;
const posted = db.prepare(`
select 1 from chat_messages
where org_id = ? and thread_id = ? and sender_type = 'agent' and sender_id = ? and created_at >= ?
limit 1
`).get(input.orgId, input.threadId, input.agentId, input.runStartedAt);
if (posted) return false;
console.info(`[chat] the reply run never posted into thread ${input.threadId}; delivering its final text as the reply.`);
chat.sendMessage({
orgId: input.orgId,
spaceId: input.spaceId,
threadId: input.threadId,
senderType: "agent",
senderId: input.agentId,
bodyMd: body,
attachmentFileIds: [],
});
return true;
}
export class ChatAgentWakeService {
private readonly organization: OrganizationService;