"""Generate reusable, project-scoped skill bundles from verified Java artifacts.""" import io import json import zipfile from src.modules.skill.domain.models import SkillError, canonical, digest class JavaSkills: def __init__(self, engine): self.engine=engine with engine.store.connect() as db: db.execute('CREATE TABLE IF NOT EXISTS web_java_skills(id TEXT PRIMARY KEY,owner TEXT,scenario TEXT,body TEXT)') def create(self, actor, kind, reference): scenario,run_id,evidence=self.engine.evidence(actor,kind,reference) self.engine.check_evidence(evidence) spec=self.engine.spec(actor,scenario) body={'scenario':scenario,'name':spec['name']+' · 已验证源码技能','run_id':run_id, 'source_digest':evidence['manifest']['source']['source_digest'],'evidence':evidence, 'base_skill':spec['skill'],'contract':spec['module'].CONTRACT, 'state':'local_verified_pending_acceptance','human_acceptance':False} key='java-'+digest([actor.user_id,body])[:24] with self.engine.store.connect() as db: db.execute('INSERT OR IGNORE INTO web_java_skills VALUES(?,?,?,?)',(key,actor.user_id,scenario,canonical(body))) return self.get(actor,key) def get(self, actor, key): with self.engine.store.connect() as db: row=db.execute('SELECT * FROM web_java_skills WHERE id=? AND owner=?',(key,actor.user_id)).fetchone() if not row: raise SkillError('SKILL_NOT_FOUND','技能不存在或不可见。',404) self.engine.spec(actor,row['scenario']) body=json.loads(row['body']) if key!='java-'+digest([actor.user_id,body])[:24]: raise SkillError('SKILL_DIGEST_MISMATCH','技能内容已改变。',409) self.engine.check_evidence(body['evidence']) return {'id':key,**body} def list(self, actor): with self.engine.store.connect() as db: ids=[r['id'] for r in db.execute('SELECT id FROM web_java_skills WHERE owner=?',(actor.user_id,))] result=[] for key in ids: try: body=self.get(actor,key) except SkillError as exc: if exc.status==404: continue raise result.append({k:v for k,v in body.items() if k not in {'evidence','contract'}}) return result def export(self, actor, key): body=self.get(actor,key) manifest=body['evidence']['manifest'] budget=manifest['snapshot']['budget'] dependency='Java17 standard library plus pinned Apache PDFBox 3.0.8' if body['scenario']=='pdf' else 'Java17 standard library only' text=f'''--- name: {key} description: Develop {body['scenario']} services using {dependency}, the verified interface and source baseline. Use for this contract; other frameworks require an explicit new profile. --- Read [the interface contract](references/contract.txt) before modifying the [verified source](assets/src/main/java/demo/App.java). The source baseline is `{body['source_digest']}`; provenance is in [validation evidence](references/evidence.json). 1. Compare the requested behavior with the pinned contract. If the business interface changes, record that change and update independent acceptance criteria before execution. 2. Preserve {dependency}, port 8080 and demo.App. Do not introduce a framework or dependency silently. Use the selected project and authenticated actor; never infer privileges from a prompt or knowledge text. Read the pinned framework and dependency versions in references/evidence.json before building; PDFBox is supplied read-only by the approved sandbox profile, never downloaded at runtime. 3. Generate only source paths allowed by the harness. Do not change protected tests or run generated code on the host. 4. In ZKCoding choose this skill and submit the requirement. The server invokes the shared Agent, bounded model repair and isolated VM. Alternatively choose Revalidate to rebuild and run the pinned source through the protected suite and preview. 5. For compile/test failure, use actual diagnostics. Read the frozen budget in references/evidence.json: at most {budget['max_model_calls']} actual model calls, {budget['max_tokens']} tokens and {budget['max_repairs']} compile/test repairs in this task. Known truncated responses may be regenerated only after usage settlement and within the same budget; unknown provider outcomes stop without automatic replay. Further repair requires a new traceable attempt linked to the failed source. 6. Return source, build/test/preview evidence and their matching digests. Technical success remains pending user acceptance. This skill does not authorize target deployment. ''' stream=io.BytesIO() with zipfile.ZipFile(stream,'w',zipfile.ZIP_DEFLATED) as z: z.writestr('SKILL.md',text) z.writestr('references/contract.txt',body['contract']) z.writestr('references/evidence.json',json.dumps(body['evidence'],ensure_ascii=False,indent=2)) z.writestr('skill-manifest.json',canonical({k:v for k,v in body.items() if k not in {'evidence','contract'}})) self.engine.runtimes[body['scenario']].harness.files({'files':manifest['source']['files']},manifest['snapshot']['framework']) for path,content in manifest['source']['files'].items(): z.writestr('assets/'+path,content) return stream.getvalue()