356 lines
20 KiB
Python
356 lines
20 KiB
Python
import asyncio
|
|
import contextlib
|
|
import json
|
|
import shlex
|
|
import time
|
|
import uuid
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
from pydantic import BaseModel, Field
|
|
from .config import BLENDER_BIN, FREEROUTING_JAR, PROVIDER
|
|
from .storage import SOURCE, ROOT, TASKS, MODEL, EFFORT, task_dir, workspace, load, save, write, event, events, fingerprint, digest, inside
|
|
from .rpc import AppServer
|
|
from .eda import execute
|
|
from .delivery import deliver
|
|
|
|
CACHE, JOBS, CLIENTS = {}, {}, {}
|
|
|
|
def object_schema(properties, required=None):
|
|
return {'type':'object','properties':properties,'required':list(properties) if required is None else required,'additionalProperties':False}
|
|
|
|
def tool(name, description, properties, required=None):
|
|
return {'type':'function','name':name,'description':description,'inputSchema':object_schema(properties,required)}
|
|
|
|
STR={'type':'string'}
|
|
TOOLS=[
|
|
tool('pcb_proposal','Publish or replace the proposal for website review; not production authorization.',{'title':STR,'markdown':STR}),
|
|
tool('pcb_read_reference','Read official EDA API documentation. Relative path under api-reference, e.g. references/_index.md. Also available via shell during production.',{'path':STR,'offset':{'type':'integer','minimum':0},'limit':{'type':'integer','minimum':100,'maximum':40000}},['path']),
|
|
tool('pcb_eda','Execute YOUR JavaScript async function body in this task dedicated EasyEDA session. Only after website approval. Global eda and async pcb_export(file,relative_name) are available. Use code OR code_file (workspace-relative JS path). Full outputs preserved; uncertain writes must be inspected.',{'label':STR,'code':STR,'code_file':STR},['label']),
|
|
tool('pcb_deliver','Submit real exported files for independent validation. Rejected submission returns evidence so you can repair in the SAME session.',{'schematic_id':STR,'pcb_id':STR,'notes':STR,'files':{'type':'array','items':object_schema({'path':STR,'kind':{'type':'string','enum':['project','schematic','layout','step','render','bom','report','other']},'label':STR}), 'minItems':5}}),
|
|
tool('pcb_blocked','Record a concrete unrecoverable blocker and preserve draft. Do not use for ordinary tool errors; investigate and repair first.',{'reason':STR,'evidence':STR}),
|
|
]
|
|
|
|
def get(tid):
|
|
try:
|
|
if tid not in CACHE:
|
|
CACHE[tid]=load(tid)
|
|
return CACHE[tid]
|
|
except (ValueError,FileNotFoundError):
|
|
raise HTTPException(404,'Task not found')
|
|
|
|
def public(t):
|
|
return {k:v for k,v in t.items() if k not in {'pending_prompt','internal'}}
|
|
|
|
def frozen(t):
|
|
if t.get('approval') and fingerprint()!=t['frozen_source']:
|
|
raise RuntimeError('Backend source or runtime settings changed after approval. This acceptance run is invalid; start a new task.')
|
|
|
|
async def handle(t, method, p):
|
|
if method != 'item/tool/call':
|
|
event(t,'request.unhandled',{'method':method})
|
|
if 'requestApproval' in method:
|
|
if 'permissions' in method:
|
|
return {'permissions':{},'scope':'turn'}
|
|
return {'decision':'decline'}
|
|
if 'requestUserInput' in method:
|
|
return {'answers':{}}
|
|
if 'elicitation' in method:
|
|
return {'action':'cancel','content':None}
|
|
raise RuntimeError('Unsupported Codex request: '+method)
|
|
name, a = p['tool'],p['arguments']
|
|
event(t,'tool.started',{'tool':name,'call_id':p['callId']})
|
|
try:
|
|
frozen(t)
|
|
if t['state'] == 'cancelled':
|
|
raise RuntimeError('Task cancelled')
|
|
if name=='pcb_proposal':
|
|
if t.get('approval'):
|
|
raise ValueError('Already approved; preserve accepted design and finish production')
|
|
markdown=a['markdown'].strip()
|
|
if len(markdown)<120:
|
|
raise ValueError('Proposal must describe circuit, board, assumptions and deliverables')
|
|
revision=t.get('proposal',{}).get('revision',0)+1
|
|
t['proposal']={'revision':revision,'title':a['title'],'markdown':markdown,'hash':digest(markdown.encode()),'at':time.time()}
|
|
event(t,'proposal.published',t['proposal'])
|
|
result={'published':True,'revision':revision,'instruction':'Now finish your reply and wait for the website approval. Do not operate EDA.'}
|
|
elif name=='pcb_read_reference':
|
|
path=inside(ROOT/'resources',a['path'])
|
|
if path.suffix not in {'.md','.txt','.json'}:
|
|
raise ValueError('Reference text files only')
|
|
text=path.read_text()
|
|
start=max(0,a.get('offset',0));limit=min(40000,a.get('limit',18000))
|
|
result={'path':str(path),'text':text[start:start+limit],'total_chars':len(text),'next_offset':start+limit if start+limit<len(text) else None}
|
|
elif name=='pcb_eda':
|
|
if bool(a.get('code'))==bool(a.get('code_file')):
|
|
raise ValueError('Specify exactly one of code or code_file')
|
|
code=a.get('code') or inside(workspace(t),a['code_file']).read_text()
|
|
result=await execute(t,code,a['label'])
|
|
raw=json.dumps(result,ensure_ascii=False)
|
|
if len(raw)>24000:
|
|
result={'output_path':str(workspace(t)/'evidence'/f'eda-{t["eda_calls"]:05d}.json'),'preview':raw[:18000],'truncated':True,'instruction':'Read the complete file using shell; do not guess omitted fields'}
|
|
elif name=='pcb_deliver':
|
|
if not t.get('approval') or t['state']!='running':
|
|
raise ValueError('Production approval required')
|
|
t['delivery']=await deliver(t,a)
|
|
frozen(t)
|
|
t['state']='completed'
|
|
event(t,'delivery.completed',t['delivery'])
|
|
result={'accepted':True,'files':t['delivery']['files'],'instruction':'Delivery verified; give a concise user summary and finish.'}
|
|
elif name=='pcb_blocked':
|
|
if not t.get('approval'):
|
|
raise ValueError('Publish assumptions or questions in the proposal during review')
|
|
t['state']='blocked';t['error']=a['reason'];t['blocker_evidence']=a['evidence']
|
|
event(t,'production.blocked',a)
|
|
result={'recorded':True,'instruction':'Report the blocker honestly and stop.'}
|
|
else:
|
|
raise ValueError('Unknown tool '+name)
|
|
event(t,'tool.completed',{'tool':name,'call_id':p['callId']})
|
|
return {'success':True,'contentItems':[{'type':'inputText','text':json.dumps(result,ensure_ascii=False)}]}
|
|
except Exception as e:
|
|
event(t,'tool.error',{'tool':name,'message':str(e)})
|
|
return {'success':False,'contentItems':[{'type':'inputText','text':str(e)}]}
|
|
|
|
async def run_turn(t, prompt, production):
|
|
frozen(t)
|
|
if t.get('model', MODEL) != MODEL or t.get('effort', EFFORT) != EFFORT or (t.get('thread_id') and t.get('provider', 'openai') != PROVIDER):
|
|
raise RuntimeError('Task model, effort or provider differs from the current runtime; restore the previous settings or start a new task.')
|
|
directory=workspace(t)
|
|
async with AppServer(directory, production, lambda m,p:handle(t,m,p)) as client:
|
|
CLIENTS[t['id']]=client
|
|
common={'model':MODEL,'modelProvider':PROVIDER,'cwd':str(directory),'approvalPolicy':'never','sandbox':'workspace-write' if production else 'read-only','developerInstructions':(SOURCE/'instructions.md').read_text(),'serviceTier':'default'}
|
|
if t.get('thread_id'):
|
|
result=await client.call('thread/resume',dict(common,threadId=t['thread_id'],excludeTurns=True))
|
|
if result['thread']['id']!=t['thread_id']:
|
|
raise RuntimeError('Codex resumed a different thread')
|
|
event(t,'session.resumed',{'thread_id':t['thread_id']})
|
|
else:
|
|
result=await client.call('thread/start',dict(common,ephemeral=False,dynamicTools=TOOLS,serviceName='vibemake-persistent-pcb',allowProviderModelFallback=False))
|
|
t['thread_id']=result['thread']['id']
|
|
event(t,'session.created',{'thread_id':t['thread_id'],'model':result.get('model')})
|
|
if result.get('model') not in {None,MODEL}:
|
|
raise RuntimeError('Unexpected model substitution')
|
|
policy={'type':'workspaceWrite','writableRoots':[str(directory)],'networkAccess':True,'excludeSlashTmp':True,'excludeTmpdirEnvVar':True} if production else {'type':'readOnly','networkAccess':False}
|
|
reply=await client.call('turn/start',{'threadId':t['thread_id'],'input':[{'type':'text','text':prompt}],'model':MODEL,'effort':EFFORT,'approvalPolicy':'never','sandboxPolicy':policy})
|
|
t['turn_id']=reply['turn']['id'];t['turn_started_at']=time.time()
|
|
event(t,'turn.started',{'turn_id':t['turn_id'],'production':production,'model':MODEL,'effort':EFFORT,'source_hash':fingerprint()})
|
|
trace=directory/'evidence'/'codex-events.jsonl'
|
|
trace.parent.mkdir(exist_ok=True)
|
|
last_heartbeat=time.monotonic()
|
|
while True:
|
|
try:
|
|
msg=await asyncio.wait_for(client.events.get(),30)
|
|
except asyncio.TimeoutError:
|
|
event(t,'heartbeat',{'turn_id':t['turn_id'],'elapsed':round(time.time()-t['turn_started_at'])})
|
|
continue
|
|
method,p=msg['method'],msg.get('params',{})
|
|
if method in {'transport/closed','transport/error'}:
|
|
raise ConnectionError(p.get('message','Codex disconnected'))
|
|
if p.get('threadId') not in {None,t['thread_id']}:
|
|
continue
|
|
# Store complete native event evidence; no lossy context reconstruction.
|
|
with trace.open('a') as f:
|
|
f.write(json.dumps(msg,ensure_ascii=False)+'\n')
|
|
if method=='item/completed':
|
|
item=p.get('item',{})
|
|
if item.get('type')=='agentMessage':
|
|
event(t,'message',{'role':'assistant','text':item.get('text',''),'phase':item.get('phase')})
|
|
elif item.get('type') not in {'userMessage','reasoning'}:
|
|
event(t,'activity',{'kind':item.get('type'),'status':item.get('status'),'command':item.get('command'),'tool':item.get('tool')})
|
|
elif method=='item/started':
|
|
item=p.get('item',{})
|
|
if item.get('type') not in {'userMessage','agentMessage','reasoning'}:
|
|
event(t,'activity',{'kind':item.get('type'),'status':'running','command':item.get('command'),'tool':item.get('tool')})
|
|
elif method=='thread/tokenUsage/updated':
|
|
t['usage']=p.get('tokenUsage');save(t)
|
|
elif method=='turn/completed' and p.get('turn',{}).get('id')==t['turn_id']:
|
|
turn=p['turn']
|
|
event(t,'turn.completed',{'turn_id':turn['id'],'status':turn['status'],'error':turn.get('error')})
|
|
if turn['status']!='completed':
|
|
raise RuntimeError('Codex turn '+turn['status']+': '+json.dumps(turn.get('error'),ensure_ascii=False))
|
|
return
|
|
|
|
async def work(t, prompt, production):
|
|
t['pending_prompt']=prompt
|
|
save(t)
|
|
try:
|
|
failures=0;no_progress=0
|
|
while True:
|
|
before=t.get('eda_calls',0)
|
|
try:
|
|
await run_turn(t,prompt,production)
|
|
failures=0
|
|
except (ConnectionError,asyncio.TimeoutError) as e:
|
|
if t['state'] in {'cancelled','completed','blocked'}:
|
|
break
|
|
failures+=1
|
|
event(t,'session.reconnecting',{'attempt':failures,'message':str(e)})
|
|
if failures>=5:
|
|
raise
|
|
await asyncio.sleep(min(5*failures,30))
|
|
prompt='Connection recovery. Continue the existing approved task from the full conversation and actual files/EDA state. Inspect uncertain mutations before replaying anything.' if production else t['pending_prompt']
|
|
continue
|
|
if not production:
|
|
if t.get('proposal'):
|
|
t['state']='review'
|
|
else:
|
|
t['state']='needs_input'
|
|
t['error']='Codex has not published a proposal; add a comment to continue.'
|
|
save(t);break
|
|
if t['state'] in {'completed','blocked','cancelled'}:
|
|
break
|
|
no_progress=no_progress+1 if before==t.get('eda_calls',0) else 0
|
|
if no_progress>=3:
|
|
raise RuntimeError('Three completed turns made no EDA progress and produced no verified delivery. Draft and full session retained.')
|
|
prompt='The approved task is still incomplete: no accepted pcb_deliver result exists. Continue autonomously in this same session, inspect the actual workspace/EDA, resolve remaining problems and deliver. If a concrete external blocker makes further progress impossible, use pcb_blocked with evidence. Do not merely state intentions.'
|
|
event(t,'production.continued',{'reason':'No verified delivery yet','same_thread':t['thread_id']})
|
|
except asyncio.CancelledError:
|
|
if t['state'] not in {'completed','cancelled','blocked'}:
|
|
t['state']='interrupted'
|
|
event(t,'session.interrupted',{'resume_same_thread':t.get('thread_id')})
|
|
raise
|
|
except Exception as e:
|
|
if t['state'] not in {'completed','cancelled','blocked'}:
|
|
t['state']='failed';t['error']=str(e)
|
|
event(t,'session.failed',{'message':str(e)})
|
|
finally:
|
|
CLIENTS.pop(t['id'],None)
|
|
JOBS.pop(t['id'],None)
|
|
t['busy']=False;save(t)
|
|
|
|
def schedule(t,prompt,production):
|
|
if t['id'] in JOBS:
|
|
raise HTTPException(409,'Task already running')
|
|
t['busy']=True
|
|
t['state']='running' if production else 'planning'
|
|
t.pop('error',None)
|
|
save(t)
|
|
JOBS[t['id']]=asyncio.create_task(work(t,prompt,production))
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app):
|
|
TASKS.mkdir(parents=True,exist_ok=True)
|
|
for p in TASKS.glob('*/state.json'):
|
|
t=json.loads(p.read_text());CACHE[t['id']]=t
|
|
if t['state'] not in {'completed','cancelled','blocked','failed'} and (t.get('busy') or t['state'] in {'running','planning','interrupted'}):
|
|
t['check_eda_session']=bool(t.get('eda_session'))
|
|
prompt=('Service restarted. Continue this approved task in the same session. Inspect live state and existing files before repeating uncertain actions.' if t.get('approval') else t.get('pending_prompt',t['request']))
|
|
schedule(t,prompt,bool(t.get('approval')))
|
|
yield
|
|
for job in list(JOBS.values()):
|
|
job.cancel()
|
|
await asyncio.gather(*list(JOBS.values()),return_exceptions=True)
|
|
|
|
app=FastAPI(lifespan=lifespan)
|
|
|
|
class Request(BaseModel):
|
|
text:str=Field(min_length=5,max_length=16000)
|
|
class Approval(BaseModel):
|
|
revision:int
|
|
hash:str
|
|
|
|
@app.get('/api/health')
|
|
async def health():
|
|
return {'ok':True,'engine':'persistent-codex','model':MODEL,'effort':EFFORT,'source_hash':fingerprint(),'legacy_pipeline':False}
|
|
|
|
@app.get('/api/sessions')
|
|
async def list_sessions():
|
|
return sorted([public(t) for t in CACHE.values()],key=lambda t:t['created_at'],reverse=True)
|
|
|
|
@app.post('/api/sessions',status_code=201)
|
|
async def create(req:Request):
|
|
tid='pcb-'+uuid.uuid4().hex[:24]
|
|
t={'id':tid,'request':req.text,'created_at':time.time(),'state':'planning','cursor':0,'model':MODEL,'effort':EFFORT,'provider':PROVIDER,'busy':False}
|
|
directory=workspace(t);directory.mkdir(parents=True)
|
|
for name in ('artifacts','evidence','scripts','tmp'):
|
|
(directory/name).mkdir()
|
|
(directory/'api-reference').symlink_to(ROOT/'resources',target_is_directory=True)
|
|
(directory/'AGENTS.md').write_text('Use only this workspace for generated PCB scripts/files. Do not read or use legacy PCB pipelines. Official API references are under api-reference. Do not edit service code or unrelated projects. Do not spawn subagents.\n')
|
|
CACHE[tid]=t;save(t)
|
|
event(t,'message',{'role':'user','text':req.text})
|
|
schedule(t,'User requirement:\n'+req.text+'\n\nFirst prepare a reviewable proposal and publish it with pcb_proposal. Do not produce the board yet.',False)
|
|
return public(t)
|
|
|
|
@app.get('/api/sessions/{tid}')
|
|
async def detail(tid:str):
|
|
return public(get(tid))
|
|
|
|
@app.get('/api/sessions/{tid}/events')
|
|
async def history(tid:str,after:int=0):
|
|
get(tid)
|
|
return events(tid,after)
|
|
|
|
@app.post('/api/sessions/{tid}/comments')
|
|
async def comment(tid:str,req:Request):
|
|
t=get(tid)
|
|
if t.get('approval') or t['state'] not in {'review','needs_input','failed'} or t.get('busy'):
|
|
raise HTTPException(409,'Comments are allowed only while reviewing a proposal')
|
|
event(t,'message',{'role':'user','text':req.text})
|
|
schedule(t,'User review comment:\n'+req.text+'\nRevise the proposal in this same conversation and publish a replacement with pcb_proposal. This is not production authorization.',False)
|
|
return public(t)
|
|
|
|
@app.post('/api/sessions/{tid}/approve')
|
|
async def approve(tid:str,req:Approval):
|
|
t=get(tid)
|
|
p=t.get('proposal',{})
|
|
if t.get('approval'):
|
|
if t['approval']['hash']==req.hash:
|
|
return public(t)
|
|
raise HTTPException(409,'Different proposal already approved')
|
|
if t['state']!='review' or t.get('busy') or p.get('revision')!=req.revision or p.get('hash')!=req.hash:
|
|
raise HTTPException(409,'Approve the latest completed proposal revision')
|
|
t['approval']={'revision':req.revision,'hash':req.hash,'at':time.time()}
|
|
t['frozen_source']=fingerprint()
|
|
event(t,'proposal.approved',t['approval'])
|
|
prompt=f'''The user explicitly clicked APPROVE AND START for proposal revision {req.revision}, SHA256 {req.hash}.
|
|
This server-owned approval authorizes production of the accepted proposal in this same persistent session. Earlier requests to wait applied only to review.
|
|
Workspace: {workspace(t)}
|
|
Official API documentation: {workspace(t)}/api-reference/references/_index.md
|
|
External router launcher: python3 {shlex.quote(str(SOURCE/'freerouting.py'))} --input <board.dsn> --output <new-result.ses> --jar {shlex.quote(str(FREEROUTING_JAR))} --timeout 900 -- -mp 30 -mt 1
|
|
Use this launcher for Freerouting, including --help. It validates paths, skips the startup countdown and runs on a private virtual display. Choose routing options and timeout according to the board; do not invoke the JAR directly. Blender command: {shlex.quote(BLENDER_BIN)}.
|
|
Now use native Codex tools and pcb_eda to implement, inspect, fix, route, verify and export the real board. You write the design scripts yourself. No fixed backend design pipeline exists. Keep working until pcb_deliver accepts native project, schematic, layout, assembled STEP and real 3D render. Do not edit backend code or use legacy PCB flows. Backend source is frozen for this run.
|
|
Accepted proposal:\n{p['markdown']}'''
|
|
schedule(t,prompt,True)
|
|
return public(t)
|
|
|
|
@app.post('/api/sessions/{tid}/cancel')
|
|
async def cancel(tid:str):
|
|
t=get(tid)
|
|
if t['state']=='completed':
|
|
raise HTTPException(409,'Already completed')
|
|
t['state']='cancelled';event(t,'task.cancelled',{})
|
|
client=CLIENTS.get(tid)
|
|
if client and t.get('turn_id'):
|
|
with contextlib.suppress(Exception):
|
|
await client.call('turn/interrupt',{'threadId':t['thread_id'],'turnId':t['turn_id']},timeout=10)
|
|
job=JOBS.get(tid)
|
|
if job:
|
|
job.cancel()
|
|
return public(t)
|
|
|
|
@app.post('/api/sessions/{tid}/resume')
|
|
async def resume(tid:str):
|
|
t=get(tid)
|
|
if t.get('busy') or t['state'] not in {'failed','blocked','interrupted','cancelled'}:
|
|
raise HTTPException(409,'Task is not resumable')
|
|
try:
|
|
frozen(t)
|
|
except RuntimeError as e:
|
|
raise HTTPException(409,str(e))
|
|
t['check_eda_session']=bool(t.get('eda_session'))
|
|
event(t,'task.resume_requested',{})
|
|
schedule(t,'The user requests continuing this same task. Preserve the accepted design and inspect actual saved files and EDA state. Continue to verified delivery.' if t.get('approval') else 'Continue the proposal review and publish a complete proposal.',bool(t.get('approval')))
|
|
return public(t)
|
|
|
|
@app.get('/api/sessions/{tid}/files/{name}')
|
|
async def download(tid:str,name:str):
|
|
t=get(tid)
|
|
item=next((f for f in t.get('delivery',{}).get('files',[]) if f['name']==name),None)
|
|
if not item or t['state']!='completed':
|
|
raise HTTPException(404,'Verified artifact not found')
|
|
path=inside(task_dir(tid)/'delivery',name)
|
|
if digest(path.read_bytes())!=item['sha256']:
|
|
raise HTTPException(409,'Artifact checksum mismatch')
|
|
return FileResponse(path,filename=name,headers={'Cache-Control':'no-store','X-Content-Type-Options':'nosniff'})
|