PEP-259: вопрос и его «Подробнее» пишутся одной транзакцией (гонка в reviewer.test)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
82d6ec0b73
commit
b106fba99f
@ -3,7 +3,9 @@
|
|||||||
// message.refusals:<s<id>|t<id>> refusals of the format in a row (core/agent-message.ts)
|
// 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 («готово»)
|
// task.result:<taskId> the agent's own words about the finished story («готово»)
|
||||||
// Details of an event (progress, blocker) live in its payload.
|
// 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 { inArray } from "drizzle-orm";
|
||||||
import { deleteSetting, getSetting, setSetting } from "./settings.ts";
|
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));
|
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>> {
|
export async function messageDetails(db: Db, ids: number[]): Promise<Map<number, string>> {
|
||||||
const uniq = [...new Set(ids)];
|
const uniq = [...new Set(ids)];
|
||||||
if (!uniq.length) return new Map();
|
if (!uniq.length) return new Map();
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import { actorColumns } from "./actor.ts";
|
|||||||
import type { Question, QuestionKind } from "./tasks.ts";
|
import type { Question, QuestionKind } from "./tasks.ts";
|
||||||
import { defaultMessageType, type MessageType } from "./questions.ts";
|
import { defaultMessageType, type MessageType } from "./questions.ts";
|
||||||
import { userProjectIds } from "./users.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 { questionStates, raiseOnce, type MessageTopic } from "./message-state.ts";
|
||||||
import { keepQuestionDetails, messageText } from "./user-text.ts";
|
import { keepQuestionDetails, messageText } from "./user-text.ts";
|
||||||
|
|
||||||
@ -205,11 +205,7 @@ export async function raiseSessionQuestion(
|
|||||||
const original = text;
|
const original = text;
|
||||||
text = messageText(kind, 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 row = await raiseOnce(db, { projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, ...o }, async () => {
|
||||||
const [created] = await db
|
return insertQuestion(db, { projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, msgType: msgType ?? defaultMessageType({ kind, options }) }, details);
|
||||||
.insert(questions)
|
|
||||||
.values({ projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, msgType: msgType ?? defaultMessageType({ kind, options }) })
|
|
||||||
.returning();
|
|
||||||
return created!;
|
|
||||||
});
|
});
|
||||||
if (details !== undefined) await setMessageDetails(db, row.id, details);
|
if (details !== undefined) await setMessageDetails(db, row.id, details);
|
||||||
await keepQuestionDetails(db, row.id, original, text).catch(() => {});
|
await keepQuestionDetails(db, row.id, original, text).catch(() => {});
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import { raiseOnce, type MessageTopic } from "./message-state.ts";
|
|||||||
import { keepQuestionDetails, messageText, stripServiceBlocks } from "./user-text.ts";
|
import { keepQuestionDetails, messageText, stripServiceBlocks } from "./user-text.ts";
|
||||||
import { defaultMessageType, type MessageType } from "./questions.ts";
|
import { defaultMessageType, type MessageType } from "./questions.ts";
|
||||||
import { renderMain, type AgentMessage } from "./agent-message.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";
|
import { touchVersion } from "./task-origin.ts";
|
||||||
|
|
||||||
export type Task = typeof tasks.$inferSelect;
|
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) };
|
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.
|
// 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 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
|
return insertQuestion(db, { projectId: ref.project.id, taskId: ref.task.id, ...q, msgType: q.msgType ?? defaultMessageType(q) }, details);
|
||||||
.insert(questions)
|
|
||||||
.values({ projectId: ref.project.id, taskId: ref.task.id, ...q, msgType: q.msgType ?? defaultMessageType(q) })
|
|
||||||
.returning();
|
|
||||||
return created!;
|
|
||||||
});
|
});
|
||||||
// The status goes right after the question: an answer that comes at once (it moves the story
|
// 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».
|
// on) must not be overwritten by a late «awaiting approval».
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user