-- Three coupled changes for agent self-correction safety:
--
-- 1. `entity_facts.last_modified_by_*` + `last_modified_at` track who
--    most recently mutated each fact. `created_by_*` from migration
--    2026-05-10-000300 records who first wrote the row; this records
--    who last *changed* it. Separate columns so the create vs update
--    audit is independently grep-able ("show me every fact gpt-5
--    altered last week" stays a single index scan).
--
-- 2. `personas.allow_agent_corrections` is the gate for the new
--    agent-side `update_fact` / `supersede_fact` tools. Default OFF —
--    a fresh persona's agent can create but can't alter or replace.
--    Operator opts in per-persona after the model has earned trust,
--    typically via the strict-mode flow (curate, then ratchet up
--    agent autonomy as confidence rises). Parallel in shape to
--    `reviewed_only_facts` from 2026-05-10-000400; they compose.
--
-- 3. Index on `last_modified_at` (partial, NOT NULL) for the
--    audit-feed reads in the curation UI ("show recent agent edits
--    sorted newest first").

ALTER TABLE entity_facts ADD COLUMN last_modified_by_model TEXT;
ALTER TABLE entity_facts ADD COLUMN last_modified_by_backend TEXT;
ALTER TABLE entity_facts ADD COLUMN last_modified_at BIGINT;

CREATE INDEX idx_entity_facts_last_modified_at
    ON entity_facts(last_modified_at)
    WHERE last_modified_at IS NOT NULL;

ALTER TABLE personas ADD COLUMN allow_agent_corrections BOOLEAN NOT NULL DEFAULT 0;
