# Task-sync hooks for Claude Code

Source: https://projectfeed.app/docs/agents/task-sync

Inject open tasks into Claude Code sessions and remind the agent to update progress after edits and commits.

## Install

The scripts need `pf` and `jq` on PATH, and a credential with `tasks:read` and `tasks:write` scopes. The hooks use Claude Code's hook events and settings file. They run as bash scripts, so on native Windows run Claude Code in WSL. The context hook limits each call to five seconds with `perl -e 'alarm 5'`, which relies on Unix behavior. Replace it with another timeout if you run the script elsewhere.

1.  Wire the hooks in `.claude/settings.json` (checked into the repo, so every agent session gets them):
    
    JSON
    
    ```
    {
      "hooks": {
        "UserPromptSubmit": [
          {
            "hooks": [
              { "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/pf-task-context.sh" }
            ]
          }
        ],
        "Stop": [
          {
            "hooks": [
              { "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/pf-task-checkout.sh" }
            ]
          }
        ],
        "PostToolUse": [
          {
            "matcher": "Bash|PowerShell",
            "hooks": [
              { "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/pf-task-commit-nudge.sh" }
            ]
          }
        ]
      },
      "permissions": {
        "allow": ["Bash(pf:*)"]
      }
    }
    ```
    
2.  Save the three scripts from the sections below into `.claude/hooks/`.
3.  Make them executable and set your project ID:
    
    Shell
    
    ```
    mkdir -p .claude/hooks
    # save the three scripts below into .claude/hooks/, then:
    chmod +x .claude/hooks/pf-task-*.sh
    export PF_PROJECT_ID=your-project-id   # or hardcode it in the scripts
    ```
    
4.  Verify. Start an agent session and ask for any small code edit without mentioning tasks. When the agent tries to end the turn, the gate should block it with instructions to sync the task, and the agent should create or update one on its own. If the turn ends silently instead, the gate is not firing; check that `jq` is on PATH and the script is executable.

## \1. Prompt-time context (UserPromptSubmit)

Before each prompt, the hook loads open tasks and the task workflow instructions. If the list stops appearing, check that the CLI is on PATH and can reach the API. The prompt can still run if the hook fails.

Shell

```
#!/usr/bin/env bash
# UserPromptSubmit: inject the open Project Feed task list into every prompt,
# plus the standing rule. Fails open: a hung API never blocks the user.
set -u

PF_PROJECT_ID="${PF_PROJECT_ID:-your-project-id}"

command -v pf >/dev/null 2>&1 || exit 0

# Hard 5s timeout so a slow network cannot stall the prompt.
pf_safe() { perl -e 'alarm 5; exec @ARGV' pf "$@" 2>/dev/null; }

command -v jq >/dev/null 2>&1 || exit 0

todo=$(pf_safe tasks list --project "$PF_PROJECT_ID" --status todo --json id,title,priority)
doing=$(pf_safe tasks list --project "$PF_PROJECT_ID" --status in_progress --json id,title,priority)
todo_count=$(printf '%s' "$todo" | jq 'length' 2>/dev/null)
doing_count=$(printf '%s' "$doing" | jq 'length' 2>/dev/null)
[ "${todo_count:-0}" -eq 0 ] && [ "${doing_count:-0}" -eq 0 ] && exit 0

cat <<CONTEXT
## Project Feed tasks (auto-injected)

=== todo ===
$todo

=== in progress ===
$doing

Standing rule: every non-trivial change maps to a Project Feed task. Before
editing code, find the matching task above or create one and move it to
in_progress. Keep its checklist and comments current as you work, not only
at the end. Move it to in_review when the planned work is complete. Never
mark a task done unless the user explicitly asks.
CONTEXT
```

## \2. Turn-end gate (Stop)

If the agent edited code without running `pf`, the hook blocks the end of the turn and asks it to update the task. The check detects a pf command invocation, not a successful task update. Review the task to confirm its status and checklist. The agent can also explain why the work does not need a task.

When adapting the script: Scan only the current turn so earlier edits do not trigger the check again. Match every shell tool your agent uses, including PowerShell on Windows. A Bash-only matcher will miss those commands.

Shell

```
#!/usr/bin/env bash
# Stop hook: if THIS TURN touched code (Write/Edit/NotebookEdit tool use, or a
# git commit run through the Bash or PowerShell tool) but never ran `pf`,
# block turn-end with a sync reminder. "This turn" = transcript entries after
# the last genuine user prompt (a user message containing text, not a
# tool_result carrier). The stop_hook_active flag prevents infinite loops.
set -u

command -v jq >/dev/null 2>&1 || exit 0

INPUT=$(cat)
STOP_ACTIVE=$(printf '%s' "$INPUT" | jq -r '.stop_hook_active // false' 2>/dev/null || echo "false")
TRANSCRIPT=$(printf '%s' "$INPUT" | jq -r '.transcript_path // ""' 2>/dev/null || echo "")

[ "$STOP_ACTIVE" = "true" ] && exit 0
{ [ -z "$TRANSCRIPT" ] || [ ! -f "$TRANSCRIPT" ]; } && exit 0

# Slice the transcript to the current turn, then flatten to tool_use blocks.
JQ_TURN_TOOLS='
  def is_user_prompt:
    .type == "user"
    and (((.message?.content? | type) == "string")
         or (((.message?.content? | type) == "array")
             and ([.message.content[]? | .type? // ""] | index("text") != null)
             and ([.message.content[]? | .type? // ""] | index("tool_result") == null)));
  ([to_entries[] | select(.value | is_user_prompt) | .key] | last // -1) as $turn_start
  | [ .[($turn_start + 1):][] | .message? | .content? // [] | .[]?
      | select(.type? == "tool_use") ]
'

WORK_HAPPENED=$(jq -s "$JQ_TURN_TOOLS"'
  | [ .[] | select(
        (.name == "Write") or (.name == "Edit") or (.name == "NotebookEdit") or
        (((.name == "Bash") or (.name == "PowerShell"))
          and ((.input.command // "") | test("(^|[;&|]|\\n)[[:space:]]*git[[:space:]]+commit([[:space:]]|$)")))
      ) ] | length
' "$TRANSCRIPT" 2>/dev/null || echo 0)

PF_RAN=$(jq -s "$JQ_TURN_TOOLS"'
  | [ .[] | select((.name == "Bash") or (.name == "PowerShell"))
      | select((.input.command // "") | test("(^|[^a-zA-Z_-])pf[[:space:]]")) ] | length
' "$TRANSCRIPT" 2>/dev/null || echo 0)

if [ "${WORK_HAPPENED:-0}" -gt 0 ] && [ "${PF_RAN:-0}" -eq 0 ]; then
  jq -n '{
    decision: "block",
    reason: "You modified code this turn but did not touch any Project Feed (pf) task. Identify or create the task, update its checklist, leave a short comment on what changed and what is next, then end the turn. If this work genuinely needs no task (read-only research, trivial typo), say so and continue."
  }'
fi
exit 0
```

## \3. Commit nudge (PostToolUse)

After `git commit`, the hook reminds the agent to update the task's checklist and add a comment about the change.

Shell

```
#!/usr/bin/env bash
# PostToolUse on Bash/PowerShell: after a real `git commit`, remind the agent
# to mirror the commit into the active Project Feed task.
set -u

command -v jq >/dev/null 2>&1 || exit 0

INPUT=$(cat)
TOOL_NAME=$(printf '%s' "$INPUT" | jq -r '.tool_name // ""' 2>/dev/null || echo "")
CMD=$(printf '%s' "$INPUT" | jq -r '.tool_input.command // ""' 2>/dev/null || echo "")

case "$TOOL_NAME" in Bash|PowerShell) ;; *) exit 0 ;; esac

# Match `git commit` as an actual invocation (line start or after && || ; |),
# not inside a string literal; skip help/dry-run forms.
[[ "$CMD" =~ (^|[\;\&\|])[[:space:]]*git[[:space:]]+commit([[:space:]]|$) ]] || exit 0
[[ "$CMD" =~ --dry-run|--help ]] && exit 0

# Plain stdout from PostToolUse is not shown to the agent; send additionalContext.
jq -n '{
  hookSpecificOutput: {
    hookEventName: "PostToolUse",
    additionalContext: "A git commit just landed. Mirror it into the active Project Feed task now: check off completed checklist items, and add a one-line comment referencing the commit (what changed, what was validated)."
  }
}'
```

## How the hooks work

The context hook loads task details before a turn. The Stop hook checks for pf commands after code edits, and the commit hook reminds the agent to add a progress comment. Use the hooks with the AGENTS.md instructions in the [agent setup guide](https://projectfeed.app/docs/agents).

The hooks let the session continue if the API is unavailable. Give the agent a credential with task write access so it can update status, checklists, and comments. See [Scopes and token types](https://projectfeed.app/docs/api/scopes).
