Skip to content

fix: guard SubagentStop hook to only fire during active workflows#27

Merged
avifenesh merged 4 commits intomainfrom
fix/subagent-stop-hook-guard
Apr 11, 2026
Merged

fix: guard SubagentStop hook to only fire during active workflows#27
avifenesh merged 4 commits intomainfrom
fix/subagent-stop-hook-guard

Conversation

@avifenesh
Copy link
Copy Markdown
Contributor

Summary

  • Switch SubagentStop hook from unconditional prompt type to guarded command type
  • Add subagent-stop-guard.js - a Node.js script that checks for active flow.json before injecting the workflow enforcement prompt
  • Uses Node instead of bash for cross-platform support (Windows + macOS + Linux)
  • Add 12 tests covering no-op cases (no flow, completed, aborted, failed, corrupted JSON) and active workflow cases

Problem

The prompt type SubagentStop hook fired on every subagent stop across the entire session, not just during /next-task workflows. An exploration agent wasted 136K tokens and 19 minutes processing gate logic before returning "All gates blocked."

Fix

The guard script reads {stateDir}/flow.json using the existing platform-aware state directory detection. It only outputs the enforcement prompt to stderr when status === 'in_progress' and phase !== 'complete'. Otherwise it exits silently (no-op).

Closes agent-sh/agentsys#325

Test plan

  • 7 no-op tests: no flow.json, completed, aborted, failed, corrupted JSON, empty, phase=complete edge case
  • 5 active workflow tests: implementation phase, exploration phase, all sections present, missing task title, missing task object
  • Pre-push hooks pass
  • Manual: enable plugin, verify non-workflow subagents complete without delay
  • Manual: run /next-task workflow, verify enforcement prompt still appears between phases

The prompt-type SubagentStop hook fired unconditionally on every subagent
stop, wasting tokens on workflow enforcement when no /next-task workflow
was running. Observed: 136K tokens and 19 minutes wasted on a single
exploration agent.

Switch to a command-type hook with a Node.js guard script that checks
for an active flow.json before outputting the enforcement prompt. Uses
Node instead of bash for cross-platform support (Windows + Unix).

Closes agent-sh/agentsys#325
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the static subagent-stop hook with a dynamic Node.js guard script, subagent-stop-guard.js, which injects workflow enforcement prompts only when a workflow is actively in progress. It also introduces a comprehensive test suite for the guard script and adds a test execution script to package.json. The review feedback recommends simplifying the state directory path resolution by utilizing existing library helpers and adding a fallback value for the workflow phase in the generated prompt to prevent 'undefined' output.

Comment thread hooks/subagent-stop-guard.js Outdated
Comment thread hooks/subagent-stop-guard.js Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 66951f0517

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread hooks/subagent-stop-guard.js
Comment thread hooks/hooks.json Outdated
Comment thread tests/hooks/subagent-stop-guard.test.js Outdated
@avifenesh avifenesh merged commit c0f2745 into main Apr 11, 2026
9 checks passed
@avifenesh avifenesh deleted the fix/subagent-stop-hook-guard branch April 11, 2026 01:13
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.

next-task SubagentStop hook fires on ALL subagents, not just workflow agents

1 participant