PEP-259: вопрос и его «Подробнее» пишутся одной транзакцией (гонка в reviewer.test)
All checks were successful
peppa/lint lint: success за 2с
peppa/typecheck typecheck: success за 49с
peppa/build build: success за 36с
peppa/test test: success за 1552с
peppa-ci / ci (pull_request) Successful in 28m30s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
peppa-bot 2026-11-25 14:33:58 +00:00
parent 82d6ec0b73
commit b106fba99f
3 changed files with 20 additions and 13 deletions

View File

@ -3,7 +3,9 @@
// message.refusals:<s<id>|t<id>> refusals of the format in a row (core/agent-message.ts)
// task.result:<taskId> 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<Map<number, string>> {
const uniq = [...new Set(ids)];
if (!uniq.length) return new Map();

View File

@ -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(() => {});

View File

@ -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».