Skip to content

fix: guard against large output limit causing infinite summarize loop#399

Merged
thdxr merged 1 commit into
anomalyco:devfrom
unkhz:fix/output-limit-summarize-loop
Jun 25, 2025
Merged

fix: guard against large output limit causing infinite summarize loop#399
thdxr merged 1 commit into
anomalyco:devfrom
unkhz:fix/output-limit-summarize-loop

Conversation

@unkhz

@unkhz unkhz commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

Attempt fixing #262

Problem:
The auto summarize logic goes into infinite loop summarizing the previous conversation if model output limit ends up being larger than the context limit.

Easy way to reproduce this is using a local model with such configuration. However, this could happen for other providers as well if the reported limits are faulty.

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "lmstudio": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "http://localhost:8080/v1"
      },
      "models": {
        "qwen/qwen3-30b-a3b": {
          "limit": { "context": 131072, "output": 1000000 }
        },
        "mistralai/devstral-small-2505": {
          "limit": { "context": 131072, "output": 1000000 }
        }
      }
    }
  }
}

Solution:
Use 0 as minimum value for the limit calculation to avoid any possibility of that calculation resulting in negative number.

@bernardolsp

bernardolsp commented Jun 25, 2025

Copy link
Copy Markdown

I have seen a similar issue with devstral via external/not selfhosted API, too

@thdxr
thdxr merged commit f4c0d2d into anomalyco:dev Jun 25, 2025
achembarpu pushed a commit to achembarpu/opencode that referenced this pull request Aug 4, 2025
andreipromarketing-dev pushed a commit to andreipromarketing-dev/opencode that referenced this pull request Apr 7, 2026
…servations (anomalyco#399)

* fix(observe): add 5-layer automated session guard to prevent self-loop observations

observe.sh currently fires for ALL hook events including automated/programmatic
sessions: the ECC observer's own Haiku analysis runs, claude-mem observer
sessions, CI pipelines, and any other tool that spawns `claude --print`.

This causes an infinite feedback loop where automated sessions generate
observations that trigger more automated analysis, burning Haiku tokens with
no human activity.

Add a 5-layer guard block after the `disabled` check:

Layer 1: agent_id payload field — only present in subagent hooks; skip any
         subagent-scoped session (always automated by definition).

Layer 2: CLAUDE_CODE_ENTRYPOINT env var — Claude Code sets this to sdk-ts,
         sdk-py, sdk-cli, mcp, or remote for programmatic/SDK invocations.
         Skip if any non-cli entrypoint is detected. This is universal: catches
         any tool using the Anthropic SDK without requiring tool cooperation.

Layer 3: ECC_HOOK_PROFILE=minimal — existing ECC mechanism; respect it here
         to suppress non-essential hooks in observer contexts.

Layer 4: ECC_SKIP_OBSERVE=1 — cooperative env var any external tool can set
         before spawning automated sessions (explicit opt-out contract).

Layer 5: CWD path exclusions — skip sessions whose working directory matches
         known observer-session path patterns. Configurable via
         ECC_OBSERVE_SKIP_PATHS (comma-separated substrings, default:
         "observer-sessions,.claude-mem").

Also fix observer-loop.sh to set ECC_SKIP_OBSERVE=1 and ECC_HOOK_PROFILE=minimal
before spawning the Haiku analysis subprocess, making the observer loop
self-aware and closing the ECC→ECC self-observation loop without needing
external coordination.

Fixes: observe.sh fires unconditionally on automated sessions (anomalyco#398)

* fix(observe): address review feedback — reorder guards cheapest-first, fix empty pattern bug

Two issues flagged by Copilot and CodeRabbit in PR anomalyco#399:

1. Layer ordering: the agent_id check spawns a Python subprocess but ran
   before the cheap env-var checks (CLAUDE_CODE_ENTRYPOINT, ECC_HOOK_PROFILE,
   ECC_SKIP_OBSERVE). Reorder to put all env-var checks first (Layers 1-3),
   then the subprocess-requiring agent_id check (Layer 4). Automated sessions
   that set env vars — the common case — now exit without spawning Python.

2. Empty pattern bug in Layer 5: if ECC_OBSERVE_SKIP_PATHS contains a trailing
   comma or spaces after commas (e.g. "path1, path2" or "path1,"), _pattern
   becomes empty or whitespace-only, and the glob *""* matches every CWD,
   silently disabling all observations. Fix: trim leading/trailing whitespace
   from each pattern and skip empty patterns with `continue`.

* fix: fail closed for non-cli entrypoints

---------

Co-authored-by: Affaan Mustafa <affaan@dcube.ai>
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026

@masuda-masuo masuda-masuo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

レビューコメント

概要:
が を上回る(または近い)場合に、 が負の数になり、常に が成立して要約ループが無限に発生するバグの修正。

良い点:

  • ✅ 問題の特定と原因説明が的確。特に具体的な再現手順と設定例があるのが良い。
  • ✅ 修正は最小限(4行追加)でリスクが低い。 で下限を0にクランプするアプローチはシンプルかつ正しい。
  • ✅ の nullable ハンドリングは既にされていた。

気になった点:

  • のマジックナンバーは既存コードからの踏襲でこのPRのスコープ外ではあるが、いずれ定数化を検討しても良いかも(例: )。

全体的に問題の特定から修正までクリーンなPRだと思います。LGTM。

@masuda-masuo masuda-masuo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

レビューコメント

概要:
model.info.limit.outputmodel.info.limit.context を上回る(または近い)場合に、context - output が負の数になり、常に tokens > 負の数 が成立して要約ループが無限に発生するバグの修正。

良い点:

  • ✅ 問題の特定と原因説明が的確。具体的な再現手順と設定例があり再現性が高い。
  • ✅ 修正は最小限(4行追加)でリスクが低い。Math.max(..., 0) で下限を0にクランプするアプローチはシンプルかつ正しい。
  • model.info.limit.output ?? 0 の nullable ハンドリングは既にされており型安全。

気になった点:

  • 0.9 のマジックナンバーは既存コードからの踏襲でこのPRのスコープ外ではあるが、いずれ定数化を検討しても良いかも。

全体的に問題の特定から修正までクリーンなPRだと思います。LGTM。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants