File size: 12,803 Bytes
d09a296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4946331
 
 
 
 
 
d09a296
 
 
b9b5fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
d09a296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4946331
d09a296
4946331
 
 
 
 
d09a296
 
 
 
 
 
4946331
 
d09a296
 
 
 
 
 
 
 
 
 
 
4946331
 
d09a296
 
b9b5fd9
 
 
 
 
4946331
d09a296
 
 
 
b9b5fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d09a296
4946331
d09a296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebbcf47
d09a296
 
 
 
4946331
d09a296
ebbcf47
d09a296
 
ebbcf47
 
 
 
 
 
 
4946331
d09a296
 
4946331
 
d09a296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1a6941
 
 
 
4946331
e1a6941
 
 
 
 
 
4946331
e1a6941
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""Use the existing Supabase schema through each staff user's JWT and RLS."""
import json
import logging
from pathlib import Path
import re
import threading
import time
from urllib.parse import quote
import httpx

log=logging.getLogger('defectrag.cloud')
_staff_cache={}
_cache_lock=threading.Lock()


def read_public_config(path):
    text=Path(path).read_text(encoding='utf8')
    output={}
    for source,key in (('supabaseUrl','url'),('supabasePublishableKey','key')):
        match=re.search(r'''["']?'''+source+r'''["']?\s*:\s*["']([^"']*)''',text)
        output[key]=match.group(1) if match else ''
    if not output['url'].startswith('https://') or not output['key'].startswith('sb_publishable_'):
        raise ValueError('Supabase public settings are not configured')
    return output


class StaffRepository:
    table='shared_inspections'
    bucket='team-inspections'
    notes_table='staff_notes'
    storage_type='supabase'
    workspace='team'
    workspace_stats_enabled=False
    def __init__(self,config,token,artifacts):
        self.config,self.token,self.artifacts=config,token,Path(artifacts)
        self.headers={'apikey':config['key'],'Authorization':'Bearer '+token}
        if config.get('auth_workflow_enabled'):
            # Supabase verifies the JWT. No decoded/browser AAL or role is trusted.
            # Deliberately fresh for each protected operation: revocation must
            # not reuse the legacy 45-second membership cache before GPU work.
            posture=self.request('POST','/rest/v1/rpc/defectrag_auth_status',json={})
            if (not isinstance(posture,dict) or posture.get('approved') is not True
                    or posture.get('revoked') is not False or posture.get('authorized') is not True
                    or type(posture.get('mfa_required')) is not bool
                    or (posture['mfa_required'] and posture.get('aal2') is not True)
                    or posture.get('role') not in ('staff','developer') or not posture.get('user_id')):
                raise ValueError('Approved staff and required authenticator verification are needed. Open Staff workspace.')
            self.user={'id':posture['user_id']}
            self.staff={'user_id':posture['user_id'],'role':posture['role'],'display_name':posture.get('display_name') or 'Staff member'}
            return
        # Do not trust decoded JWT claims. Supabase validates the token, and the
        # staff allowlist is read through its existing authenticated policies.
        with _cache_lock:
            cached=_staff_cache.get(token)
        if cached and cached[0]>time.time():
            self.user,self.staff=cached[1:]
        else:
            self.user=self.request('GET','/auth/v1/user')
            rows=self.request('GET','/rest/v1/staff_members',params={'user_id':'eq.'+self.user['id'],'select':'user_id,display_name,role','limit':'1'})
            if not rows:
                raise ValueError('Your account is not on the staff allowlist')
            self.staff=rows[0]
            with _cache_lock:
                if len(_staff_cache)>=256:
                    _staff_cache.clear()
                _staff_cache[token]=(time.time()+45,self.user,self.staff)

    def request(self,method,path,**kwargs):
        ignore_conflict=kwargs.pop('ignore_conflict',False)
        headers={**self.headers,**kwargs.pop('headers',{})}
        try:
            with httpx.Client(timeout=30,trust_env=False) as client:
                response=client.request(method,self.config['url']+path,headers=headers,**kwargs)
        except httpx.HTTPError:
            raise RuntimeError('Cloud request did not complete. Retry the same submission to verify whether it was saved.') from None
        if not response.is_success:
            try:
                detail=response.json()
                code=detail.get('code',detail.get('error','unknown'))
            except (ValueError,AttributeError):
                code='unexpected_response'
            if ignore_conflict and response.status_code in (400,409) and code in ('23505','Duplicate','ResourceAlreadyExists','KeyAlreadyExists'):
                return None
            if response.status_code in (401,403):
                raise ValueError('Staff session expired or access denied. Sign in again.')
            if code in ('PGRST202','PGRST205'):
                raise ValueError('Required Supabase function/table is unavailable. Apply the reviewed additive migration.')
            raise RuntimeError(f'Supabase request failed ({response.status_code}, {code}); no durable success was reported.')
        return response.json() if response.content else None

    def decorate(self,row):
        if not row:
            return None
        record=dict(row['payload'])
        record['storage']=self.storage_type
        record['workspace']=self.workspace
        record['owner_id']=row['owner_id']
        for kind in ('image','heatmap'):
            if self.config.get('auth_workflow_enabled'):
                # SessionRepository replaces these with scoped artifact URLs;
                # the backend fetches private bytes under the current user JWT.
                record[kind+'_url']=f"/api/inspections/{record['id']}/{kind}"
                continue
            result=self.request('POST','/storage/v1/object/sign/'+self.bucket+'/'+quote(row[kind+'_path'],safe='/'),json={'expiresIn':600})
            signed=result.get('signedURL',result.get('signedUrl'))
            record[kind+'_url']=signed if signed.startswith('https://') else self.config['url']+'/storage/v1'+signed
        return record

    def private_artifact(self,identifier,kind):
        if kind not in ('image','heatmap'):raise ValueError('Invalid private artifact')
        row=self.raw(identifier)
        if row is None:raise ValueError('Private inspection is unavailable')
        try:
            with httpx.Client(timeout=25,trust_env=False) as client:
                with client.stream('GET',self.config['url']+'/storage/v1/object/authenticated/'+self.bucket+'/'+quote(row[kind+'_path'],safe='/'),headers=self.headers) as response:
                    if not response.is_success:raise ValueError('Private artifact access denied')
                    content=bytearray()
                    for chunk in response.iter_bytes(chunk_size=65536):
                        if len(content)+len(chunk)>10*1024*1024:raise ValueError('Private artifact exceeds the size limit')
                        content.extend(chunk)
                    if not content:raise ValueError('Private artifact is empty')
                    return bytes(content)
        except httpx.HTTPError:
            raise RuntimeError('Private artifact could not be loaded. Retry later.') from None

    def raw(self,identifier,archived=False):
        rows=self.request('GET','/rest/v1/'+self.table,params={'id':'eq.'+identifier,'select':'*','limit':'1',
            'archived_at':'not.is.null' if archived else 'is.null'})
        return rows[0] if rows else None

    def save(self,record):
        paths={}
        for kind,suffix in (('image',''),('heatmap','_heatmap')):
            source=self.artifacts/f"{record['id']}{suffix}.jpg"
            destination=f"{self.user['id']}/{record['id']}/{kind}.jpg"
            self.request('POST','/storage/v1/object/team-inspections/'+destination,
                content=source.read_bytes(),headers={'Content-Type':'image/jpeg','x-upsert':'false'})
            paths[kind+'_path']=destination
        payload={key:value for key,value in record.items() if not key.endswith('_url')}
        row={'id':record['id'],'owner_id':self.user['id'],'created_at':record['created_at'],
             'payload':payload,'conversation':[],**paths}
        self.request('POST','/rest/v1/shared_inspections',json=row,headers={'Prefer':'return=minimal'})
        return self.decorate(row)

    def get(self,identifier):
        return self.decorate(self.raw(identifier))

    def raw_list(self,category='',verdict='',batch='',limit=100,offset=0,archived=False):
        params={'select':'id,owner_id,created_at,payload,image_path,heatmap_path,archived_at','archived_at':'not.is.null' if archived else 'is.null',
                'order':'created_at.desc','limit':str(limit),'offset':str(offset)}
        for key,value in (('category',category),('verdict',verdict),('batch',batch)):
            if value:
                params['payload->>'+key]='eq.'+value
        return self.request('GET','/rest/v1/'+self.table,params=params)

    def list(self,category='',verdict='',batch='',limit=100,offset=0,archived=False):
        # Lists need no images until a record is opened. Avoid two storage API
        # calls per row for large history pages.
        rows=[]
        while len(rows)<limit:
            count=min(1000,limit-len(rows))
            page=self.raw_list(category,verdict,batch,count,offset+len(rows),archived)
            rows.extend(page)
            if len(page)<count:
                break
        return [{**row['payload'],'storage':self.storage_type,'workspace':self.workspace,'owner_id':row['owner_id']} for row in rows]

    def stats(self):
        if self.workspace_stats_enabled:
            return self.request('POST','/rest/v1/rpc/defectrag_workspace_stats',json={'p_scope':self.workspace})
        rows=self.list(limit=1000)
        counts={kind:sum(row['verdict']==kind for row in rows) for kind in ('Good','Anomalous','Uncertain')}
        batches={}
        for row in rows:
            item=batches.setdefault(row.get('batch',''),{'batch':row.get('batch',''),'total':0,'anomalous':0,'uncertain':0})
            item['total']+=1
            item['anomalous']+=row['verdict']=='Anomalous'
            item['uncertain']+=row['verdict']=='Uncertain'
        return {'total':len(rows),'counts':counts,'batches':list(batches.values()),'failed_requests':0,
                'scope':'latest 1000 active staff records; model failures are in server logs'}

    def messages(self,identifier):
        row=self.raw(identifier)
        return row['conversation'][-20:] if row else []

    def add_conversation(self,identifier,question,answer):
        self.request('POST','/rest/v1/rpc/defectrag_append_conversation',json={'p_id':identifier,'p_question':question,'p_answer':answer})

    def alerts(self):
        return [{'id':row['id'],'inspection_id':row['id'],'created_at':row['created_at'],
                 'kind':row['verdict'],'acknowledged':int(row.get('alert_acknowledged',False))}
                for row in self.list(limit=1000) if row['verdict']!='Good']

    def acknowledge(self,identifier):
        return bool(self.request('POST','/rest/v1/rpc/defectrag_acknowledge_review',json={'p_id':identifier}))

    def archive(self,category='',verdict='',batch=''):
        rows=self.raw_list(category,verdict,batch,limit=500)
        ids=[row['id'] for row in rows if self.staff['role']=='developer' or row['owner_id']==self.user['id']]
        count=self.request('POST','/rest/v1/rpc/archive_team_inspections',json={'p_ids':ids}) if ids else 0
        return {'archived':count,'archive_batch':'supabase-audit','scope':'own visible records, or developer-authorized records'}

    def restore(self,identifiers):
        return {'restored':self.request('POST','/rest/v1/rpc/restore_team_inspections',json={'p_ids':identifiers})}

    def failure(self,category,error):
        log.warning('Model failure in authenticated workspace: category=%s error=%s',category,error)

    def add_review(self,identifier,status,decision,note=''):
        from backend.reviews import review_body,validate_review
        body=review_body(status,decision,note)
        rows=self.request('POST','/rest/v1/'+self.notes_table,json={
            'inspection_id':identifier,'author_id':self.user['id'],'body':body},
            headers={'Prefer':'return=representation'})
        return {**validate_review(status,decision,note),'reviewer':self.staff['display_name'],
                'reviewer_id':self.user['id'],'created_at':rows[0]['created_at']}

    def review_notes(self,identifier):
        rows=self.request('GET','/rest/v1/'+self.notes_table,params={
            'inspection_id':'eq.'+identifier,'select':'body,author_id,created_at','order':'created_at.asc'})
        people=self.request('GET','/rest/v1/staff_members',params={'select':'user_id,display_name'})
        directory={row['user_id']:row['display_name'] for row in people}
        return [{**row,'reviewer':directory.get(row['author_id'],'Staff member')} for row in rows]

    def review_history(self,identifier):
        from backend.reviews import parse_review
        return [{**value,'reviewer':row['reviewer'],'reviewer_id':row['author_id'],'created_at':row['created_at']}
                for row in self.review_notes(identifier) if (value:=parse_review(row['body']))]