59 lines
4.5 KiB
Python
59 lines
4.5 KiB
Python
"""Native design workflow contract and EDA transport guard (not a shell sandbox)."""
|
|
import json
|
|
|
|
DESIGN_POLICY = 'easyeda-native-v1'
|
|
POLICY_TEXT = '''Design policy: easyeda-native-v1.
|
|
Create, edit, lay out, route, save and check the circuit only in the task's native EasyEDA project through pcb_eda.
|
|
Use native schematic/PCB primitives and native routing APIs. Do not use Freerouting, KiCad or any external EDA/router, export DSN, import SES, or exchange auto-layout/auto-route JSON files.
|
|
Local Python/JavaScript may calculate coordinates, paths and diagnostics from native object readbacks; apply their results by creating/modifying native EasyEDA objects, not by importing a converted design or routing file. Native ERC/DRC and actual native net/geometry readback are authoritative; offline checks are supplementary only.
|
|
Use pcb_eda for all EDA operations; do not bypass it with direct CLI, browser or alternate sessions. Keep the accepted circuit and manufacturing constraints. A native routing timeout has an unknown outcome: inspect the live board before another mutation. Never fall back to external routing after an error.
|
|
Validation policy: native-errors-block-warnings-record-v1. Explicit native warnings are allowed and must be recorded; actual errors and unknown/incomplete check results block delivery. Do not change electrical rules to silence errors. STEP colour/model-library appearance warnings are non-blocking: use the native STEP and native OBJ render, disclose limitations, and do not rebuild STEP geometry or spend repeated turns repairing cosmetic/library warnings. The server packages accepted delivery files; do not write packaging scripts. Call pcb_check early, then pcb_prepare_delivery when ready, then pcb_deliver. If helper tools are not present in an older session, use pcb_documents(), pcb_focus(id), pcb_capabilities() inside pcb_eda; pcb_export automatically transfers large files in chunks, and pcb_deliver applies the same warning-aware gate.
|
|
Earlier conversation instructions offering external routing are superseded. On resume, inspect the saved native board; do not replay old DSN/SES/import scripts. Existing native geometry may be retained and repaired after checking it.
|
|
Final native project, schematic/layout images, manufacturing outputs, STEP/OBJ and BOM exports remain allowed. Blender/CAD utilities may render or validate the exported 3D assembly only; they must not design, route or electrically validate the PCB instead of EasyEDA.
|
|
'''
|
|
|
|
WORKSPACE_RULES = '''Use only this workspace for generated PCB scripts/files. Do not read or use legacy PCB pipelines or other PCB tasks. Official API references are under api-reference. Do not edit service code or unrelated projects. Do not spawn subagents.
|
|
''' + POLICY_TEXT
|
|
|
|
BLOCKED_METHODS = {
|
|
'pcb_ManufactureData': ['getDsnFile', 'getAutoRouteJsonFile', 'getAutoRouteJsonFileForJRouter'],
|
|
'pcb_Document': ['importAutoRouteSesFile', 'importAutoRouteJsonFile', 'importAutoLayoutJsonFile'],
|
|
}
|
|
|
|
|
|
def refresh_workspace_rules(directory):
|
|
path = directory / 'AGENTS.md'
|
|
marker = '\n## Backend native design policy\n'
|
|
existing = path.read_text() if path.exists() else ''
|
|
# Preserve recovery notes and task-specific instructions before our section.
|
|
path.write_text(existing.split(marker)[0] + marker + WORKSPACE_RULES)
|
|
|
|
|
|
def guarded_script(code):
|
|
"""Shadow eda for the submitted script, including computed/aliased property access.
|
|
|
|
The outer invocation retains the real EDA object for the transport. This is
|
|
a workflow guard, not isolation from deliberately hostile JavaScript.
|
|
"""
|
|
return '''const __nativeOnly = (native) => {
|
|
const blocked = ''' + json.dumps(BLOCKED_METHODS) + ''';
|
|
const cache = new Map();
|
|
return new Proxy(native, {get(target, group) {
|
|
const api = Reflect.get(target, group);
|
|
if (!Object.prototype.hasOwnProperty.call(blocked, group)) return api;
|
|
if (!cache.has(group)) cache.set(group, new Proxy(api, {get(object, method) {
|
|
if (blocked[group].includes(method)) throw new Error('easyeda-native-v1: external layout/routing exchange is disabled: '+String(group)+'.'+String(method)+'. Use native primitives or native routing APIs.');
|
|
const value = Reflect.get(object, method);
|
|
return typeof value === 'function' ? value.bind(object) : value;
|
|
}}));
|
|
return cache.get(group);
|
|
}});
|
|
};
|
|
return await (async (eda) => {
|
|
''' + code + '\n})(__nativeOnly(eda));'
|
|
|
|
|
|
def validate_export_name(name):
|
|
if name.lower().endswith(('.dsn', '.ses')):
|
|
raise ValueError('easyeda-native-v1: DSN/SES routing exchange exports are disabled')
|