Skip to content

refactor(reward-penalties): share one malformed-think-tag predicate between the echo and capture paths #5008

Open

Description

@bg51717

Follow-up from a #4776 review thread.

Background

The malformed-think-tag penalty is implemented twice:

The two are kept in sync only by test_malformed_think_tag_parity (6 turn patterns x with/without IDs) in tests/unit/experience/test_capture_reward_penalties.py.

Proposal

  1. Have apply_reward_penalties call has_malformed_think_tag (or one shared ID predicate plus one text predicate) per (user, assistant) turn, so both paths run the same rules and the parity test can go. Gym's message-level has_malformed_thinking flag stays an echo-only extra check.
  2. Fix the case below in the shared predicate.

Missed case: a spelled-out </think> in a thinking-off turn

Reported by terrykong in #4776 (comment). With tag IDs known and thinking off, the ID check expects 0 close tags, but the text check only fails on more than 1. A model that writes </think> from ordinary tokens passes both:

new prompt (carry):  ... <think></think>          empty pair → thinking off
model wrote:         "Answer" "</" "think" ">"    no tag token, just text pieces
decoded text:        "Answer</think>"

ID check:    expect 0 open, 0 close; generation has 0 open IDs, 0 close IDs → passes
text check:  "<think>" 0 times → passes; "</think>" 1 time, fails only on > 1 → passes
result:      not penalized

Both paths behave this way (has_malformed_think_tag returns False on this input; echo step 4b has the same > 1 rule).

Suggested rule: when the IDs are known, compare the decoded close-tag count with the count the ID check expects (0 or 1); keep > 1 only when the IDs are unknown. The counts line up because has_malformed_think_tag decodes with skip_special_tokens=False, so a real close-tag token also shows up in the text.

Activity

  1. self-assigned this
    on Oct 31, 2026
  2. added
    bug
    on Oct 31, 2026
  3. added
    enhancement
    and removed
    bug
    on Oct 31, 2026
  4. four1er commented on Nov 23, 2026

    @four1er

    I'm working on this — sharing one malformed-think-tag predicate between the echo path (apply_reward_penalties) and the capture path (has_malformed_think_tag), and fixing the spelled-out </think> miss in thinking-off turns. I will open a PR with the refactor and regression tests.

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

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions