File size: 4,760 Bytes
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
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),
        },
    }