Skip to content

feat(analysis): add token entropy and stagnation monitor - #783

Merged
SHAURYASANYAL3 merged 2 commits into
Shinraya-Tech-Labs:mainfrom
Anshuu2004:feat/issue-760-token-entropy-rebased
Oct 26, 2026
Merged

SHAURYASANYAL3 merged 2 commits into
Shinraya-Tech-Labs:mainfrom
Anshuu2004:feat/issue-760-token-entropy-rebased

Conversation

@Anshuu2004

Copy link
Copy Markdown
Contributor

📌 Description

Adds TokenEntropyMonitor, an analyzer that watches a run's model calls for two kinds of generation stagnation, and makes the OpenAI normalizer keep optional logprobs so there is something to measure. NSoC 26, level2.

  • agentwatch/events/normalizers/llm_api.py: new token_logprobs() helper. OpenAINormalizer.map_io stores the first choice's per-token logprobs (logprob and top_logprobs only, the token text is deliberately not stored so it cannot sit in event attributes outside the redaction path) in attributes["logprobs"] when the provider sent them. When it did not, the event is exactly what it was before.
  • agentwatch/analysis/entropy.py: TokenEntropyMonitor(window=32, threshold=0.05, min_repeats=3).
    • ENTROPY_COLLAPSE: the mean Shannon entropy (bits) of the next-token distribution over a sliding window of tokens drops below threshold. Per token, the top-k logprobs are exponentiated and renormalized to sum to 1, then H = -sum(p log2 p). The window runs across turns of a run. It reports once per collapse and re-arms when the mean recovers.
    • STAGNATION_LOOP: the same completion (content hash) in min_repeats consecutive model calls. Needs no logprobs, so it is also the fallback when a provider sends none.
    • Events without logprobs contribute no entropy samples and never raise anything on their own.
  • agentwatch/analysis/registry.py: registered in builtin_analyzers(). Note this changes the engine pipeline identity (analyzer set is part of the interpretation id).
  • Records are emitted like the other v3 analyzers (derived records, record_type = "token_entropy_anomaly") with anomaly_type, turn_index, events, the measured values and an explanation. They are not legacy EventType.ANOMALY_DETECTED events.

Not implemented (honest scope note): the issue's second requirement also mentions erratic divergence spikes (sudden uncertainty jumps during deterministic steps), and my plan comment said I would flag a jump against the window mean. I did not build that. This PR only emits the two anomaly types listed in requirement 3 (ENTROPY_COLLAPSE, STAGNATION_LOOP). Also not covered: n-gram level repetition inside one completion, streaming responses, providers other than the OpenAI response shape. Happy to do the spike rule as a follow-up PR if you want it.

Fixes #760


🏛️ Architectural Plane & Classification

  • Plane A: Evidence Plane (Sensors, RawObservation, Ingest, Redaction)
  • Plane B: Interpretation Plane (Normalizers, ComputationalEvent, Entity Resolution)
  • Plane C: Structure Plane (Hyperedge Store, Execution/Information/Causal Graphs, Provenance)
  • Plane D: Model Plane (Latent States, Motifs, Behavioral Genome, Drift)
  • Plane E: Experiment Plane (Replay L0–L4, Timelines, Counterfactuals)
  • Plane F: Interface Plane (API, Typer CLI, Next.js Frontend)
  • Documentation / CI Maintenance

🧪 Verification & Evidence of Testing

# Not run yet. I could not run pytest, ruff or bandit in my setup before opening this.
# Opening as a draft so the repo CI runs them; I will fix whatever comes back red and
# paste real output here before marking it ready.
  • New unit tests or regression tests were added covering this logic. (tests/v3/test_entropy.py: entropy math, window sliding, collapse and re-arm, stagnation, missing logprobs, config validation, normalizer passthrough, engine round trip)
  • Existing test suite passes with zero regressions (pytest tests/).
  • Test coverage does not decrease (satisfies >= 70% coverage gate).

🛡️ Security & Linter Quality Checklist

  • Bandit Scan Passed: Ran bandit -r agentwatch with 0 issues found.
  • No Baseline File: Confirmed bandit-baseline.json was NOT created or edited. (the diff touches 4 files, none of them a baseline)
  • Inline Suppressions (If applicable): not applicable, no # nosec added.
  • Ruff Format: Ran ruff format --check agentwatch/ (Clean pass).
  • Ruff Check: Ran ruff check agentwatch/ (Clean pass).
  • Plane Boundaries: Verified lower planes do not import from higher planes.

✍️ Contributor Integrity Declaration

By submitting this pull request, I certify that:

  1. I have been formally assigned to this issue by a maintainer.
  2. I personally understand and wrote or thoroughly verified every line of this code.
  3. This PR contains no unreviewed, hallucinated, or unverified AI-generated slop.
  4. My branch is cleanly rebased on the latest origin/main.

Discord membership proof

Discord handle: @abhishekcse07

Discord membership screenshot

Keep optional OpenAI logprobs on model-invocation events and add
TokenEntropyMonitor, a built-in analyzer that flags ENTROPY_COLLAPSE
(sliding-window Shannon entropy) and STAGNATION_LOOP (identical
completions across consecutive turns). Fixes Shinraya-Tech-Labs#760
Entropy only needs the numbers. The token strings would sit in event
attributes next to the redacted artifacts, so drop them.

Fixes Shinraya-Tech-Labs#760
@github-actions

Copy link
Copy Markdown
Contributor

🧪 PR Test Results

Check Result
Tests (pytest tests/) ✅ success
Lint (ruff check .) ❌ failure
Coverage (agentwatch) 79.70%

Python 3.12 · commit 9542ffa

@SHAURYASANYAL3
SHAURYASANYAL3 marked this pull request as ready for review October 26, 2026 06:21
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-26T06:22:20.938303Z
We couldn’t complete this review. Comment @codex review to try again.
9542ffa Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@SHAURYASANYAL3 SHAURYASANYAL3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👁️ Multi-Turn Token Entropy & Semantic Drift Review: PR #783 (TokenEntropyMonitor) — APPROVED ✅

Outstanding implementation by @Anshuu2004 on this Level 2 milestone!

Architectural Highlights:

  1. Defensive Redaction-First Logprob Capture: In agentwatch/events/normalizers/llm_api.py, captures only numerical logprobs and top_logprobs distributions without persisting raw token text outside the redaction path.
  2. Shannon Entropy Math & Re-Arming: Accurately computes Shannon entropy $H = -\sum p \log_2 p$ over a sliding window across multi-turn completions, detecting ENTROPY_COLLAPSE with single-report re-arming semantics to avoid alert flooding.
  3. Stagnation Loop Fallback: Implements deterministic completion content hashing for STAGNATION_LOOP detection, functioning reliably even when LLM providers do not return logprob distributions.
  4. Clean Registry Integration: Correctly registered in agentwatch/analysis/registry.py under builtin_analyzers().
  5. 100% Green CI: All 12 checks passed (3m39s test suite, 79.70% coverage, 0 regressions).

Merging into main! 🚀

@SHAURYASANYAL3
SHAURYASANYAL3 merged commit 278e8ed into Shinraya-Tech-Labs:main Oct 26, 2026
14 checks passed
@SHAURYASANYAL3 SHAURYASANYAL3 added the soc26 Nexus Spring of Code 2026 label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level2 NSoC 26' soc26 Nexus Spring of Code 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Analysis] Multi-Turn Token Entropy & Generation Stagnation Monitor [Level 2]

2 participants