rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98 Download lambdas/codegen/assembler.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 4.76 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/lambdas/codegen/assembler.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/lambdas/codegen/assembler.py
-
curl -L -o assembler.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/lambdas/codegen/assembler.py
4.76 kB
| """ | |
| ALLI Codegen Subpackage: PEP 8 Script, requirements.txt & Dockerfile Assembler. | |
| """ | |
| from typing import Any, Dict, List | |
| from language_core.codegen_contracts import ( | |
| BANNED_IMPORTS, | |
| PINNED_PACKAGE_VERSIONS, | |
| STANDARD_LIBS, | |
| compute_codegen_hash, | |
| inspect_generated_ast_security, | |
| validate_generated_ast, | |
| ) | |
| from lambdas.codegen.context import CodegenContext | |
| def assemble_python_artifacts( | |
| cg: CodegenContext, | |
| name: str, | |
| clean_filename: str, | |
| ordered_ids: List[str], | |
| graph_fingerprint: str, | |
| pin_exact: bool, | |
| ) -> Dict[str, Any]: | |
| """Assemble final PEP 8 script, pinned requirements, Dockerfile, and AST diagnostics.""" | |
| file_lines = [ | |
| '"""', | |
| f"Workflow Pipeline: {name}", | |
| "Auto-generated by ALLI (Advanced Logic & Linked Intelligence)", | |
| '"""', | |
| "", | |
| ] | |
| std_imports = sorted(m for m in cg.discovered_modules if m in STANDARD_LIBS and m not in BANNED_IMPORTS) | |
| ext_imports = sorted(m for m in cg.discovered_modules if m not in STANDARD_LIBS and m not in BANNED_IMPORTS) | |
| for m in std_imports: | |
| file_lines.append(f"import {m}") | |
| if std_imports and ext_imports: | |
| file_lines.append("") | |
| for m in ext_imports: | |
| file_lines.append(f"import {m}") | |
| if cg.discovered_modules: | |
| file_lines.extend(["", ""]) | |
| else: | |
| file_lines.append("") | |
| file_lines.extend(["def main():", ' """Execute the compiled DAG pipeline steps."""']) | |
| if "math" in cg.discovered_modules: | |
| file_lines.append(" import math") | |
| file_lines.extend(cg.code_body_lines) | |
| file_lines.append("") | |
| file_lines.append(f" return {cg.last_return_var}" if cg.last_return_var else " return None") | |
| file_lines.extend(["", "", 'if __name__ == "__main__":', " main()", ""]) | |
| python_code = "\n".join(file_lines) | |
| clean_reqs = sorted(r for r in cg.discovered_requirements if r not in STANDARD_LIBS and r not in BANNED_IMPORTS) | |
| if clean_reqs: | |
| req_lines = [] | |
| for r in clean_reqs: | |
| if any(op in r for op in ("==", ">=", "<=", "~=", "!=")): | |
| req_lines.append(r) | |
| elif r.lower() in PINNED_PACKAGE_VERSIONS: | |
| req_lines.append(f"{r}=={PINNED_PACKAGE_VERSIONS[r.lower()]}") | |
| elif pin_exact: | |
| req_lines.append(f"{r}==0.1.0") | |
| else: | |
| req_lines.append(f"{r}>=0.1.0") | |
| requirements_txt = "\n".join(req_lines) + "\n" | |
| else: | |
| requirements_txt = "# Standard library only. No external pip packages needed.\n" | |
| dockerfile = ( | |
| "FROM python:3.12-slim\n" | |
| "WORKDIR /app\n" | |
| "COPY requirements.txt .\n" | |
| "RUN pip install --no-cache-dir -r requirements.txt\n" | |
| "COPY . .\n" | |
| "RUN useradd -m appuser && chown -R appuser:appuser /app\n" | |
| "USER appuser\n" | |
| f'CMD ["python", "{clean_filename}"]\n' | |
| ) | |
| is_valid_ast, ast_err = validate_generated_ast(python_code, check_security=True) | |
| security_diags = inspect_generated_ast_security(python_code) | |
| cg.diagnostics.extend(security_diags) | |
| if not is_valid_ast and not security_diags: | |
| cg.diagnostics.append({ | |
| "code": "SYNTAX-001", | |
| "severity": "error", | |
| "message": ast_err or "Synthesized Python code failed AST parse validation", | |
| }) | |
| has_errors = (not is_valid_ast) or any(d.get("severity") == "error" for d in cg.diagnostics) | |
| if has_errors: | |
| python_code = "" | |
| codegen_hash = compute_codegen_hash({ | |
| "python_code": python_code, | |
| "code": python_code, | |
| "requirements_txt": requirements_txt, | |
| "dockerfile": dockerfile, | |
| }) | |
| from language_core.workspace_env_contracts import ( | |
| DEFAULT_DOCKERIGNORE_CONTENT, | |
| generate_env_example, | |
| ) | |
| return { | |
| "success": is_valid_ast and not any(d.get("severity") == "error" for d in cg.diagnostics), | |
| "python_code": python_code, | |
| "code": python_code, | |
| "requirements_txt": requirements_txt, | |
| "dockerfile": dockerfile, | |
| "env_example": generate_env_example(sorted(cg.referenced_env_keys)), | |
| "dockerignore": DEFAULT_DOCKERIGNORE_CONTENT, | |
| "filename": clean_filename, | |
| "dependencies": clean_reqs, | |
| "execution_order": ordered_ids, | |
| "diagnostics": cg.diagnostics, | |
| "source_map": cg.source_map, | |
| "generator_version": "1.0.0", | |
| "graph_fingerprint": graph_fingerprint, | |
| "metadata": { | |
| "hash": codegen_hash, | |
| "ast_valid": is_valid_ast, | |
| "node_count": len(ordered_ids), | |
| "step_count": cg.step_counter - 1, | |
| "discovered_modules": sorted(cg.discovered_modules), | |
| }, | |
| } | |