File size: 9,236 Bytes
8f447ab
5844f89
8f447ab
947e6ce
 
 
 
 
 
8f447ab
 
 
 
 
 
947e6ce
8f447ab
 
 
 
 
31f0e47
8f447ab
 
ac9dd65
8f447ab
 
947e6ce
ac9dd65
947e6ce
 
 
 
 
8f447ab
947e6ce
 
 
 
 
8f447ab
31f0e47
8f447ab
 
ac9dd65
 
 
 
 
 
 
710a337
 
 
 
 
 
ac9dd65
 
8f447ab
ac9dd65
 
 
 
 
 
 
8f447ab
 
 
0b538e5
ac9dd65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f447ab
0b538e5
ac9dd65
 
 
 
 
 
 
 
 
8f447ab
 
ac9dd65
8f447ab
 
947e6ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c074bf7
 
8f447ab
 
 
c074bf7
 
 
31f0e47
 
 
c074bf7
8f447ab
366df60
947e6ce
 
8f447ab
 
947e6ce
8f447ab
 
947e6ce
 
 
 
366df60
8f447ab
 
947e6ce
 
366df60
 
8f447ab
ac9dd65
8f447ab
947e6ce
8f447ab
0b538e5
ac9dd65
 
 
8f447ab
947e6ce
 
8f447ab
ac9dd65
8f447ab
 
 
947e6ce
366df60
 
 
 
 
 
 
 
 
947e6ce
 
 
 
 
 
 
ac9dd65
 
 
 
 
 
 
947e6ce
 
366df60
 
 
 
 
 
 
 
 
947e6ce
 
 
 
 
8f447ab
 
947e6ce
819f3dc
 
947e6ce
31f0e47
947e6ce
 
 
819f3dc
31f0e47
947e6ce
 
8f447ab
947e6ce
366df60
8f447ab
 
31f0e47
947e6ce
 
 
 
8f447ab
947e6ce
31f0e47
8f447ab
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
inference.py - Business Simulation Environment
================================================
Runs the LLM agent through all 3 tasks and emits structured OpenEnv stdout logs.

Standard Log Format:
    [START] task=<task> env=<benchmark> model=<model>
    [STEP]  step=<n> action=<action> reward=<0.00> done=<true|false> error=<msg|null>
    [END]   success=<true|false> steps=<n> score=<0.000> rewards=<r1,r2,...>
"""

import os
import re
import json
import textwrap
from typing import List, Optional

from openai import OpenAI
from src.business_sim_env import BusinessSimEnv
from src.models import CEOAction

# -- Configuration -------------------------------------------------------------
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
API_KEY      = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
MODEL_NAME   = os.getenv("MODEL_NAME", "openai/gpt-oss-20b")
ENV_URL      = os.getenv("ENV_URL", "http://localhost:7860")

BENCHMARK    = "business-sim"
MAX_STEPS    = 12
TEMPERATURE  = 0.2
MAX_TOKENS   = 300

SUCCESS_THRESHOLD = 0.5
FALLBACK_ACTION   = CEOAction()

TASKS = [
    "single_quarter_survival",
    "four_quarter_growth",
    "adversarial_resilience",
]

# -- Prompts -------------------------------------------------------------------

SYSTEM_PROMPT = textwrap.dedent("""
    You are the CEO of a software company. Your goal is to MAXIMIZE your score (0.0 to 1.0) in a business simulation.
    
    GRADING CRITERIA (VERY IMPORTANT):
    - Task 'single_quarter_survival': Score = (Final Budget - 100,000) / 50,000. Use PREMIUM stack to reach 150k.
    - Task 'four_quarter_growth': Score = 70% Growth + 30% Reputation. Target 30% growth.
    - Task 'adversarial_resilience': Score = Survival (25%) + Profit (25%) + Reputation (20%) + Low Burnout (15%) + Rep Target Bonus (15%).

    TASK-SPECIFIC MANUAL (VICTORY LAP STRATEGY):
    - QUARTERS 1-6: Stay safe. Follow the $25k Floor. Fire 3 devs. Maintain 80% Success Rate.
    - QUARTERS 7-8 (THE VICTORY LAP): **Safety Rules are now DEACTIVATED.**
    - ACTION: Locate the highest PROFIT and REPUTATION project available. Accept it even if it is risky. 
    - GOAL: We need a vertical spike in the graph. If you won $50k earlier, aim for a $100k win now.
    - REPUTATION TOP-OFF: In Q7, if Budget > $100k, spend $20k on Training to "Pump" your final Reputation score for the grader.
    
    JSON response format:
    {
      "thought_process": "Analyze status vs goal.",
      "accept_project_id": "<id or null>",
      "hire_count": 0,
      "fire_count": <0-3>,
      "training_budget": <0-50000>,
      "tech_stack": "premium",
      "reduce_workload": false
    }
""").strip()

def build_user_prompt(step: int, obs, task_id: str) -> str:
    analyzed_projects = []
    for p in obs.available_projects:
        skill_gap = max(0.0, p.skill_required - obs.team.skill)
        burnout_factor = obs.team.burnout * 0.15
        est_risk = p.base_risk + (skill_gap * 0.5) + burnout_factor + (p.hidden_risk * 0.5)
        success_prob = max(0.05, min(0.95, 1.0 - est_risk))
        
        # Calculate Estimated ROI
        potential_profit = p.base_profit * 1.0 # Base
        if success_prob > 0.85: potential_profit *= 1.2 # Assume Premium
        roi = (potential_profit - p.resource_cost) / (p.resource_cost + (obs.team.size * 8000))

        analyzed_projects.append(
            f"- {p.id}: {p.name}\n"
            f"   [ANALYSIS] Success={success_prob:.0%} | ROI={roi:.2f} | Profit=${p.base_profit:,.0f}\n"
            f"   [DETAILS] Risk={p.base_risk:.2f} | SkillReq={p.skill_required:.2f} | Domain={p.domain}"
        )
    
    projects_text = "\n".join(analyzed_projects) or "(none available)"
    demand_text = ", ".join(f"{k}: {v:.1f}x" for k, v in obs.domain_demand.items())
    
    return textwrap.dedent(f"""
        --- !!! MISSION: {task_id.upper()} !!! ---
        --- STATUS UPDATE ---
        QUARTER: {obs.quarter} / {obs.max_quarters}
        FINANCIALS: Budget=${obs.budget:,.0f} | Rep={obs.reputation:.2f}
        TEAM: Size={obs.team.size} | Skill={obs.team.skill:.2f} | Burnout={obs.team.burnout:.2f}
        MARKET: {obs.market_phase.value} | Demand: {demand_text}
        FEEDBACK: {obs.last_action_result or "Initial Quarter"}
        ACTIVE RISKS: {", ".join(obs.active_risks) or "None"}

        AVAILABLE PROJECTS (Ranked by Risk/Reward):
        {projects_text}

        Reply with the JSON object including a "thought" field explaining your move.
    """).strip()

# -- Logging Helpers -----------------------------------------------------------

def log_start(task: str) -> None:
    print(f"[START] task={task} env={BENCHMARK} model={MODEL_NAME}", flush=True)

def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    error_val = str(error) if error else "null"
    done_val  = str(done).lower()
    # Using simple action description for the log
    print(f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}", flush=True)

def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    success_val = str(success).lower()
    print(f"[END] success={success_val} steps={steps} score={score:.3f} rewards={rewards_str}", flush=True)

# -- Logic ---------------------------------------------------------------------

def parse_action(response_text: str, observation) -> CEOAction:
    action = FALLBACK_ACTION
    try:
        clean = re.sub(r"```(?:json)?|```", "", response_text).strip()
        match = re.search(r"\{.*\}", clean, re.DOTALL)
        if match:
            data = json.loads(match.group())
            action = CEOAction(**data)
    except:
        pass
    
    return action

def run_task(client: OpenAI, task_id: str, callback=None) -> None:
    log_start(task_id)
    
    env = BusinessSimEnv.from_docker_image(
        image    = "business-sim-env:latest",
        env_vars = {"BUSINESS_SIM_TASK": task_id, "BUSINESS_SIM_URL": ENV_URL}
    )

    rewards = []
    steps_taken = 0
    success = False
    score = 0.5
    thought = ""

    try:
        result = env.reset()
        obs = result.observation
        
        if callback: callback({"step": 0, "obs": obs, "thought": "Initializing CEO Agent..."})

        hint = None
        for step in range(1, MAX_STEPS + 1):
            steps_taken = step
            try:
                user_msg = build_user_prompt(step, obs, task_id)
                if hint:
                    user_msg += f"\n\nCRITICAL HINT FROM PREVIOUS FAILURE: {hint}"

                completion = client.chat.completions.create(
                    model    = MODEL_NAME,
                    messages = [
                        {"role": "system", "content": SYSTEM_PROMPT},
                        {"role": "user",   "content": user_msg},
                    ],
                    temperature = TEMPERATURE,
                )
                response = completion.choices[0].message.content or ""
                
                # Extract thought for UI
                try:
                    clean = re.sub(r"```(?:json)?|```", "", response).strip()
                    match = re.search(r"\{.*\}", clean, re.DOTALL)
                    thought = json.loads(match.group()).get("thought_process", "")
                except:
                    thought = response[:200]

                action = parse_action(response, obs)
                
                result = env.step(action)
                obs = result.observation
                reward = float(result.reward)
                done = bool(result.done)

                # Fetch hint for next step (Counterfactuals)
                try:
                    state_data = env.get_state()
                    hint = state_data.get("counterfactual_hint")
                except:
                    hint = None

                rewards.append(reward)
                log_step(step, "ceo_decision", reward, done, None)
                
                if callback:
                    callback({
                        "step": step, 
                        "obs": obs, 
                        "thought": thought, 
                        "reward": reward,
                        "action": action.model_dump()
                    })

                if done:
                    break
            except Exception as e:
                log_step(step, "error", 0.0, True, str(e))
                break

        # Final grade
        try:
            raw_score = env.grade()
            score = max(0.01, min(0.99, float(raw_score)))
        except:
            score = 0.5
        
        success = score >= SUCCESS_THRESHOLD

    except Exception as e:
        score = 0.1
        success = False
    finally:
        log_end(success, steps_taken, score, rewards)
        if callback: callback({"done": True, "score": score, "success": success})
        env.close()

def main():
    if not API_KEY:
        print("[ERROR] HF_TOKEN / API_KEY missing.")
        return

    client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
    for task_id in TASKS:
        run_task(client, task_id)

if __name__ == "__main__":
    main()