Adds end-to-end flow for installing and keeping the per-user filtered
Claude Code marketplace in sync with the user's Agnes stack
(admin RBAC grants \ MyAIStack opt-outs U /store installs).
Setup (one-liner in install prompt step 5):
`agnes refresh-marketplace --bootstrap` clones the per-user marketplace
bare repo to ~/.agnes/marketplace, strips PAT from the cloned origin
URL, registers the local path with Claude Code, and installs every
plugin in the served manifest at --scope project. Replaces a 15-line
inline shell sequence that tripped Claude Code's agent-driven `rm -rf`
permission gate.
Auto-refresh (SessionStart hook installed by `agnes init`):
`agnes refresh-marketplace --quiet` runs every Claude Code session,
fetches+resets the clone (server rebuilds as orphan commits, so
pull --ff-only is impossible), and version-aware reconciles:
- missing in workspace -> claude plugin install <name>@agnes --scope project
- version differs -> claude plugin update <name>@agnes
- matches -> skip
Don't auto-uninstall plugins that disappeared from the manifest --
a transient empty manifest from the server would wipe the stack.
Hook output: when --quiet AND something actually changed, emits Claude
Code hook JSON on stdout -- `systemMessage` (transient toast) and
`hookSpecificOutput.additionalContext` (model-side system reminder),
both carrying the change summary plus a "/exit + restart Claude Code"
instruction (Claude only scans plugins at session start).
Windows hook compatibility: the refresh-marketplace hook command is
wrapped in `bash -c "..."` because Claude Code on Windows runs hook
commands directly without invoking a shell, so `2>/dev/null || true`
would otherwise be passed as literal argv tokens.
Cross-cutting:
- cli/lib/marketplace.py: shared CLONE_DIR + MARKETPLACE_NAME constants.
- cli/lib/hooks.py: SessionStart now has two independent entries
(pull + refresh-marketplace) so a failure in one doesn't suppress
the other; legacy `da sync` and prior single-pull layouts upgrade
cleanly on re-init.
- PAT injection on every git fetch via per-invocation credential
helper (token in \$AGNES_TOKEN env, never in argv or .git/config).
- Pre-snapshot of installed plugins captured BEFORE
`claude plugin marketplace update` so silent auto-applied version
bumps still fire notifications.
- scripts/dev/agnes-client-reset.sh: cleans ~/.claude/plugins/marketplaces/agnes,
~/.claude/plugins/cache/agnes, drops uv build cache, documents
workspace-scoped residue that can't be enumerated from the script.
- app/web/setup_instructions.py: legacy AGNES_DEBUG_AUTH path also
uses clone (direct HTTPS marketplace add is broken end-to-end on
every Claude Code distribution -- stores response as single file,
plugin source paths then 404).
28 new tests (test_cli_refresh_marketplace.py) + extended hook + setup
template tests cover bootstrap, fetch+reset ordering, version-aware
reconcile, project-path filtering, hook JSON shape, and the bash-c
Windows wrapper invariant.
104 lines
4.3 KiB
Python
104 lines
4.3 KiB
Python
"""Workspace-scoped Claude Code hook installer.
|
|
|
|
Lifted from `cli/commands/analyst.py:_install_claude_hooks` (which gets
|
|
deleted in Task 18) so `agnes init` and any future caller can use it
|
|
without dragging in the deleted command module.
|
|
|
|
Design notes:
|
|
- Workspace-scoped (`<workspace>/.claude/settings.json`), NOT user-home.
|
|
The hooks fire only when Claude Code opens this workspace.
|
|
- Idempotent: second invocation drops prior `agnes self-upgrade` /
|
|
`agnes pull` / `agnes push` / `agnes refresh-marketplace` / `da sync`
|
|
entries (matched by command substring) and appends fresh entries.
|
|
Third-party hooks (mixed entries, foreign commands) are left alone.
|
|
- Uses `|| true` in the hook command so the hook never blocks a session on
|
|
a transient sync error.
|
|
- SessionStart gets two entries:
|
|
1. Chained `agnes self-upgrade; agnes pull` — self-upgrade runs first
|
|
so any wire-protocol bump lands before pull tries to use the new
|
|
CLI version. Both `|| true`-guarded so an upgrade failure doesn't
|
|
block the pull.
|
|
2. `agnes refresh-marketplace` — independent entry so a fresh
|
|
workspace (no marketplace cloned yet) failing this command doesn't
|
|
suppress the data pull above.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
# Substrings that identify "our" hook commands. Includes legacy `da sync`
|
|
# so a workspace bootstrapped by an older CLI gets cleanly upgraded on the
|
|
# next `agnes init` run.
|
|
_OUR_COMMAND_MARKERS = (
|
|
"agnes self-upgrade",
|
|
"agnes pull",
|
|
"agnes push",
|
|
"agnes refresh-marketplace",
|
|
"da sync",
|
|
)
|
|
|
|
|
|
def install_claude_hooks(workspace: Path) -> None:
|
|
"""Install SessionStart hooks (`agnes self-upgrade; agnes pull` chained
|
|
+ `agnes refresh-marketplace` as a separate entry) and SessionEnd hook
|
|
(`agnes push`).
|
|
|
|
Idempotent. Workspace-scoped (writes `<workspace>/.claude/settings.json`).
|
|
Preserves third-party hooks and other event types.
|
|
"""
|
|
settings_path = workspace / ".claude" / "settings.json"
|
|
settings_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
if settings_path.exists():
|
|
try:
|
|
cfg = json.loads(settings_path.read_text(encoding="utf-8"))
|
|
except json.JSONDecodeError:
|
|
print(
|
|
f"Warning: {settings_path} is not valid JSON; skipping hook install.",
|
|
file=sys.stderr,
|
|
)
|
|
return
|
|
else:
|
|
cfg = {}
|
|
|
|
hooks = cfg.setdefault("hooks", {})
|
|
|
|
def _replace_or_add(event: str, commands: list[str]) -> None:
|
|
existing = hooks.setdefault(event, [])
|
|
# Remove ALL prior entries that look like ours (every command in
|
|
# the entry matches one of our markers). Third-party entries
|
|
# — which have commands like `echo hi from another tool` — fall
|
|
# through unchanged.
|
|
for entry in list(existing):
|
|
entry_cmds = [h.get("command", "") for h in entry.get("hooks", [])]
|
|
if entry_cmds and all(
|
|
any(marker in c for marker in _OUR_COMMAND_MARKERS) for c in entry_cmds
|
|
):
|
|
existing.remove(entry)
|
|
# Append fresh entries — one per command. Independent entries mean
|
|
# a failure in one (e.g. refresh-marketplace on a workspace that
|
|
# never cloned the marketplace) doesn't suppress the other.
|
|
for cmd in commands:
|
|
existing.append({"hooks": [{"type": "command", "command": cmd}]})
|
|
|
|
# `refresh-marketplace` is wrapped in `bash -c` because Claude Code on
|
|
# Windows runs hook commands directly (no shell), so the `2>/dev/null
|
|
# || true` redirection + short-circuit syntax never gets interpreted.
|
|
# The self-upgrade+pull chained entry pre-dates the Windows fix and
|
|
# isn't churned for parity (the same redirection fluff applies but
|
|
# changing the existing wire would force every workspace to re-write
|
|
# its settings.json on the next `agnes init` for no behaviour gain).
|
|
_replace_or_add("SessionStart", [
|
|
"agnes self-upgrade --quiet 2>/dev/null || true; "
|
|
"agnes pull --quiet 2>/dev/null || true",
|
|
'bash -c "agnes refresh-marketplace --quiet 2>/dev/null || true"',
|
|
])
|
|
_replace_or_add("SessionEnd", [
|
|
"agnes push --quiet 2>/dev/null || true",
|
|
])
|
|
|
|
settings_path.write_text(json.dumps(cfg, indent=2) + "\n", encoding="utf-8")
|