Skip to content

[codex] Add structured MCP run logging - #90

Open
CompleteDotTech wants to merge 3 commits into
Gennadiyev:mainfrom
CompleteDotTech:codex/run-logging-smart-polling
Open

[codex] Add structured MCP run logging#90
CompleteDotTech wants to merge 3 commits into
Gennadiyev:mainfrom
CompleteDotTech:codex/run-logging-smart-polling

Conversation

@CompleteDotTech

@CompleteDotTech CompleteDotTech commented May 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • adds a structured JSONL run logger for the MCP bridge with stable event envelopes, monotonic sequence numbers, redaction, response previews, SHA-256 hashes, and optional full-text capture
  • wraps every MCP tool so calls, results, errors, timings, and parent HTTP traffic are recorded under a shared tool_call_id
  • adds log_agent_decision(...) for explicit agent decision annotations before important choices
  • makes get_game_state and mp_get_game_state smart-poll by default through enemy turns and other transient non-actionable states, with opt-out and timeout controls
  • documents logging schema, CLI flags, redaction behavior, and smart polling controls

Research-grade detail in this slice

Each JSONL row includes schema_version, run_id, sequence, UTC timestamp, monotonic_ms, event_type, and relevant tool identifiers. Tool and HTTP result payloads include character length, byte length, SHA-256 hash, bounded preview text, and truncation status. This gives downstream analysis a stable ordering key, wall-clock time, relative timing, integrity checks, and enough payload detail for lightweight review without requiring full response retention by default.

The logger records session_start, tool_call_start, tool_call_result, tool_call_error, http_request, http_response, http_error, state_poll, state_poll_final_format, and agent_decision. Sensitive keys containing authorization, cookie, password, secret, token, api_key, or apikey are redacted recursively before writes.

Validation

  • python3 -m py_compile mcp/server.py mcp/run_logger.py
  • uv run --directory mcp python server.py --help
  • direct smoke test for logger creation, redaction, log_agent_decision, and combat actionability predicates
  • git diff --check

Fixes #10

Token accounting update

  • adds deterministic, versioned token estimates to every JSONL record: input_tokens, output_tokens, tool_response_tokens, hidden_poll_tokens, total_tokens, token_source, tokenizer_name, tokenizer_version, model_family, and estimation_method
  • writes run_<id>.summary.json with rollups by tool, event type, state type, game mode, action category, run phase, and floor where available
  • tracks hidden smart-poll token cost, repeated-state cost, invalid-action cost, largest payloads, replay artifact metadata, and externally supplied usage records
  • adds log_external_token_usage(usage_json) so clients can attach exact provider/model token usage by related_tool_call_id, related_event_id, or another stable id
  • adds configurable tokenizer/model profile flags plus mcp/validate_run_log.py for JSONL, monotonic sequence, token rollup, summary consistency, and deterministic fixture validation

Prompt and turn accounting update

  • adds first-class model_message records through log_model_message(...) with stable message_id, turn_id, turn_index, role/source, content hash/preview metadata, privacy flags, state hash links, related tool/event links, and optional exact token usage
  • adds conversation summary rollups for total prompts, total turns, total messages, model messages, tool-result messages, agent decision messages, average tokens per turn, token share by tool-result and hidden polling, largest prompts, by-turn totals, by-role totals, and by-source totals
  • extends external usage reconciliation to related_message_id as well as existing tool/event ids
  • extends validation for model-message roles, monotonic first-seen turn ordering, message-to-turn linkage, and external-usage-to-message reconciliation

@CompleteDotTech

CompleteDotTech commented May 10, 2026

Copy link
Copy Markdown
Contributor Author

Live validation against the local game install passed after closing and relaunching Slay the Spire 2.

Checked:

  • active installed STS2_MCP.dll matches the pending all-PR DLL (50255b0caf5ffa397aa9ad4ab2dd0b2f13750bfa62548ed581e35f0dbecf2cb2)
  • game API came back on localhost:15526 with 17 endpoints, including /api/v1/wiki
  • [codex] Add structured MCP run logging #90 MCP bridge imported via uv run --directory mcp python server.py --help
  • live bridge calls to get_game_state(format="json", wait_for_actionable=false) and smart get_game_state(... wait_for_actionable=true) both returned status: ok / state_type: menu
  • log_agent_decision(...) wrote an agent_decision record
  • generated JSONL log contained session_start, tool_call_start, tool_call_result, http_request, http_response, state_poll, and agent_decision
  • log sequences were monotonic/unique and configured sensitive metadata was redacted
  • git diff --check remains clean

No follow-up code changes were needed from this test pass.

@CompleteDotTech
CompleteDotTech marked this pull request as ready for review May 10, 2026 00:52
@CompleteDotTech

Copy link
Copy Markdown
Contributor Author

Token accounting implementation and validation added in 73945fd.

Validated locally:

  • python3 -m py_compile mcp/server.py mcp/run_logger.py mcp/token_usage.py mcp/validate_run_log.py
  • uv run --directory mcp python server.py --help
  • python3 mcp/validate_run_log.py --self-test
  • generated a fixture JSONL run log using log_agent_decision, log_external_token_usage, and a synthetic hidden state_poll
  • python3 mcp/validate_run_log.py <fixture run>.jsonl passed and reconciled JSONL totals with run_<id>.summary.json
  • git diff --check

The fixture summary verified total-token consistency, external usage rollups, hidden polling cost, repeated-state cost, replay artifact token metadata, and redaction of sensitive token metadata while preserving accounting fields such as estimated_tokens.

@CompleteDotTech

Copy link
Copy Markdown
Contributor Author

Prompt/turn accounting implementation and validation added in e2b7a02.

Validated locally:

  • python3 -m py_compile mcp/server.py mcp/run_logger.py mcp/token_usage.py mcp/validate_run_log.py
  • uv run --directory mcp python server.py --help
  • python3 mcp/validate_run_log.py --self-test
  • generated a fixture JSONL run log with log_model_message records for system, user, assistant, and tool roles on a shared turn
  • fixture included exact assistant token usage, log_external_token_usage linked by related_message_id, an agent decision annotation, and a synthetic hidden/repeated state_poll
  • python3 mcp/validate_run_log.py <fixture run>.jsonl passed and reconciled JSONL totals with run_<id>.summary.json
  • git diff --check

The fixture verified total prompts, total turns, model/tool message counts, agent decision message count, by-turn message linkage, external usage reconciliation by message id, exact-vs-estimated token source rollups, hidden polling cost, repeated-state cost, and prompt privacy behavior (content_preview=false).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Add run logging and smart state polling for combat turns

1 participant