Skip to content

sdk: Expose AgentStop session hook across languages - #2054

Open
belaltaher8 wants to merge 4 commits into
github:mainfrom
belaltaher8:bt/sdk-on-agent-stop
Open

sdk: Expose AgentStop session hook across languages#2054
belaltaher8 wants to merge 4 commits into
github:mainfrom
belaltaher8:bt/sdk-on-agent-stop

Conversation

@belaltaher8

@belaltaher8 belaltaher8 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Exposes the top-level agent's agentStop lifecycle hook across the Node.js, Python, Go, .NET, Rust, and Java SDKs.

The Copilot CLI runtime already fires agentStop when the top-level agent reaches a natural terminal stop and supports a block/continue loop: returning { decision: "block", reason } enqueues reason as a follow-up message, while consecutive blocks are capped to prevent runaway loops. This PR adds the missing public SDK types and callback dispatch paths.

Changes

  • Add AgentStop hook input, output, and handler APIs in all six SDK languages.
  • Register and dispatch agentStop through each language's existing hooks.invoke callback transport.
  • Normalize the runtime's stop_hook_active wire field to Node's public stopHookActive field.
  • Export the Node AgentStop types and SessionHooks from the package root.
  • Document AgentStop behavior in the shared hook guide and Node README.
  • Add unit coverage across the language implementations.
  • Add Go and .NET replay-harness E2E coverage for natural-stop callback delivery and block-driven continuation.

Motivation

This enables SDK consumers, including Copilot cloud coding agent integrations, to run completion checks at the natural end of an agent turn and request remediation through the platform hook instead of relying on ad-hoc commit-boundary callbacks.

Behavior

Returning no decision allows the agent to stop normally. Returning:

{ "decision": "block", "reason": "Run final validation and fix any failures." }

keeps the agent running with the reason as a follow-up prompt. Consumers can use the active-stop flag (stopHookActive, with language-appropriate casing) to avoid repeatedly blocking a continuation turn.

Related: github/copilot-agent-runtime#13230

The runtime already fires the top-level agent's `agentStop` hook and
registers it for SDK callback sessions (REGISTERED_CALLBACK_EVENT_NAMES),
with a working block/continue re-prompt loop, but the Node SDK never
exposed it: SessionHooks had no `onAgentStop` and `_handleHooksInvoke`
had no dispatch entry, so `agentStop` callbacks were silently dropped.

Add the AgentStopHookInput/Output/Handler types, the `onAgentStop` field
on SessionHooks, and the `agentStop` entry in the hook dispatcher.
Returning `{ decision: "block", reason }` keeps the agent running with
`reason` enqueued as a follow-up message (e.g. to remediate findings a
handler surfaced); returning nothing lets the agent stop.

This unblocks the Copilot cloud agent restoring its post-completion
security-tool hooks (dependabot / secret scanning) on the proper platform
hook rather than ad-hoc per-commit hooks.

Note: parallel changes for the Python/Go/.NET SDKs are follow-ups.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2031e02b-7fe5-4075-9d75-eb20eb29f407
Copilot AI review requested due to automatic review settings July 22, 2026 16:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Exposes the Node.js SDK’s top-level agentStop lifecycle hook.

Changes:

  • Adds typed agent-stop input, output, and handler APIs.
  • Routes agentStop callbacks through the session dispatcher.
  • Adds dispatcher and JSON-RPC unit tests.
Show a summary per file
File Description
nodejs/src/types.ts Defines the agent-stop hook API.
nodejs/src/session.ts Dispatches agent-stop callbacks.
nodejs/test/client.test.ts Tests direct and JSON-RPC dispatch.

Review details

  • Files reviewed: 3/3 changed files
  • Comments generated: 3
  • Review effort level: Medium

Comment thread nodejs/src/session.ts
Comment thread nodejs/src/types.ts
Comment thread nodejs/src/types.ts
Add AgentStop types and dispatch support for Python, Go, .NET, Rust, and Java, and normalize the Node stop_hook_active wire field.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4f4d61ce-6d2f-4b72-a2da-52f60300df59
Copilot AI review requested due to automatic review settings July 27, 2026 17:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

  • Files reviewed: 23/23 changed files
  • Comments generated: 3
  • Review effort level: Medium

Comment thread docs/hooks/session-lifecycle.md
Comment thread dotnet/src/Types.cs
Comment thread go/types.go
Export Node hook types, document the hook, cover wire-field normalization, add Go and .NET E2E block-continuation coverage, and format the Python README.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4f4d61ce-6d2f-4b72-a2da-52f60300df59
Copilot AI review requested due to automatic review settings July 28, 2026 02:15
@belaltaher8 belaltaher8 changed the title nodejs: Expose onAgentStop session hook sdk: Expose AgentStop session hook across languages Jul 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

  • Files reviewed: 29/29 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment thread docs/hooks/session-lifecycle.md Outdated
Document the public input member names for Node, Python, Go, .NET, Rust, and Java.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 4f4d61ce-6d2f-4b72-a2da-52f60300df59
Copilot AI review requested due to automatic review settings July 28, 2026 02:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review details

  • Files reviewed: 29/29 changed files
  • Comments generated: 0 new
  • Review effort level: Medium

@belaltaher8
belaltaher8 marked this pull request as ready for review July 28, 2026 02:50
@belaltaher8
belaltaher8 requested a review from a team as a code owner July 28, 2026 02:50
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