Skip to content

Add stopReason to AgentLoop result when stop() is called#15

Merged
chinmaymk merged 1 commit intomainfrom
claude/ctx-stop-reason-logging-tGmzL
Mar 10, 2026
Merged

Add stopReason to AgentLoop result when stop() is called#15
chinmaymk merged 1 commit intomainfrom
claude/ctx-stop-reason-logging-tGmzL

Conversation

@chinmaymk
Copy link
Copy Markdown
Owner

Summary

This PR adds support for capturing and returning a stop reason when the agent loop is stopped via ctx.stop(reason). The stop reason is now included in the LoopResult as an optional stopReason field.

Key Changes

  • Updated StoppableContext.stop() signature to accept an optional reason parameter in src/agent/types.ts
  • Enhanced LoopResult interface to include an optional stopReason?: string field in src/agent/loop.ts
  • Modified stop function implementation to capture the provided reason and log it when stopping the loop
  • Updated return statements to conditionally include stopReason in the result object only when a reason was provided
  • Added comprehensive test coverage with two new test cases:
    • Verifies that ctx.stop(reason) includes the stop reason in the result
    • Verifies that stopReason is undefined when stop() is called without a reason

Implementation Details

  • The stopReason is stored in a closure variable and conditionally spread into the return object using the pattern ...(stopReason && { stopReason })
  • A debug log statement is included when a stop reason is provided
  • The feature is backward compatible - existing code calling stop() without arguments continues to work as before

https://claude.ai/code/session_017FisrUENRPyYjwgErHWAYt

ctx.stop() now accepts an optional reason string. When provided, it
logs the reason and includes it as stopReason in the LoopResult.

https://claude.ai/code/session_017FisrUENRPyYjwgErHWAYt
@chinmaymk chinmaymk merged commit 9d9bfc7 into main Mar 10, 2026
1 check passed
@chinmaymk chinmaymk deleted the claude/ctx-stop-reason-logging-tGmzL branch March 10, 2026 03:58
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