15 KiB
name, description
| name | description |
|---|---|
| th1-ai-director | TH1 project workflow for the new AI Director architecture, AI kernel switching, AI action generation, AI Director diagnostics, JSONL log analysis, infinite-loop/card/hero-task/action-repeat triage, and iterative fixes to the simplified non-behavior-tree AI. Use when working on TH1 新AI, AI Director, AI_Director_Diagnostics logs, AI卡死/死循环, AI行为不聪明, AI action filtering, AI replay/testing loops, or replacing/maintaining behavior-tree AI. |
TH1 AI Director
Scope
Use this skill for the new non-behavior-tree AI under Unity/Assets/Scripts/TH1_Logic/AI/Director, AI kernel registration, AI action-pool filtering, and local diagnostic loops.
Always layer these project skills first when editing code:
th1-basefor broad Unity/hotfix constraints.th1-action-logicfor action execution,CheckCan,CompleteExecute, AI action generation, and replay/network implications.th1-hero-implementationif changing hero task semantics or hero gameplay, not just AI filtering.
Key Files
Unity/Assets/Scripts/TH1_Logic/AI/Kernel/- AI kernel abstraction and switching.Unity/Assets/Scripts/TH1_Logic/AI/Director/- new AI Director decision/cache/index/diagnostics implementation.Unity/Assets/Scripts/TH1_Logic/AI/AIActionGenerator.cs- shared AI action candidate generation used by Director and legacy AI paths.Unity/Assets/Scripts/TH1_Logic/AI/AILogic.cs- AI update loop and action execution guard.Unity/Assets/Scripts/TH1_Logic/Core/GameLogic.cs- AI update caller.MD/GameMDFramework/18-AI导演系统策划文档.md- planning/design intent.MD/GameMDFramework/19-AI导演系统逻辑语言.md- pseudocode/logic-language source.MD/GameMDFramework/20-AI导演系统测试与诊断流程.md- logging and testing process.Unity/Logs/AI_Director_Diagnostics/*.jsonl- local debug diagnostic output.
Fast Log Triage
Run the bundled analyzer before reading huge JSONL files manually:
python .codex/skills/th1-ai-director/scripts/analyze_ai_director_log.py
Useful options:
python .codex/skills/th1-ai-director/scripts/analyze_ai_director_log.py --log Unity/Logs/AI_Director_Diagnostics/ai_director_YYYYMMDD_HHMMSS.jsonl
python .codex/skills/th1-ai-director/scripts/analyze_ai_director_log.py --last 50 --top 30
python .codex/skills/th1-ai-director/scripts/analyze_ai_director_log.py --json
For batch-level quality work, use the batch analyzer first:
python .codex/skills/th1-ai-director/scripts/analyze_ai_batch_quality.py
python .codex/skills/th1-ai-director/scripts/analyze_ai_batch_quality.py --batch Unity/Logs/AI_Batch/YYYYMMDD_HHMMSS/batch_summary.json --top 12
python .codex/skills/th1-ai-director/scripts/analyze_ai_batch_quality.py --json
Read the analyzer output in this order:
- Latest log path and event counts.
- Executions by player and max actions per turn.
- Decision/execution action distributions.
- Repeated stable keys.
- No-effect successful actions.
- Last execution rows.
Read the batch analyzer output in this order:
Gamessuccess/failure count and whether any run timed out or was manually interrupted.- Runtime throughput:
avgGame,actions/sec,frames/sec,actions/turn. - Outcome shape:
avgTurn,avgSurvivors, eliminations, and winners if present. - Expansion: alive average city count, all-player average city count, max city count, alive
>=2and>=3city ratios. - Power and attrition: alive unit count, score p10/p50/p90, kills, acting-unit deaths.
- Hero count: read both
HeroSummaryfrombatch_summary.jsonand detailedHero:from JSONL. Prefer hero-eligible ratios when the batch includes non-hero factions. - Action quality: no-effect actions, repeated stable actions, max actions per player turn.
- Decision time: average, p95, max, and top lanes/actions.
Do not judge AI quality from a single interrupted or partial run. The batch analyzer filters incomplete logs when the summary only contains completed games, but final before/after claims should use completed batches with the same options.
Self-Optimization Loop
Use this loop for all long-running AI intelligence work. The goal is not just "no crash"; the AI must become measurably stronger without becoming slower or more brittle.
-
Establish a baseline before changing behavior.
- Use the same map size, player count, turn limit, difficulty, and action budgets that will be used after the patch.
- Save the
Unity/Logs/AI_Batch/<timestamp>/batch_summary.jsonpath and the batch analyzer output. - If there is no usable baseline, run at least one compact baseline batch first.
-
Run a bounded local batch, never an open-ended Unity session.
- Close the visible Unity Editor before batchmode runs.
- Always use explicit
-TimeoutSeconds,-MaxActions, and-MaxActionsPerPlayerTurn. - Prefer
-KeepGoingfor quality loops so one bad game still yields evidence. - If Unity appears slow, inspect
unity_batch.log,batch_summary.json, latest JSONL logs, and the Unity process before killing it. - After killing or interrupting a batch, confirm no
Unity.exeprocess remains.
-
Analyze metrics before reading raw logs.
- Use
analyze_ai_batch_quality.pyfor the batch. - Use
analyze_ai_director_log.pyonly after the batch report points to repeated actions, no-effect actions, outlier players, or slow lanes. - If the log is too sparse to explain the issue, improve diagnostics before guessing at AI logic.
- Use
-
Classify the problem type.
- Correctness: failed games, exceptions, null references, action timeout, action budget stop, no-effect successful actions, illegal repeated actions.
- Intelligence: weak expansion, low city count, poor unit count, bad attrition, city loss, long capital threat, empty threatened cities, obvious idle/economic waste, bad hero task timing, weak hero style expression, weak special-unit skill expression, over-defense, under-attack.
- Performance: low actions/sec, high avg game runtime, high decision p95/max, too many actions per player turn, oversized JSONL output.
- Noise: legal repeated city upgrades, normal tactical move/attack chains, expected Steam warmup messages, incomplete logs from killed batches.
-
Fix in the right layer.
- If the design intent is wrong, update
18-AI导演系统策划文档.mdand19-AI导演系统逻辑语言.mdbefore or alongside code. - If action availability is wrong for every caller, fix
CheckCanor action semantics. - If the action is only wrong for AI, filter/scoring-gate it in AI generation or Director indexing.
- If scoring is wrong, adjust lane priority, target value, or cache features; do not add broad hardcoded hacks that only satisfy one replay.
- If performance is wrong, prefer caching, indexed lookup, action-pool pruning, and cheaper diagnostics before reducing strategic search quality.
- If the design intent is wrong, update
-
Re-run the same batch options and compare.
- A change only counts as an AI improvement when the target metric improves and no core guardrail regresses.
- If a performance shortcut improves p95 but hurts expansion/city count/action quality, revert it or redesign it.
- Keep failed experiments out of the final patch; mention them in the final report only when they explain the chosen direction.
-
Commit only coherent, verified changes.
- Stage AI code, docs, scripts, and diagnostics changes that belong together.
- Do not stage Unity auto-generated side effects such as
Unity.sln,ProjectSettings.asset, orpackages-lock.jsonunless they are intentionally part of the task.
Quality Metrics
Primary guardrails:
failedGamesmust be0for normal quality claims.noEffectshould be0; any nonzero value needs raw-log explanation.repeatedshould be0after excluding known legal repeats such as same-turnCityLevelUpAction:Parkconsuming city upgrade points.maxActions/playerTurnshould stay well below the forced-stop budget; investigate anything above80.- No AI loop, forced AI stop, fatal exception, or unresolved null reference is acceptable.
cityLostandcapitalOwnershipChangedare high-severity intelligence/correctness signals; inspect the TurnStart-to-TurnStart city signature before changing combat weights.
Intelligence targets for compact 17-player, 20x20, 20-turn Director batches:
- Expansion should not trigger
LOW_EXPANSION; targetaliveAvgCities >= 1.35. - Second-city rate should not trigger
FEW_SECOND_CITIES; target alive>=2city ratio at least25%. - Track max city count and alive
>=3city ratio as snowball signals, but do not overfit to one high-roll player. - Unit count and score p10/p50/p90 should not collapse while expansion improves.
- Attrition should be interpreted with context: more kills and more deaths may indicate stronger aggression, not necessarily worse play.
- Defense should be read from
Defense:in the batch analyzer:cityLost=0is the target for short compact batches; risingcapitalThreatTurns,emptyThreatTurns, orworsened > resolvedmeans Emergency, city production, or Hold/Front logic needs review. - Hero count should be read from
HeroSummaryfirst: in 17-player 20-turn batches, many players may be non-hero factions, so useeligibleAvgSelected,eligibleAvgSpawned,eligibleAvgMaxSlots,eligibleSpawned>=1,eligibleSpawned>=2, andeligibleSelected>=2for hero intelligence. The detailed JSONLHero final countsremains useful for action-level deltas and style buckets. Low eligible hero count means review culture-slot unlock, hero selection timing, and spawn-city pressure before judging hero playbook quality. - Hero style should not collapse to only
General;style bucketsshould show expected class/faction roles such as Defense, Recovery, Burst, Control, Summon, Economy, Mobility, and HeroLifecycle as heroes appear. - Hero personal expression must be checked through
Hero personal expression: compare each hero's role, executions, kills, damage, healing, moves, skill changes, and top actions. A hero that is selected/spawned but only moves or has near-zero damage/healing/control should trigger HeroPlaybook review. - Unit role expression must be checked through
Unit role expression: Mobility should create moves/captures and low waste, Melee should hold line and finish targets, Ranged/Siege should contribute damage with low deaths, Defender should resolve threats without excessive self-deaths, and Special/Summon should show concrete skill/action signatures. - Special-unit expression should be checked through
Unit skill expression:skill-bearing actor actions,signature-changing actions,actor signatures, andskill_signature_expressionreveal whether new faction units are actually participating.
Performance targets:
actions/secshould stay at or above15in compact batches.- Decision p95 above
60msis a warning and above100msshould be treated as an optimization target. - If p95 is high and top slow lanes are Front/Expansion/Emergency, inspect action generation, move lookup,
CheckCan, and world-cache computation before trimming strategic behavior. - Large diagnostic output is acceptable for local debug, but batch analysis JSON should remain compact enough for automated comparison.
Recommended batch commands:
# Fast smoke after compile-sensitive changes.
Tools/RunAIDirectorBatch.ps1 -Games 1 -Players 2 -Turns 1 -TimeoutSeconds 60
# Compact quality loop for before/after comparison.
Tools/RunAIDirectorBatch.ps1 -Games 3 -Players 17 -Width 20 -Height 20 -Turns 20 -TimeoutSeconds 420 -MaxActions 9000 -MaxActionsPerPlayerTurn 120 -Difficulty LUNATIC -KeepGoing
# Larger confidence loop when compact metrics look good.
Tools/RunAIDirectorBatch.ps1 -Games 5 -Players 17 -Width 30 -Height 30 -Turns 30 -TimeoutSeconds 900 -MaxActions 16000 -MaxActionsPerPlayerTurn 160 -Difficulty LUNATIC -KeepGoing
Infinite Loop Triage
Classify the repeated action before patching:
- If
netActionDelta=1and all gameplay deltas are zero, the action is probably not a valid AI autonomous action. Filter it fromAIActionGeneratorand, if needed, fromAIDirectorActionIndex.IsDangerousAction. - If the action mutates state but remains repeatedly high priority, fix the Director lane priority or add once-per-turn/action-budget gating.
- If
CompleteExecutereturns true butCheckCanremains true forever because the action's own state is not consumed, inspect the action layer. Do not change authoritative action semantics unless the action is actually wrong for player/network/replay use. - If an action is player-only, UI-only, debug-only, or a hidden milestone/card/task helper, prefer excluding it from AI candidate generation.
- If the action should be AI-usable long term but currently has missing scoring conditions, temporarily filter it and leave a concise code comment; then update 18/19 before re-enabling.
Known temporary filters from this iteration:
CommonActionType.BuyCultureCardis only generated forSecondHeroandThirdHeroslot unlock cards; other culture cards remain disabled for AI generation.UnitActionType.ToggleShenlanis filtered because it is a debug/visual toggle with no action-point cost.PlayerActionType.FinishHeroTaskis filtered because it can execute repeatedly without observable AI turn progress.
Candidate Filtering Rules
Keep filters near shared AI generation when the action should not be available to either Director or legacy behavior-tree AI.
Use AIDirectorActionIndex.IsDangerousAction as a second safety net for Director-specific indexing and fallback selection.
Do not fix AI loops by changing CheckCan unless the action is invalid for all callers. Player/UI, network, replay, and scripted effects share the action layer.
Diagnostic Expectations
A useful AI diagnostic log should explain:
- Session and player turn boundaries.
- Decision lane, priority, reason, fallback flag, stable action key.
- Action pool counts by category.
- World cache snapshot: posture, city threats, fronts, development targets, local battles, hero states, diplomacy.
- Execution before/after/delta snapshots.
- Guard/forced-stop reason when action count budget is exceeded.
When adding diagnostics, keep them editor/debug gated. Do not add game-facing text.
Verification
After AI or action filtering changes, run:
dotnet build Unity/TH1.Hotfix.csproj --no-restore
For log-only script changes, run:
python .codex/skills/th1-ai-director/scripts/analyze_ai_director_log.py --top 10 --last 10
For local autonomous smoke runs, close the Unity Editor first, then run:
Tools/RunAIDirectorBatch.ps1 -Games 1 -Players 2 -Turns 1 -TimeoutSeconds 60
The runner writes batch_summary.json under Unity/Logs/AI_Batch/<timestamp>/. Use -Games 10, higher -Turns, and the normal 17 players for larger AI quality loops.
Tools/RunAIDirectorBatch.ps1 defaults to not stopping on settlement/game-end checks so quality loops can run to the requested -Turns even if a settlement winner appears early. Pass -StopOnGameEnd only when validating settlement/endgame behavior.