From b106fba99f8a4099dc78fa62efe9208a7901004d Mon Sep 17 00:00:00 2001 From: peppa-bot Date: Wed, 25 Nov 2026 14:33:58 +0000 Subject: [PATCH] =?UTF-8?q?PEP-259:=20=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81?= =?UTF-8?q?=20=D0=B8=20=D0=B5=D0=B3=D0=BE=20=C2=AB=D0=9F=D0=BE=D0=B4=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=B5=D0=B5=C2=BB=20=D0=BF=D0=B8=D1=88=D1=83?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8=D0=B5=D0=B9=20?= =?UTF-8?q?(=D0=B3=D0=BE=D0=BD=D0=BA=D0=B0=20=D0=B2=20reviewer.test)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- packages/core/src/message-details.ts | 17 ++++++++++++++++- packages/core/src/sessions.ts | 8 ++------ packages/core/src/tasks.ts | 8 ++------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/core/src/message-details.ts b/packages/core/src/message-details.ts index c5617a89..7d48cd93 100644 --- a/packages/core/src/message-details.ts +++ b/packages/core/src/message-details.ts @@ -3,7 +3,9 @@ // message.refusals:|t> refusals of the format in a row (core/agent-message.ts) // task.result: the agent's own words about the finished story («готово») // Details of an event (progress, blocker) live in its payload. -import { settings, type Db } from "@peppa/db"; +import { questions, settings, type Db } from "@peppa/db"; + +type Question = typeof questions.$inferSelect; import { inArray } from "drizzle-orm"; import { deleteSetting, getSetting, setSetting } from "./settings.ts"; @@ -16,6 +18,19 @@ export async function setMessageDetails(db: Db, questionId: number, details?: st else await deleteSetting(db, qKey(questionId)); } +/** + * Inserts a `questions` row together with its details in one transaction: a reader (the bot, + * a page, a test) never sees the new message without its «Подробнее». + */ +export function insertQuestion(db: Db, values: typeof questions.$inferInsert, details?: string | null): Question { + const d = (details ?? "").trim().slice(0, DETAILS_MAX); + return db.transaction((tx) => { + const row = tx.insert(questions).values(values).returning().get() as Question; + if (d) tx.insert(settings).values({ key: qKey(row.id), value: d }).onConflictDoUpdate({ target: settings.key, set: { value: d, updatedAt: new Date() } }).run(); + return row; + }); +} + export async function messageDetails(db: Db, ids: number[]): Promise> { const uniq = [...new Set(ids)]; if (!uniq.length) return new Map(); diff --git a/packages/core/src/sessions.ts b/packages/core/src/sessions.ts index f6ec095c..275d0070 100644 --- a/packages/core/src/sessions.ts +++ b/packages/core/src/sessions.ts @@ -7,7 +7,7 @@ import { actorColumns } from "./actor.ts"; import type { Question, QuestionKind } from "./tasks.ts"; import { defaultMessageType, type MessageType } from "./questions.ts"; import { userProjectIds } from "./users.ts"; -import { setMessageDetails } from "./message-details.ts"; +import { insertQuestion, setMessageDetails } from "./message-details.ts"; import { questionStates, raiseOnce, type MessageTopic } from "./message-state.ts"; import { keepQuestionDetails, messageText } from "./user-text.ts"; @@ -205,11 +205,7 @@ export async function raiseSessionQuestion( const original = text; text = messageText(kind, text); const row = await raiseOnce(db, { projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, ...o }, async () => { - const [created] = await db - .insert(questions) - .values({ projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, msgType: msgType ?? defaultMessageType({ kind, options }) }) - .returning(); - return created!; + return insertQuestion(db, { projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, msgType: msgType ?? defaultMessageType({ kind, options }) }, details); }); if (details !== undefined) await setMessageDetails(db, row.id, details); await keepQuestionDetails(db, row.id, original, text).catch(() => {}); diff --git a/packages/core/src/tasks.ts b/packages/core/src/tasks.ts index 1a920c24..3387aceb 100644 --- a/packages/core/src/tasks.ts +++ b/packages/core/src/tasks.ts @@ -19,7 +19,7 @@ import { raiseOnce, type MessageTopic } from "./message-state.ts"; import { keepQuestionDetails, messageText, stripServiceBlocks } from "./user-text.ts"; import { defaultMessageType, type MessageType } from "./questions.ts"; import { renderMain, type AgentMessage } from "./agent-message.ts"; -import { setMessageDetails } from "./message-details.ts"; +import { insertQuestion, setMessageDetails } from "./message-details.ts"; import { touchVersion } from "./task-origin.ts"; export type Task = typeof tasks.$inferSelect; @@ -348,11 +348,7 @@ export async function askPm(db: Db, ref: TaskRef, { topic, head, details, ...q } q = { ...q, text: messageText(q.kind, q.text) }; // PEP-151: the same state of the story again refreshes the earlier message instead of adding one. const row = await raiseOnce(db, { projectId: ref.project.id, taskId: ref.task.id, sessionId: q.sessionId ?? null, kind: q.kind, text: q.text, options: q.options, topic, head }, async () => { - const [created] = await db - .insert(questions) - .values({ projectId: ref.project.id, taskId: ref.task.id, ...q, msgType: q.msgType ?? defaultMessageType(q) }) - .returning(); - return created!; + return insertQuestion(db, { projectId: ref.project.id, taskId: ref.task.id, ...q, msgType: q.msgType ?? defaultMessageType(q) }, details); }); // The status goes right after the question: an answer that comes at once (it moves the story // on) must not be overwritten by a late «awaiting approval».