/cc-authoring:cc-hooks-main-vs-subagentfrom cc-authoring
How a Claude Code PreToolUse/PostToolUse hook can tell whether a tool call came from the MAIN conversation thread or from a SUBAGENT (Task/Agent tool), plus the gotchas of testing hooks via headless `claude -p`. Use when: (1) building a hook that should apply only to the main thread (e.g. orchestrator-only enforcement that denies Edit/Write in main but allows subagents) or only to subagents; (2) wondering if hook input contains an is_subagent / agent indicator — it does, but it's undocumented; (3) a hook behaves differently than expected under `claude -p` because CLAUDE_CODE_ENTRYPOINT is sdk-cli there and `env CLAUDE_CODE_ENTRYPOINT=cli` does NOT survive (the CLI overwrites it); (4) a headless hook test shows "Claude requested permissions ... but you haven't granted it yet" instead of the hook's deny reason — the permission gate fires before the hook surfaces; (5) you need to force the interactive code path in a headless test (use a --settings file with an env block).
Ships in cc-authoring. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Hooks configured in settings.json fire for BOTH main-conversation tool calls and subagent tool calls. The official hooks docs do not document any way to tell them apart, which blocks patterns like "orchestrator-only main thread" (deny Edit/Write in main, allow in subagents) or subagent-only auditing.
claude -p and getting confusing results.Subagent tool calls include two extra fields in the hook stdin JSON that are entirely absent for main-thread calls:
"agent_id": "a0fc2a0a18648d281",
"agent_type": "general-purpose"
Everything else is identical between main and subagent calls — same
session_id, same transcript_path (subagents do NOT get an agent-*.jsonl
transcript_path in hook input), same cwd, same permission_mode, and a
byte-identical environment (no env var distinguishes them).
Hook logic:
input=$(cat)
if [ -n "$(printf '%s' "$input" | jq -r '.agent_id // empty')" ]; then
exit 0 # subagent — allow / skip
fi
# ... main-thread handling, e.g. deny:
printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"..."}}\n'
The hook process env contains CLAUDE_CODE_ENTRYPOINT: cli for interactive
sessions, sdk-cli for claude -p. Gate on case "$CLAUDE_CODE_ENTRYPOINT" in sdk*) exit 0;; esac to exempt headless workers (ralph loops, autoresearch,
skill evals) from main-thread enforcement.
env CLAUDE_CODE_ENTRYPOINT=cli claude -p ... does NOT work — the CLI
unconditionally overwrites the var to sdk-cli in print mode. To force the
interactive code path in a headless test, use a settings file instead:
claude --settings force-cli.json -p ... where the file contains
{"env":{"CLAUDE_CODE_ENTRYPOINT":"cli"}}. The settings env block wins.--permission-mode acceptEdits (or an allowedTools grant), a headless Write is rejected with
"Claude requested permissions to write to X, but you haven't granted it
yet" — which looks like a hook deny but is not. Grant permissions first so
the hook's permissionDecisionReason is what you actually observe.-p "prompt"; putting
--allowedTools between -p and the prompt yields "Input must be provided
either through stdin or as a prompt argument".jq -c --arg env "$(env | grep -iE 'CLAUDE|AGENT')" '{input: ., env: $env}' >> log.jsonl
on a temp --settings file is the cheapest way to see all hook input
fields for a given CLI version.{"env":{"CLAUDE_CODE_ENTRYPOINT":"cli"}} settings, a
main-thread Write was denied with the hook's reason while a delegated
subagent Write in the same configuration succeeded; plain claude -p
(sdk-cli) main-thread Write succeeded (exemption worked).Working production example: ~/.claude/hooks/orchestrator-guard.sh wired in
~/.claude/settings.json (PreToolUse, matcher Edit|Write|NotebookEdit|Bash)
— denies main-thread file mutations in interactive sessions, exempts
subagents via agent_id, exempts headless via CLAUDE_CODE_ENTRYPOINT=sdk*,
kill switch CLAUDE_ORCH_GUARD=off.
agent_id/agent_type are undocumented; re-verify after major Claude
Code upgrades (one logging-hook run, see gotcha 4). Verified on 2.1.173.SubagentStart/SubagentStop hook events exist in the settings schema but
are NOT needed for per-call discrimination and are racy for it (parallel
agents overlap the main loop).What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub cajias/claude-skills --plugin cc-authoring16 skills in this plugin
/apm-marketplace-authoring-gotchasEight APM-marketplace-authoring gotchas discovered during real
migrations from hand-authored .claude-plugin/marketplace.json to
APM-managed and from CC-native plugin authoring.
Use when: (1) running APM's packaging step (with or without
`--dry-run`) or its marketplace-check command in a repo with an
`apm.yml`; (2) wiring CI for an APM
marketplace; (3) the CI staleness check (repack, then `git diff
--quiet`) fails despite a clean local; (4) prettier reformats
`marketplace.json` after APM's packer writes it; (5) APM exit code 1 on
local-path-only marketplaces; (6) confused by `packages:` in apm.yml
vs `plugins:` in marketplace.json; (7) commitlint rejects a commit
with `footer-leading-blank` despite no apparent footer; (8) `apm
compile -t claude` exits 0 with zero output and no compiled plugin
appears; (9) husky+lint-staged pre-commit blocks the FIRST marketplace
commit with markdownlint `MD040/fenced-code-language` errors on a
SKILL.md file copied verbatim from `~/.claude/skills/`. The gotchas
form a chain — fixing one usually surfaces the next.
/apm-plugin-distribution-pathsCanonical authoring layout for an APM (microsoft/apm)
package, plus the decision matrix for whether you need a `marketplace:` block.
Use when: (1) you've run `apm init` and are unsure where SKILL.md / agent .md
/ command .md files belong (answer: under `.apm/`, NOT at repo root); (2)
you've authored CC-native files at `commands/agents/skills/` at repo root and
wonder if that's "right" (it works as fallback layout #3 "Claude plugin", but
it's not the canonical authoring path); (3) you're trying to choose between
the three layouts APM recognizes (one-skill repo, multi-primitive `.apm/`,
Claude plugin); (4) `apm targets` fails with "No such command" in CLI 0.12.1
(subcommand doesn't exist); (5) `apm validate` fails with "No such command"
in CLI 0.12.1 (use APM's packaging command in dry-run mode instead); (6) wondering whether
`apm compile -t claude` will generate output for a hand-authored CC-native
plugin (it won't; it's a no-op for layout #3); (7) wondering if you need a
`marketplace:` block in apm.yml (you don't, for a single-plugin repo).
Covers the canonical `.apm/` source layout, the source-to-plugin remap
table, the three-layout choice, and the four distribution paths.