fix(rectification): preserve assistant turns in dossier
Staging Backend Quality Gate / validate (pull_request) Successful in 17m28s
Staging Backend Quality Gate / publish (pull_request) Has been skipped

This commit is contained in:
Jesse_Chen
2026-08-12 15:33:04 +08:00
parent 4152103726
commit 746fdc184b
3 changed files with 198 additions and 0 deletions
@@ -0,0 +1,133 @@
-- Preserve both logical messages stored by a completed rectification turn.
-- A normal turn stores user_message and assistant_message on the same physical
-- row; the dossier must expose both in conversational order. Business schema
-- only: do not copy this migration into frontend/db/migrations.
begin;
create or replace function public.get_agentic_rectification_case_dossier(
p_user_id uuid,
p_case_id uuid
)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_case public.agentic_rectification_cases%rowtype;
v_turns jsonb;
v_evidence jsonb;
v_result public.agentic_rectification_results%rowtype;
v_evidence_count bigint;
v_turn_count bigint;
begin
if p_user_id is null or p_case_id is null then
raise exception 'agentic_rectification_invalid_input' using errcode = 'P0001';
end if;
select * into v_case
from public.agentic_rectification_cases
where id = p_case_id and user_id = p_user_id;
if not found then
raise exception 'agentic_rectification_case_not_found' using errcode = 'P0001';
end if;
select coalesce(jsonb_agg(
jsonb_build_object(
'id', t.id,
'role', message.role,
'text', message.text,
'status', t.status,
'created_at', t.created_at,
'completed_at', t.completed_at
) order by t.created_at, message.ordinal
), '[]'::jsonb) into v_turns
from public.agentic_rectification_turns t
cross join lateral (
values
(1, 'user'::text, t.user_message),
(2, 'assistant'::text, t.assistant_message)
) as message(ordinal, role, text)
where t.case_id = v_case.id
and message.text is not null;
select coalesce(jsonb_agg(
jsonb_build_object(
'id', e.id,
'source_turn_id', e.source_turn_id,
'subject', e.subject,
'event_kind', e.event_kind,
'domain', e.domain,
'occurred_from', e.occurred_from,
'occurred_to', e.occurred_to,
'date_precision', e.date_precision,
'summary', e.summary,
'status', e.status,
'supersedes_evidence_id', e.supersedes_evidence_id,
'created_at', e.created_at
) order by e.created_at
), '[]'::jsonb) into v_evidence
from public.agentic_rectification_evidence e
where e.case_id = v_case.id;
select count(*) into v_evidence_count
from public.agentic_rectification_evidence
where case_id = v_case.id;
select count(*) into v_turn_count
from public.agentic_rectification_turns
where case_id = v_case.id;
select * into v_result
from public.agentic_rectification_results
where case_id = v_case.id
and invalidated_at is null
order by created_at desc
limit 1;
return jsonb_build_object(
'case', jsonb_build_object(
'case_id', v_case.id,
'session_id', v_case.session_id,
'status', v_case.status,
'skill_name', v_case.skill_name,
'skill_version', v_case.skill_version,
'candidate_range', v_case.candidate_range,
'accepted_time', v_case.accepted_time,
'confirmed_time', v_case.confirmed_time,
'completed_at', v_case.completed_at,
'closed_reason', v_case.closed_reason,
'last_activity_at', v_case.last_activity_at,
'evidence_count', v_evidence_count,
'turn_count', v_turn_count
),
'turns', v_turns,
'evidence', v_evidence,
'latest_result', case
when v_result.id is null then null
else jsonb_build_object(
'result_id', v_result.id,
'candidates', v_result.candidates,
'overall_confidence', v_result.overall_confidence,
'selection_allowed', v_result.selection_allowed,
'confirmation_allowed', v_result.confirmation_allowed,
'representative_time', v_result.representative_time,
'selected_time', v_result.selected_time,
'selection_kind', v_result.selection_kind,
'evidence_ledger_fingerprint', v_result.evidence_ledger_fingerprint,
'candidate_range_fingerprint', v_result.candidate_range_fingerprint,
'skill_version', v_result.skill_version,
'algorithm_version', v_result.algorithm_version,
'created_at', v_result.created_at,
'invalidated_at', v_result.invalidated_at
)
end
);
end;
$$;
revoke all on function public.get_agentic_rectification_case_dossier(uuid, uuid)
from public, anon, authenticated;
grant execute on function public.get_agentic_rectification_case_dossier(uuid, uuid)
to service_role;
commit;
@@ -399,3 +399,53 @@ test("birth-context activity migration stays out of the identity migration tree"
"business migration must not be copied into frontend/db/migrations (BUG-127/BUG-144)",
);
});
// ---------------------------------------------------------------------------
// 20260813030000_rectification_turn_message_projection.sql
// ---------------------------------------------------------------------------
const turnMessageProjectionMigration = readFileSync(
new URL(
"../supabase/migrations/20260813030000_rectification_turn_message_projection.sql",
import.meta.url,
),
"utf8",
);
const turnMessageProjectionMigrationCopy = fileURLToPath(
new URL(
"../db/migrations/20260813030000_rectification_turn_message_projection.sql",
import.meta.url,
),
);
test("rectification turn projection migration follows the V9 activity migration", () => {
assert.ok(
"20260813030000_rectification_turn_message_projection.sql" >
"20260813020000_rectification_birth_context_activity.sql",
);
assert.match(turnMessageProjectionMigration, /^-- Preserve both logical messages[\s\S]*\nbegin;[\s\S]*^commit;$/m);
});
test("rectification dossier expands each physical turn into user and assistant messages", () => {
assert.match(
turnMessageProjectionMigration,
/cross join lateral \(\s*values\s*\(1, 'user'::text, t\.user_message\),\s*\(2, 'assistant'::text, t\.assistant_message\)\s*\) as message\(ordinal, role, text\)/,
);
assert.match(turnMessageProjectionMigration, /'role', message\.role/);
assert.match(turnMessageProjectionMigration, /'text', message\.text/);
assert.match(turnMessageProjectionMigration, /order by t\.created_at, message\.ordinal/);
assert.match(turnMessageProjectionMigration, /and message\.text is not null/);
assert.doesNotMatch(
turnMessageProjectionMigration,
/coalesce\(t\.user_message, t\.assistant_message\)/,
);
});
test("rectification turn projection migration stays out of the identity migration tree", () => {
assert.equal(
existsSync(turnMessageProjectionMigrationCopy),
false,
"business migration must not be copied into frontend/db/migrations (BUG-127/BUG-144)",
);
});