File size: 2,746 Bytes
f96f9fe
 
 
9d365e4
fc1a91c
f96f9fe
fc1a91c
 
 
 
 
f96f9fe
fc1a91c
 
7b090a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d365e4
fc1a91c
 
 
 
9d365e4
 
c86defe
 
 
 
9d365e4
c86defe
 
 
 
 
7b090a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dedee3a
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
import json
import re
from core.schemas import TaskSpec, ExecutorAction
from utils.llm_client import complete as llm_complete, active_backend
from core.llm_client import call_llm

EXECUTOR_SYSTEM = """Procurement compliance analyst at AXIOM Corp (aerospace/defense).
Detect violations. Frameworks: FAR, DFARS, EU Directive 2014/24/EU.
Violations: expired SAM.gov registration, undisclosed OCI, missing mandatory clauses, ITAR breach, defective cost pricing, debarred vendor, TINA threshold breach.
Return JSON only:
{"decision":"COMPLIANT|NON-COMPLIANT","violations_found":["FAR X.XXX - reason"],"reasoning":"...","confidence":0.0}"""

# Legacy alias
EXECUTOR_SYSTEM_PROMPT = EXECUTOR_SYSTEM


def act(
    task: TaskSpec,
    world_state_text: str,
    feedback: str = None,
    attempt_number: int = 1,
) -> ExecutorAction:
    prompt = (
        f"WORLD STATE:\n{world_state_text}\n\n"
        f"TASK CONTEXT:\n{task.scenario_context}\n\n"
        f"CONTRACT/DOCUMENT:\n{task.contract_text or 'No document provided'}\n\n"
    )
    if attempt_number > 1 and feedback:
        prompt += f"ARBITER FEEDBACK:\n{feedback}\n\n"
    prompt += "Provide your analysis in the required JSON format."

    try:
        try:
            raw = call_llm([{"role": "user", "content": prompt}], agent="executor", system=EXECUTOR_SYSTEM)
        except Exception:
            raw = llm_complete(EXECUTOR_SYSTEM_PROMPT, prompt, max_tokens=600)
        raw = re.sub(r"```json|```", "", raw).strip()
    except Exception as exc:
        return ExecutorAction(
            task_id=task.task_id,
            attempt_number=attempt_number,
            decision="PARSE_ERROR",
            reasoning=f"Executor LLM call failed: {type(exc).__name__}: {exc}",
            confidence=0.0,
            violations_found=[],
            supporting_evidence=[],
            recommendation=None,
        )

    try:
        data = json.loads(raw)
        return ExecutorAction(
            task_id=task.task_id,
            attempt_number=attempt_number,
            decision=data.get("decision", "NON-COMPLIANT"),
            reasoning=data.get("reasoning", ""),
            violations_found=data.get("violations_found", []),
            supporting_evidence=data.get("supporting_evidence", []),
            confidence=float(data.get("confidence", 0.5)),
            recommendation=data.get("recommendation"),
        )
    except Exception:
        return ExecutorAction(
            task_id=task.task_id,
            attempt_number=attempt_number,
            decision="PARSE_ERROR",
            reasoning="Failed to parse response",
            confidence=0.0,
            violations_found=[],
            supporting_evidence=[],
            recommendation=None,
        )