Skip to content

fix: finalize the Stop hook target turn - #71

Open
tutar wants to merge 1 commit into
langfuse:mainfrom
tutar:issue-1-final-turn
Open

fix: finalize the Stop hook target turn#71
tutar wants to merge 1 commit into
langfuse:mainfrom
tutar:issue-1-final-turn

Conversation

@tutar

@tutar tutar commented Sep 5, 2026

Copy link
Copy Markdown

Summary

  • treat the exact turn_id from the Stop-hook payload as the turn that has stopped
  • skip every other incomplete top-level or subagent turn found while scanning the rollout
  • write the sidecar delivery receipt only after telemetry shutdown and flush succeed
  • keep failed exports retryable while preserving fail-open and optional fail-on-error behavior

Why

Codex can invoke the Stop hook before it appends the matching task_complete event, and persist that event only after the hook process exits. The current implementation can therefore export an incomplete turn without recording it in the sidecar. A later invocation exports the completed turn again. Simply deferring incomplete turns prevents that duplicate but permanently loses the final turn when no later Stop hook runs.

The Stop payload already identifies the exact stopped turn. This change uses that signal only for the named turn; arbitrary incomplete turns remain suppressed.

The corrected lifecycle was reproduced and production-verified in tutar#1.

Verification

  • TypeScript: tsc --noEmit
  • bundle: tsdown --config plugins/tracing/tsdown.config.ts
  • focused Stop-hook and trace tests: 22 passed
  • full suite: 5 files, 47 tests passed
  • bundled-hook coverage checks one exported root, target turn and thread identity, final output, bounded end time, successful sidecar write, replay deduplication, and failed-flush retryability
  • Standards review: 0 findings
  • Spec review: 0 findings

Related upstream work

Fixes #46.

Related to #12 and #65. This covers both halves of the lifecycle that are handled separately or incompletely by #17, #31, and #50.

@claude claude Bot 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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

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.

[Bug] Duplicate Langfuse traces for a single Codex turn when Stop hook runs before task_complete

1 participant