Add execution-model decision handoff and Jev AALC research
This commit is contained in:
+41
-20
@@ -14,6 +14,7 @@ from pathlib import Path
|
||||
from mcp_session import Session as Bridge, SessionCancelled, SessionTimeout
|
||||
from game_scenes import battle_is_terminal, battle_is_visible, death_retry_screen
|
||||
from run_state import prepare_run, save_json, record_claim, claim_is_current, complete_run
|
||||
from decision_router import DecisionRouter, DecisionRequired
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
RUNTIME_HOME = Path(os.environ.get('LC_JEV_HOME', str(Path.home() / 'lc_mcp' / 'jev')))
|
||||
@@ -540,7 +541,7 @@ def candidates(state, previous_battle=False, refresh_used=0):
|
||||
|
||||
|
||||
def decide(cfg, state, options, recent):
|
||||
body = {'model': 'jev-1.13-free',
|
||||
body = {'model': cfg['model'],
|
||||
'state': '目标:按用户已配置的队伍和速通规则推进当前镜牢。当前楼层:%s\n当前游戏 OCR(坐标仅供辨识):\n' % state.get('floor', '尚未识别') +
|
||||
'\n'.join('%s @%d,%d' % (i['text'], i['x'], i['y']) for i in text_items(state)
|
||||
if not i['text'].upper().startswith('STEAM')) +
|
||||
@@ -572,7 +573,9 @@ def decide(cfg, state, options, recent):
|
||||
choice = answer.get('choice')
|
||||
if answer.get('type') != 'choice' or choice not in options:
|
||||
raise ValueError('Jev returned an unsupported candidate')
|
||||
return choice, round((time.monotonic() - start) * 1000)
|
||||
return {'choice': choice, 'latency_ms': round((time.monotonic() - start) * 1000),
|
||||
'confidence': answer.get('confidence'), 'probabilities': answer.get('probabilities'),
|
||||
'model': result.get('model') or cfg['model']}
|
||||
|
||||
|
||||
def fixed_fallback(options):
|
||||
@@ -677,9 +680,10 @@ def execute(bridge, action, deadline=None):
|
||||
return call(action['tool'], action.get('args'))
|
||||
|
||||
|
||||
def main():
|
||||
def main(executor_decider=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('config')
|
||||
parser.add_argument('config', nargs='?', help='Optional Jev JSON config; omit for execution-model handoff')
|
||||
parser.add_argument('--decision-engine', choices=('auto', 'jev', 'executor', 'fixed'))
|
||||
parser.add_argument('--steps', type=int, default=250)
|
||||
parser.add_argument('--seconds', type=int, default=1200)
|
||||
mode = parser.add_mutually_exclusive_group()
|
||||
@@ -710,13 +714,15 @@ def main():
|
||||
stable_since = time.monotonic()
|
||||
last_fingerprint = None
|
||||
unknown_since = None
|
||||
decision_fallback_reason = None
|
||||
try:
|
||||
RUNTIME_HOME.mkdir(parents=True, exist_ok=True)
|
||||
cfg = json.loads(Path(args.config).read_text(encoding='utf-8-sig'))
|
||||
cfg = json.loads(Path(args.config).read_text(encoding='utf-8-sig')) if args.config else {}
|
||||
cfg['apiKey'] = os.environ.get('OPENCODE_API_KEY') or cfg.get('apiKey')
|
||||
if not cfg['apiKey'] and os.environ.get('LC_FIXED_FALLBACK') != '1':
|
||||
raise RuntimeError('Set OPENCODE_API_KEY or enable LC_FIXED_FALLBACK=1')
|
||||
if args.decision_engine:
|
||||
cfg['decisionEngine'] = args.decision_engine
|
||||
router = DecisionRouter(cfg, decide, executor_decider=executor_decider,
|
||||
fixed_decider=fixed_fallback, rules=SYSTEM_RULES,
|
||||
force_fixed=os.environ.get('LC_FIXED_FALLBACK') == '1')
|
||||
if STOP_FILE.exists():
|
||||
save_json(status, {'state': 'stopped', 'reason': 'stop_requested'})
|
||||
return
|
||||
@@ -779,6 +785,7 @@ def main():
|
||||
unknown_since = None
|
||||
# User explicitly prescribed the complete battle loop. Only scene
|
||||
# transitions return to Jev; combat does not require new decisions.
|
||||
decision = None
|
||||
if any(re.search(r'CON+ECTING|LOADING', i['text'].upper()) for i in text_items(state)):
|
||||
choice, latency = 'wait', 0
|
||||
elif len(options)==2 and time.monotonic() - unknown_since < 10:
|
||||
@@ -792,21 +799,35 @@ def main():
|
||||
elif 'battle' in options:
|
||||
choice, latency = 'battle', 0
|
||||
else:
|
||||
if os.environ.get('LC_FIXED_FALLBACK') == '1' or decision_fallback_reason:
|
||||
choice, latency = fixed_fallback(options), 0
|
||||
else:
|
||||
try:
|
||||
choice, latency = decide(cfg, state, options, recent)
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code not in (401,403,429):
|
||||
raise
|
||||
decision_fallback_reason='Jev HTTP %d; deterministic fallback for this session' % error.code
|
||||
choice, latency=fixed_fallback(options),0
|
||||
try:
|
||||
decision = router.choose(state, options, recent, run['run_id'])
|
||||
choice, latency = decision.choice, decision.jev_ms
|
||||
except DecisionRequired as required:
|
||||
record = {'state': 'requires_decision', 'step': step, 'run_id': run['run_id'],
|
||||
'decision_engine': 'executor', 'decision_request': required.request,
|
||||
'ocr_ms': observation_ms, 'screen': state.get('screen'),
|
||||
'at': time.strftime('%Y-%m-%dT%H:%M:%S')}
|
||||
if 'failed_jev_ms' in required.request:
|
||||
record['failed_jev_ms'] = required.request['failed_jev_ms']
|
||||
run['status'] = 'requires_decision'
|
||||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||||
save_json(RUNTIME_HOME / 'decision_request.json', required.request)
|
||||
save_json(status, record)
|
||||
with log.open('a', encoding='utf-8') as output:
|
||||
output.write(json.dumps(record, ensure_ascii=False) + '\n')
|
||||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||||
return
|
||||
action = options[choice]['action']
|
||||
record = {'step': step, 'run_id': run['run_id'], 'choice': choice, 'action': action, 'jev_ms': latency,
|
||||
'ocr_ms':observation_ms,'screen': state.get('screen'), 'at': time.strftime('%Y-%m-%dT%H:%M:%S')}
|
||||
if decision_fallback_reason:
|
||||
record['decision_fallback_reason']=decision_fallback_reason
|
||||
record['decision_engine'] = decision.engine if decision else 'rules'
|
||||
if decision:
|
||||
record.update(decision_ms=decision.latency_ms + (decision.failed_jev_ms or 0), confidence=decision.confidence,
|
||||
probabilities=decision.probabilities, model=decision.model)
|
||||
if decision.failed_jev_ms is not None:
|
||||
record['failed_jev_ms'] = decision.failed_jev_ms
|
||||
if decision.fallback_reason:
|
||||
record['decision_fallback_reason'] = decision.fallback_reason
|
||||
if choice == 'run_complete':
|
||||
complete_run(RUNTIME_HOME, run, context, state.get('screen'))
|
||||
record['state'] = 'completed'
|
||||
|
||||
Reference in New Issue
Block a user