fix: keep rectification questions context-aware

This commit is contained in:
Jesse_Chen
2026-07-30 07:58:06 +08:00
parent eb303c7089
commit e9d20382d9
12 changed files with 322 additions and 71 deletions
@@ -26,16 +26,19 @@ export function createRectificationV4CaseService(
const realizeQuestion = options.regenerateQuestion ?? regenerateQuestionRealization;
async function response(userId: string, caseValue: RectificationV4Case, jobId?: string): Promise<RectificationV4ApiResponse> {
const [events, turns, analysis] = await Promise.all([
const [events, turns, analysis, job] = await Promise.all([
store.loadEvents(userId, caseValue.id),
store.loadTurns(userId, caseValue.id),
caseValue.deploymentMode === "v5_agent"
? store.loadAnalysisMessages(userId, caseValue.id)
: Promise.resolve([]),
jobId
? store.loadJob(userId, jobId)
: caseValue.status === "processing" ? store.loadActiveJob(userId, caseValue.id) : null,
]);
return {
case: caseValue,
job: jobId ? await store.loadJob(userId, jobId) : null,
job,
events: [...events],
turns: [...turns],
analysis: [...analysis],
@@ -230,6 +230,12 @@ export function createRectificationV4MemoryStore(): RectificationV4Store & {
owned(userId, job.caseId);
return job;
},
async loadActiveJob(userId, caseId) {
owned(userId, caseId);
return [...jobs.values()]
.filter((job) => job.caseId === caseId && ["pending", "processing"].includes(job.status))
.sort((left, right) => right.createdAt.localeCompare(left.createdAt))[0] ?? null;
},
async updateJobPhase(input) {
const job = jobs.get(input.jobId);
if (!job || job.workerId !== input.workerId || job.status !== "processing") throw new RectificationV4StoreError("lease_lost");
@@ -90,6 +90,7 @@ export interface RectificationV4Store {
readonly now: string;
}): Promise<RectificationV4Case>;
loadJob(userId: string, jobId: string): Promise<RectificationV4Job | null>;
loadActiveJob(userId: string, caseId: string): Promise<RectificationV4Job | null>;
updateJobPhase(input: { readonly workerId: string; readonly jobId: string; readonly phase: RectificationV4Phase; readonly now: string }): Promise<void>;
claimNextJob(workerId: string, now: string): Promise<ClaimedRectificationV4Job | null>;
completeJob(input: CompleteRectificationV4JobInput, now: string): Promise<RectificationV4Case>;
@@ -345,6 +345,14 @@ export function createRectificationV4SupabaseStore(supabase: SupabaseClient): Re
if (!row || row.user_id !== userId) return null;
return jobValue(row);
},
async loadActiveJob(userId, caseId) {
const { data, error } = await supabase.from("birth_time_rectification_v4_jobs")
.select("*").eq("user_id", userId).eq("case_id", caseId)
.in("status", ["pending", "processing"])
.order("created_at", { ascending: false }).limit(1).maybeSingle();
if (error) throw storeError(error);
return data ? jobValue(data as Row) : null;
},
async updateJobPhase(input) {
await rpc("update_birth_time_rectification_v4_job_phase", {
p_worker_id: input.workerId,