Skip to content

harbor: count reasoning tokens a trace logs apart - #1782

Merged
burtenshaw merged 4 commits into
openenv:mainfrom
younesbelkada:harbor/atif-reasoning-tokens
Dec 1, 2026
Merged

burtenshaw merged 4 commits into
openenv:mainfrom
younesbelkada:harbor/atif-reasoning-tokens

Conversation

@younesbelkada

@younesbelkada younesbelkada commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

reconcile compares each captured call's sampled token count with the ATIF step's completion_tokens. Harbor's opencode trace counts only the visible answer there and logs the thinking in metrics.extra.reasoning_tokens. The engine sampled both, so with a thinking model every call differs by its reasoning: the rollout fails the check and can't be trained on. This adds the logged reasoning back, but only when that makes the counts line up.

Type of Change

  • Bug fix

What happened without it

opencode with a thinking model in GRPO training: every rollout failed the ATIF cross-check, and none was trainable. The captured counts were exactly completion_tokens + reasoning_tokens per call.

Changes

  • atif.py: _with_reasoning_tokens adds each step's metrics.extra.reasoning_tokens to its completion_tokens. reconcile uses the sums only when they equal the captured counts or are a subsequence of them (the existing auxiliary-call path). Otherwise the original counts are kept and the existing failures apply.
  • Unchanged cases: traces without extra.reasoning_tokens and non-thinking runs give the same counts as before.

Test Plan

Three tests added to tests/envs/test_harbor_reconcile.py:

  • reasoning logged apart still reconciles;
  • the same with an auxiliary call in between;
  • a difference that reasoning doesn't explain still fails.

The first two fail on main.

PYTHONPATH=src:envs uv run pytest tests/envs/test_harbor_reconcile.py tests/envs/test_harbor_aux_masking.py tests/envs/test_harbor_aux_token_count.py

55 passed. ruff format --check, ruff check and usort check are clean.

Used in production as a runtime patch for opencode GRPO training with a thinking model.

RFC Status

  • Not required (bug fix, docs, minor refactoring)

🤖 Generated with Claude Code


Note

Medium Risk
Changes the trainability gate in reconcile token comparison, but only applies reasoning sums after the existing exact-match and subsequence checks to avoid masking real capture errors.

Overview
Fixes ATIF reconciliation for thinking models when Harbor’s opencode trace reports only visible output in completion_tokens and puts thinking tokens in metrics.extra.reasoning_tokens, while the intercept counts both.

reconcile now builds the ATIF side via _with_reasoning_tokens: it adds per-step reasoning_tokens to completion_tokens only when the raw trace counts do not already match the capture, and the full adjusted list either equals captured turn lengths or fits as the existing auxiliary-call subsequence. If raw counts already align, or the adjusted totals still do not explain the gap, the original trace counts are kept so real mismatches still surface as turn_mismatch (including pi-style traces where reasoning is already inside completion_tokens).

Adds reconcile tests for opencode-style split logging, auxiliary calls with reasoning, pi-style double-counting safety, and cases where reasoning does not explain the difference.

Reviewed by Cursor Bugbot for commit 8740af0. Bugbot is set up for automated code reviews on this repo. Configure here.

reconcile() compares each captured call's sampled tokens with the ATIF step's
completion_tokens. Harbor's opencode trace counts only the visible answer there
and logs thinking in metrics.extra.reasoning_tokens, so every call of a
thinking model differed by its reasoning, the rollout failed as a mismatch, and
no opencode rollout of such a model could be trained on.

The per-step sums replace the counts only when they then equal the capture or
are a subsequence of it, so a difference reasoning does not explain still
fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

The docs for this pull request are available at https://openenv.github.io/OpenEnv/pr-preview/pr-1782/ (commit 8740af0). The preview may take a minute to update and is removed when the pull request closes.

younesbelkada and others added 3 commits December 1, 2026 15:19
Harbor's pi trace writes `metrics.extra.reasoning_tokens` too, but as thinking
already counted in `completion_tokens`. When pi's own counts lined up through
the auxiliary-call path and the reasoning sums happened to fit as well, the sums
won: a real agent turn was marked auxiliary and masked, while the auxiliary call
stayed trainable with the rollout's reward. The sums now apply only when the
trace's own counts do not line up, so a rollout that reconciles today keeps its
verdict and its auxiliary calls.

Co-authored-by: Cursor <cursoragent@cursor.com>
The negative test only covered a call whose difference reasoning does not
explain. Add a capture that lost one call's thinking: choosing the sum per step
would accept it, so pin that the trace's reading applies to all steps and that
the failure is the turn mismatch.

Co-authored-by: Cursor <cursoragent@cursor.com>
@burtenshaw
burtenshaw merged commit 25c68c5 into openenv:main Dec 1, 2026
17 checks passed
@younesbelkada
younesbelkada deleted the harbor/atif-reasoning-tokens branch December 1, 2026 11:40
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.

2 participants