fix(kyberforge): harden plugin-content sync, reinject Copilot mcpServers

PR #95's review of the issue #90 apm-conversion work found several
defects in scripts/sync-plugin-content.sh and the gate wired to it:

- --check claimed never to mutate the plugin root, but apm pack still
  wrote .claude-plugin/plugin.json and .github/plugin/plugin.json into
  the real plugin_dir on first-time creation. --check now packs a
  throwaway copy instead.
- check-plugin-content-sync hardcoded the six plugin directories
  instead of deriving them the way check-manifests.sh already does.
  Added an --all flag that parses .claude-plugin/marketplace.json, and
  simplified the pre-commit hook to use it.
- A missing plugin_dir and one that legitimately has no .apm/ yet both
  reported SKIP/success; a missing directory now FAILs.
- The dispatch loop backgrounded every plugin with no concurrency cap,
  unlike the JOBS-bounded pattern this same PR added to
  tests/run-bats.sh and tests/run-tests.sh. Added the same
  bash-3.2-safe getconf + batched-wait cap here for consistency.
- Per-plugin scratch/log/status files were keyed only by basename, with
  no collision guard across arguments; added a fail-fast check.
- sync_hooks_json()'s trailing-newline normalization was duplicated
  between its --check and write branches; factored into one helper.
- tests/test-sync-plugin-content.sh set two competing `trap ... EXIT`
  statements, so the first (cleaning up $FIXTURE) was silently
  replaced by the second and its tmp dir leaked every run. Adopted the
  track()/CLEANUP_DIRS pattern already used in
  tests/test-check-release-needed.sh.

Separately: apm's Copilot-ecosystem plugin.json builder unconditionally
strips mcpServers, citing (in its own docstring) that the field is out
of schema for Copilot -- a claim this repo's own researched Copilot
plugin schema docs contradict. reinject_mcp_servers() narrowly restores
it from the plugin's .mcp.json on real syncs only, regenerating
plugins/bin/.github/plugin/plugin.json (the only plugin that currently
declares any MCP servers). Documented as an amendment to ADR-0017,
since it's a deliberate, narrow exception to that ADR's rejection of
patching apm's compiled output -- apm's premise for stripping
skills/agents/commands/hooks pointers is still accurate; its premise
for stripping mcpServers is not.

All 12 assertions in tests/test-sync-plugin-content.sh pass
individually, plus 5 new regression tests added for this round; the
full bats and shell-script suites are green; shellcheck is clean.

Refs: #95
ADR: 0017
This commit is contained in:
2026-08-13 19:47:36 +00:00
parent bff9662c52
commit 9c140efa2e
5 changed files with 283 additions and 21 deletions

View File

@@ -61,11 +61,56 @@ EOF
echo "$dir"
}
# Same base fixture, but with a copilot target (so apm pack produces
# .github/plugin/plugin.json) and a caller-supplied .mcp.json -- for exercising
# reinject_mcp_servers().
make_fixture_with_mcp() {
local mcp_json="$1" dir
dir="$(mktemp -d)"
mkdir -p "$dir/.apm/skills/hello" "$dir/.apm/agents"
cat > "$dir/apm.yml" <<'YAML'
name: fixture
version: 0.0.1
description: fixture
license: MIT
type: hybrid
targets:
- claude
- copilot
dependencies:
apm: []
mcp: []
includes: auto
devDependencies:
apm: []
scripts: {}
YAML
cat > "$dir/.apm/skills/hello/SKILL.md" <<'EOF'
---
name: hello
description: hello
---
Hello.
EOF
cat > "$dir/.apm/agents/foo.agent.md" <<'EOF'
---
name: foo
description: foo
---
Foo.
EOF
printf '%s' "$mcp_json" > "$dir/.mcp.json"
echo "$dir"
}
CLEANUP_DIRS=()
trap 'rm -rf "${CLEANUP_DIRS[@]}"' EXIT
track() { CLEANUP_DIRS+=("$1"); }
# --- 1. --check reports drift before any sync has run ---
echo ""
echo "--- --check reports drift on an unsynced fixture ---"
FIXTURE="$(make_fixture)"
trap 'rm -rf "$FIXTURE"' EXIT
FIXTURE="$(make_fixture)"; track "$FIXTURE"
if bash "$SCRIPT" --check "$FIXTURE" > /dev/null 2>&1; then
fail "exited 0 on an unsynced fixture — expected drift (exit 1)"
else
@@ -128,14 +173,69 @@ fi
# --- 6. A plugin dir with no .apm/ is skipped cleanly, not treated as an error ---
echo ""
echo "--- a plugin dir with no .apm/ is skipped, not failed ---"
NO_APM="$(mktemp -d)"
trap 'rm -rf "$NO_APM"' EXIT
NO_APM="$(mktemp -d)"; track "$NO_APM"
if bash "$SCRIPT" "$NO_APM" > /dev/null 2>&1 && bash "$SCRIPT" --check "$NO_APM" > /dev/null 2>&1; then
pass "a plugin dir with no .apm/ exits 0 in both real and --check mode"
else
fail "a plugin dir with no .apm/ should exit 0 (skip), not fail"
fi
# --- 7. A plugin dir that doesn't exist at all is a hard failure, not a skip ---
echo ""
echo "--- a plugin dir that does not exist fails, distinct from an existing-but-empty one ---"
MISSING_ROOT="$(mktemp -d)"; track "$MISSING_ROOT"
MISSING="$MISSING_ROOT/does-not-exist"
if bash "$SCRIPT" "$MISSING" > /dev/null 2>&1; then
fail "exited 0 for a plugin dir that does not exist — expected a hard failure"
else
pass "a nonexistent plugin dir fails instead of silently skipping"
fi
# --- 8. --check never mutates the real plugin root, even on first-time manifest creation ---
echo ""
echo "--- --check does not create .claude-plugin/plugin.json or .github/plugin/plugin.json ---"
FIXTURE8="$(make_fixture)"; track "$FIXTURE8"
bash "$SCRIPT" --check "$FIXTURE8" > /dev/null 2>&1 || true
if [[ ! -e "$FIXTURE8/.claude-plugin/plugin.json" ]] && [[ ! -e "$FIXTURE8/.github/plugin/plugin.json" ]]; then
pass "--check leaves the real plugin root without a first-write plugin.json"
else
fail "--check created plugin.json in the real plugin root — it must never mutate it"
fi
# --- 9. A duplicate plugin-dir basename among arguments fails fast, not silently ---
echo ""
echo "--- duplicate plugin dir basenames among arguments are rejected ---"
DUP_PARENT_A="$(mktemp -d)"; track "$DUP_PARENT_A"
DUP_PARENT_B="$(mktemp -d)"; track "$DUP_PARENT_B"
mkdir -p "$DUP_PARENT_A/dup" "$DUP_PARENT_B/dup"
if bash "$SCRIPT" "$DUP_PARENT_A/dup" "$DUP_PARENT_B/dup" > /dev/null 2>&1; then
fail "exited 0 with two plugin-dir arguments sharing a basename — expected a collision error"
else
pass "rejects two plugin-dir arguments that share a basename"
fi
# --- 10. Real sync re-injects mcpServers that apm's Copilot builder strips ---
echo ""
echo "--- real sync re-injects mcpServers into .github/plugin/plugin.json ---"
FIXTURE10="$(make_fixture_with_mcp '{"mcpServers":{"demo":{"command":"demo-server","type":"stdio"}}}')"; track "$FIXTURE10"
bash "$SCRIPT" "$FIXTURE10" > /dev/null 2>&1
if jq -e '.mcpServers.demo.command == "demo-server"' "$FIXTURE10/.github/plugin/plugin.json" > /dev/null 2>&1; then
pass "mcpServers from .mcp.json is present in .github/plugin/plugin.json after a real sync"
else
fail "mcpServers was not re-injected into .github/plugin/plugin.json"
fi
# --- 11. An empty .mcp.json does not add a redundant mcpServers: {} ---
echo ""
echo "--- an empty .mcp.json does not add mcpServers: {} ---"
FIXTURE11="$(make_fixture_with_mcp '{"mcpServers":{}}')"; track "$FIXTURE11"
bash "$SCRIPT" "$FIXTURE11" > /dev/null 2>&1
if jq -e 'has("mcpServers") | not' "$FIXTURE11/.github/plugin/plugin.json" > /dev/null 2>&1; then
pass "an empty .mcp.json does not add mcpServers to .github/plugin/plugin.json"
else
fail "an empty .mcp.json still added mcpServers -- should match apm's own omit-when-empty convention"
fi
echo ""
echo "Results: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]]