File size: 5,938 Bytes
e516bd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
-- Version 1. Installed transactionally; never changes the transmission ledger.
CREATE TABLE IF NOT EXISTS approval_revocations (
    item_id TEXT PRIMARY KEY,
    label TEXT NOT NULL,
    revoked_at TEXT NOT NULL,
    reason TEXT NOT NULL,
    previous_accepted_at TEXT
);

-- Model classification remains the existing reviewer's responsibility. These
-- structural gates also reject missing, malformed, or withdrawn review evidence.
CREATE VIEW IF NOT EXISTS eligible_approval_items AS
SELECT i.id, i.label, i.city_id, i.design_id FROM items AS i
WHERE i.kind = 'route' AND i.status = 'ai_pass'
  AND i.geometry_ok = 1 AND i.seoul_ok = 1
  AND i.label IN ('dog','heart','tree','tshirt','candle','pants','star')
  AND CASE WHEN json_valid(i.ai_json) THEN COALESCE(
    json_type(i.ai_json) = 'object'
    AND json_extract(i.ai_json, '$.status') = 'ai_pass'
    AND json_extract(i.ai_json, '$.expected_label') = i.label
    AND json_type(i.ai_json, '$.blind') = 'true'
    AND json_type(i.ai_json, '$.qwen_performed') = 'true'
    AND json_type(i.ai_json, '$.protocol') = 'text'
    AND length(trim(json_extract(i.ai_json, '$.protocol'))) > 0
    AND json_type(i.ai_json, '$.siglip_support_angles') = 'array'
    AND json_array_length(i.ai_json, '$.siglip_support_angles') > 0
    AND json_type(i.ai_json, '$.siglip_views') = 'array'
    AND EXISTS (SELECT 1 FROM json_each(i.ai_json, '$.siglip_views') AS s
      WHERE CASE WHEN s.type = 'object'
        THEN json_extract(s.value, '$.top_label') = i.label ELSE 0 END)
    AND json_type(i.ai_json, '$.qwen') = 'array'
    AND json_array_length(i.ai_json, '$.qwen') > 0
    AND NOT EXISTS (SELECT 1 FROM json_each(i.ai_json, '$.qwen') AS q
      WHERE CASE WHEN q.type = 'object' THEN COALESCE(
        json_type(q.value, '$.valid') = 'true'
        AND json_type(q.value, '$.object') = 'text'
        AND json_type(q.value, '$.clarity') = 'integer'
        AND json_extract(q.value, '$.clarity') BETWEEN 0 AND 3, 0)
      ELSE 0 END = 0)
    AND json_type(i.ai_json, '$.evidence.valid') = 'true'
    AND json_extract(i.ai_json, '$.evidence.clarity') >= 2
    AND EXISTS (SELECT 1 FROM json_each(i.ai_json, '$.qwen') AS q
      WHERE CASE WHEN q.type = 'object' THEN
        json_extract(q.value, '$.object') = json_extract(i.ai_json, '$.evidence.object')
        AND json_extract(q.value, '$.clarity') = json_extract(i.ai_json, '$.evidence.clarity')
        ELSE 0 END),
    0) ELSE 0 END;

CREATE TRIGGER IF NOT EXISTS accepted_guard_insert
BEFORE INSERT ON accepted
WHEN NOT EXISTS (
  SELECT 1 FROM eligible_approval_items AS i
  WHERE i.id = NEW.item_id AND i.label IS NEW.label
    AND i.city_id IS NEW.city_id AND i.design_id IS NEW.design_id
    AND NOT EXISTS (SELECT 1 FROM approval_revocations WHERE item_id = NEW.item_id)
)
BEGIN
  SELECT RAISE(ABORT, 'Approval conditions invalid or explicit reapproval required');
END;

CREATE TRIGGER IF NOT EXISTS accepted_guard_update
BEFORE UPDATE ON accepted
WHEN NEW.item_id IS NOT OLD.item_id OR NOT EXISTS (
  SELECT 1 FROM eligible_approval_items AS i
  WHERE i.id = NEW.item_id AND i.label IS NEW.label
    AND i.city_id IS NEW.city_id AND i.design_id IS NEW.design_id
    AND NOT EXISTS (SELECT 1 FROM approval_revocations WHERE item_id = NEW.item_id)
)
BEGIN
  SELECT RAISE(ABORT, 'Approval conditions invalid or explicit reapproval required');
END;

CREATE TRIGGER IF NOT EXISTS accepted_record_revocation
AFTER DELETE ON accepted
BEGIN
  INSERT OR IGNORE INTO approval_revocations
    (item_id, label, revoked_at, reason, previous_accepted_at)
  VALUES (OLD.item_id, OLD.label, strftime('%Y-%m-%dT%H:%M:%fZ','now'),
    CASE WHEN NOT EXISTS (SELECT 1 FROM items WHERE id = OLD.item_id)
      THEN 'source_item_deleted'
      WHEN NOT EXISTS (SELECT 1 FROM eligible_approval_items WHERE id = OLD.item_id)
      THEN 'approval_conditions_invalid'
      ELSE 'accepted_removed' END,
    OLD.accepted_at);
END;

CREATE TRIGGER IF NOT EXISTS items_revoke_invalid_approval
AFTER UPDATE OF id, kind, status, geometry_ok, seoul_ok, ai_json, label, city_id, design_id,
  graph, record_path, points_json, source_points_json, edge_json, fingerprint ON items
BEGIN
  INSERT OR IGNORE INTO approval_revocations
    (item_id, label, revoked_at, reason, previous_accepted_at)
  SELECT a.item_id, a.label, strftime('%Y-%m-%dT%H:%M:%fZ','now'),
    CASE WHEN NEW.ai_json IS NOT OLD.ai_json
      THEN 'review_evidence_changed_requires_reapproval'
      WHEN NEW.graph IS NOT OLD.graph OR NEW.record_path IS NOT OLD.record_path
        OR NEW.points_json IS NOT OLD.points_json OR NEW.source_points_json IS NOT OLD.source_points_json
        OR NEW.edge_json IS NOT OLD.edge_json OR NEW.fingerprint IS NOT OLD.fingerprint
      THEN 'route_source_changed_requires_reapproval'
      ELSE 'approval_state_or_identity_changed' END, a.accepted_at
  FROM accepted AS a WHERE a.item_id = OLD.id AND (
    NEW.ai_json IS NOT OLD.ai_json
    OR NEW.graph IS NOT OLD.graph OR NEW.record_path IS NOT OLD.record_path
    OR NEW.points_json IS NOT OLD.points_json OR NEW.source_points_json IS NOT OLD.source_points_json
    OR NEW.edge_json IS NOT OLD.edge_json OR NEW.fingerprint IS NOT OLD.fingerprint
    OR NOT EXISTS (
      SELECT 1 FROM eligible_approval_items AS i
      WHERE i.id = a.item_id AND i.label IS a.label
        AND i.city_id IS a.city_id AND i.design_id IS a.design_id
    )
  );
  DELETE FROM accepted WHERE item_id = OLD.id
    AND EXISTS (SELECT 1 FROM approval_revocations WHERE item_id = OLD.id);
END;

CREATE TRIGGER IF NOT EXISTS revocations_exclude_accepted
AFTER INSERT ON approval_revocations
BEGIN
  DELETE FROM accepted WHERE item_id = NEW.item_id;
END;

CREATE TRIGGER IF NOT EXISTS items_revoke_deleted_approval
AFTER DELETE ON items
BEGIN
  DELETE FROM accepted WHERE item_id = OLD.id;
END;

INSERT INTO settings(key, value) VALUES ('approval_guards_version', '1')
ON CONFLICT(key) DO UPDATE SET value = excluded.value;