feat(ci): run one uncapped Stage research after reviewer cleanup
One owner authorized natural Stage research / acceptance (push) Successful in 11s
One owner authorized natural Stage research / acceptance (push) Successful in 11s
This commit is contained in:
1 parent
c8063b5f2a
commit
b21ab3699c
3 files changed
+135
No files matched your search
@@ -0,0 +1,29 @@
|
||||
name: One owner authorized natural Stage research
|
||||
on:
|
||||
push:
|
||||
branches: [ci/v4-natural-c8063b5-20261024]
|
||||
jobs:
|
||||
acceptance:
|
||||
runs-on: canina
|
||||
steps:
|
||||
- name: Close independent reviewer before supervised research
|
||||
shell: bash
|
||||
env:
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
SOURCE_SHA: ${{ github.sha }}
|
||||
RUN_ATTEMPT: ${{ github.run_attempt }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ "$RUN_ATTEMPT" == 1 && "$SOURCE_SHA" =~ ^[a-f0-9]{40}$ ]]
|
||||
[[ -n "$DEPLOY_KEY" && -n "$DEPLOY_KNOWN_HOSTS" ]]
|
||||
umask 077
|
||||
scratch=$(mktemp -d)
|
||||
trap 'rm -rf "$scratch"' EXIT
|
||||
printf '%s\n' "$DEPLOY_KEY" > "$scratch/key"
|
||||
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$scratch/known_hosts"
|
||||
unset DEPLOY_KEY DEPLOY_KNOWN_HOSTS
|
||||
ssh-keygen -F '[87.248.133.138]:2234' -f "$scratch/known_hosts" >/dev/null
|
||||
wget --no-proxy -qO "$scratch/run.py" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/$SOURCE_SHA/scripts/stage-natural-acceptance.py"
|
||||
ssh -F /dev/null -T -p 2234 -i "$scratch/key" -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$scratch/known_hosts" -o GlobalKnownHostsFile=/dev/null -o ConnectTimeout=15 devops@87.248.133.138 'sudo -n python3 -c '\''import sys,io; s=sys.stdin.read(); sys.stdin=io.StringIO(s); exec(compile(s,"<tracked-ci>","exec"))'\''' < "$scratch/run.py"
|
||||
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One owner-authorized research-only acceptance; reviewer closes before inference."""
|
||||
import hashlib,json,os,subprocess,sys,time,uuid,urllib.request
|
||||
from pathlib import Path
|
||||
SHA='c8063b5f2afe420df8f920a52364ea73e3f2e513'
|
||||
SESSION='v4-natural-c8063b5-20261024'
|
||||
REQUEST='08c7a9de-4549-44f7-bb50-89fc77d558da'
|
||||
ROOT=Path('/opt/services/canina/acceptance')/SESSION
|
||||
|
||||
def orchestrate(open_,approve,close,verify,launch):
|
||||
try:
|
||||
open_(); task=approve()
|
||||
finally: close()
|
||||
verify()
|
||||
return launch(task)
|
||||
|
||||
def cmd(args):return subprocess.check_output(args,stderr=subprocess.DEVNULL,timeout=90).decode()
|
||||
def docker(*args):return cmd(['docker','exec',*args])
|
||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
def redirect_request(self,*args,**kwargs):return None
|
||||
def request(token,route,body=None):
|
||||
opener=urllib.request.build_opener(urllib.request.ProxyHandler({}),NoRedirect())
|
||||
req=urllib.request.Request('https://stageapi.canina.ir/api/'+route,data=None if body is None else json.dumps(body).encode(),headers={'Authorization':'Bearer '+token,'Content-Type':'application/json'})
|
||||
with opener.open(req,timeout=15) as r:
|
||||
data=r.read(262145)
|
||||
if len(data)>262144:raise ValueError('response bound')
|
||||
return json.loads(data)
|
||||
def worker_token():return docker('--user','hermes','canina-hermes','cat','/opt/data/editorial-v4/credentials/editorial-pmo-token').strip()
|
||||
def account(action):
|
||||
return cmd(['env','CI=true','STAGE_TEST_APPROVAL=owner-authorized-stage-editorial','bash','/opt/services/canina/releases/'+SHA+'/scripts/stage-test-principal-host.sh',SHA,SESSION,'600',action])
|
||||
def verify_cleanup():
|
||||
code="const {PrismaClient}=require('@prisma/client');const p=new PrismaClient();p.user.count({where:{email:'"+EMAIL+"'}}).then(n=>{console.log(n);return p.$disconnect()})"
|
||||
if docker('canino_backend_stage','node','-e',code).strip()!='0':raise ValueError('account remains')
|
||||
if subprocess.run(['docker','exec','canino_backend_stage','test','!','-e','/app/private-stage-test/'+SESSION+'.json'],stdout=subprocess.DEVNULL).returncode:raise ValueError('handle remains')
|
||||
def approve():
|
||||
handle=json.loads(docker('canino_backend_stage','cat','/app/private-stage-test/'+SESSION+'.json'))
|
||||
if handle['user']['role']!='SUPER_ADMIN':raise ValueError('independent reviewer required')
|
||||
admin=handle['accessToken']; token=worker_token()
|
||||
a=lambda r,b=None:request(admin,'admin/orchestration/'+r,b)
|
||||
old=[a('tasks/'+t['id']) for t in a('tasks') if t.get('key','').startswith('editorial-v1:')]
|
||||
failed=[t for t in old if t['state']=='FAILED']
|
||||
if len(failed)!=4 or sum(x['amountCents'] for t in failed for x in t['budgetLedger'])!=80:raise ValueError('prior failed unknown80 fence')
|
||||
ROOT.joinpath('prior.json').write_text(json.dumps({t['id']:t['budgetLedger'] for t in failed}))
|
||||
if any(t['state'] not in ('FAILED','CANCELLED') for t in old):raise ValueError('existing active attempt')
|
||||
proposal={'requestId':REQUEST,'researchTopic':'Canina pet supplement market research and evidence-grounded editorial opportunities','acceptanceCriteria':['Research to brief only; record real retrieved sources and explicit analytics/inventory gaps','Provide distinct scored editorial candidates; independent QA remains pending; no publication or Production authority'],'pilotKind':'editorial-research-to-brief-v1','operationMode':'PREPARE','environment':'staging','scope':'EDITORIAL_RESEARCH_TO_BRIEF','commitSha':SHA,'budgetLimitCents':20}
|
||||
result=request(token,'internal/prepare-worker/editorial-proposals',proposal); task=result['taskId']; row=a('tasks/'+task)
|
||||
if row['runs'] or row['budgetLedger']:raise ValueError('started task rejected')
|
||||
approved=a('tasks/'+task+'/gates/1/decide',{'decision':'APPROVED','expectedVersion':row['version'],'idempotencyKey':SESSION+':gate1','expiresAt':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime(time.time()+450)),'notes':'Owner authorized research-only functional bringup; independent Stage QA pending; no Production release.'})
|
||||
if approved['state']!='READY':raise ValueError('READY required')
|
||||
ROOT.joinpath('task.json').write_text(json.dumps({'taskId':task,'requestId':REQUEST,'commitSha':SHA}))
|
||||
return task
|
||||
# Read canonical fixed seed email from tracked module, never customer data.
|
||||
EMAIL='stage-editorial-reviewer@ci.canina.invalid'
|
||||
def launch(task):
|
||||
unit=SESSION
|
||||
subprocess.run(['systemd-run','--unit='+unit,'--property=Type=exec','--property=Restart=no','/usr/bin/python3',str(ROOT/'runner.py'),'--worker',task],check=True,stdout=subprocess.DEVNULL)
|
||||
result={'status':'ASYNC_STARTED_NOT_COMPLETE','persistentJobHandle':unit+'.service','taskId':task,'commitSha':SHA,'cleanupAccountCount':0,'cleanupHandleAbsent':True,'automaticPaidRetry':False}
|
||||
ROOT.joinpath('status.json').write_text(json.dumps(result));print(json.dumps(result))
|
||||
def work(task):
|
||||
code="import sys;sys.path.insert(0,'/opt/data/editorial-v4/runtime-package');import runner;from pathlib import Path;runner.run(Path('/opt/data'),sys.argv[1])"
|
||||
rc=subprocess.run(['docker','exec','--user','hermes','canina-hermes','python3','-c',code,task],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode
|
||||
status=request(worker_token(),'internal/prepare-worker/editorial-proposals/'+task)
|
||||
result={'taskId':task,'state':status['state'],'runnerExit':rc,'costStatus':'unknown-reservation-retained','qaStatus':status.get('qaStatus'),'automaticPaidRetry':False}
|
||||
if status['state']=='VERIFYING' and status.get('qaStatus')=='PENDING_INDEPENDENT_QA':
|
||||
artifact=status['artifact']; data=artifact['bytes'];binding=json.loads(data)
|
||||
if hashlib.sha256(data.encode()).hexdigest()!=artifact['sha256'] or binding['taskId']!=task or binding['commitSha']!=SHA:raise ValueError('digest binding')
|
||||
result['artifactSha256']=artifact['sha256']
|
||||
ROOT.joinpath('result.json').write_text(json.dumps(result))
|
||||
def main():
|
||||
if os.getuid()!=0 or Path('/.dockerenv').exists():raise ValueError('host only')
|
||||
if sys.argv[1:2]==['--worker']:work(sys.argv[2]);return
|
||||
logs=Path('/opt/services/git/gitea/gitea/gitea/actions_log/parsa/canina')
|
||||
if not any(SHA in (s:=p.read_text()) and 'Deployment for develop completed successfully!' in s and '🏁 Job succeeded' in s for p in logs.glob('*/*.log')):raise ValueError('exact CI success required')
|
||||
if docker('canino_backend_stage','cat','/app/STAGE_TEST_ARTIFACT_SHA').strip()!=SHA or json.loads(docker('canina-hermes','cat','/opt/data/editorial-v4/deployment.json'))['commitSha']!=SHA:raise ValueError('deployment mismatch')
|
||||
ROOT.mkdir(mode=0o700,parents=True,exist_ok=False)
|
||||
source=sys.stdin.read()
|
||||
if not source:raise ValueError('tracked persistent source required')
|
||||
ROOT.joinpath('runner.py').write_text(source)
|
||||
orchestrate(lambda:account('open'),approve,lambda:account('close'),verify_cleanup,launch)
|
||||
if __name__=='__main__':
|
||||
try:main()
|
||||
except Exception:
|
||||
print('Acceptance stopped; no automatic paid retry; inspect allowlisted status',file=sys.stderr);sys.exit(1)
|
||||
@@ -0,0 +1,23 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
ROOT=Path(__file__).parent
|
||||
class NaturalAcceptanceTests(unittest.TestCase):
|
||||
def test_close_precedes_launch_and_never_retries(self):
|
||||
s=importlib.util.spec_from_file_location('natural',ROOT/'stage-natural-acceptance.py'); m=importlib.util.module_from_spec(s);s.loader.exec_module(m)
|
||||
events=[]
|
||||
m.orchestrate(lambda:events.append('open'),lambda:events.append('approve') or 'task',lambda:events.append('close'),lambda:events.append('verify'),lambda t:events.append('launch:'+t))
|
||||
self.assertEqual(events,['open','approve','close','verify','launch:task'])
|
||||
def test_failed_approval_closes_without_launch(self):
|
||||
s=importlib.util.spec_from_file_location('natural',ROOT/'stage-natural-acceptance.py'); m=importlib.util.module_from_spec(s);s.loader.exec_module(m)
|
||||
events=[]
|
||||
def fail():raise ValueError()
|
||||
with self.assertRaises(ValueError):m.orchestrate(lambda:events.append('open'),fail,lambda:events.append('close'),lambda:events.append('verify'),lambda t:events.append('launch'))
|
||||
self.assertEqual(events,['open','close'])
|
||||
def test_failed_cleanup_blocks_launch(self):
|
||||
s=importlib.util.spec_from_file_location('natural',ROOT/'stage-natural-acceptance.py'); m=importlib.util.module_from_spec(s);s.loader.exec_module(m)
|
||||
events=[]
|
||||
def fail():raise ValueError()
|
||||
with self.assertRaises(ValueError):m.orchestrate(lambda:None,lambda:'task',lambda:None,fail,lambda t:events.append('launch'))
|
||||
self.assertEqual(events,[])
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in new issue
Block a user