diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eb59b78c..3f21b0a9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ breaking changes may land in a minor release. ### Added +- Add a first-class Cline CLI adapter profile with a `cline-file-hooks` dialect. `init` writes executable Cline hook scripts that relay task start, completion, shutdown, and error, map `taskId` to `sessionId`, preserve failure text, and seed the scripts into worktrees. - Add the experimental `tickets` work source for BMAD v7 initiative ticket trees (`[stories] source = "tickets"`): dispatch `bmad-build-auto ticket ` in initiative build order, verify each plan at `built`, and mark it `done` through `tickets.py mark` diff --git a/docs/README.md b/docs/README.md index b7ca53c75..8c1d00c93 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,6 +22,7 @@ guides below go deeper, roughly in the order you'll need them. ## Extending bmad-loop - **[Authoring CLI adapters & profiles](adapter-authoring-guide.md)** — using `bmad-loop probe-adapter` to collect + sanitize the hook payload shape, transcript location, and token schema a new CLI profile needs, plus the `CodingCLIAdapter` ABC and how an adapter class (and the profile selecting it) ships out-of-tree. +- **[Cline CLI](cline-hook-bridge.md)** — built-in profile: file hooks, the JSON registration shim, worktree seeding, and unattended launch flags. - **[Writing a bmad-loop plugin](plugin-authoring-guide.md)** — the plugin system: `plugin.toml` manifest, hooks, lifecycle stages, settings, the trust model, and workflow injection, with a worked walkthrough. - **[Writing a Game Engine plugin](game-engine-plugin-guide.md)** — the game-engine layer (built on the plugin system): driving a live engine Editor, the `editor_mode` ↔ `[scm] isolation` coupling, a minimal Godot example. - **[Writing a plugin for a specific Editor MCP](game-engine-mcp-guide.md)** — Editor-MCP specifics for the bundled Unity plugin: IvanMurzak vs CoplayDev, readiness probes, `per_worktree` isolation, and the full `BMAD_LOOP_*` env-var reference. diff --git a/docs/cline-hook-bridge.md b/docs/cline-hook-bridge.md new file mode 100644 index 000000000..d16ebb350 --- /dev/null +++ b/docs/cline-hook-bridge.md @@ -0,0 +1,11 @@ +# Cline CLI + +Cline is a built-in adapter profile. `bmad-loop init --cli cline` writes a JSON registration shim and four executable hook scripts in `.cline/hooks/`, then Cline relays `TaskStart`, `TaskComplete`, `SessionShutdown`, and `TaskError` into bmad-loop. + +Cline discovers hooks as files, not as a JSON settings file. The shim exists so `validate` can check registration; the scripts are the live hook path. `init` writes them with the relay command for this installation. + +## Setup + +Run `cline auth` once in the project, then `bmad-loop init --project . --cli cline` and `bmad-loop validate --project .`. The profile reads skills from `.agents/skills` and seeds the four hook scripts into worktrees. + +Unattended stages use `--auto-approve true` rather than `--yolo`, because yolo mode disables Cline's hook forwarding. Token usage is not parsed; the profile sets `usage_parser = "none"`. diff --git a/src/bmad_loop/adapters/profile.py b/src/bmad_loop/adapters/profile.py index e5af4b93d..70c0b36c0 100644 --- a/src/bmad_loop/adapters/profile.py +++ b/src/bmad_loop/adapters/profile.py @@ -76,6 +76,9 @@ "gemini-settings-json", "copilot-settings-json", "antigravity-hooks-json", + # Cline discovers hooks as executable files in .cline/hooks/. init still + # writes a JSON registration shim for validate, then writes the scripts. + "cline-file-hooks", # hookless: the adapter observes completion itself (HTTP/SSE transport) — # no hook config is ever written, so config_path/events must stay empty. "none", diff --git a/src/bmad_loop/data/profiles/cline.toml b/src/bmad_loop/data/profiles/cline.toml new file mode 100644 index 000000000..269c504bb --- /dev/null +++ b/src/bmad_loop/data/profiles/cline.toml @@ -0,0 +1,27 @@ +# Cline CLI, using file-based hooks in .cline/hooks/. +# init writes the JSON registration shim at config_path and executable scripts +# for each mapped native event. --yolo would disable Cline's hook forwarding, +# so unattended runs use --auto-approve true instead. +name = "cline" +binary = "cline" +prompt_template = "{prompt}" +bypass_args = ["--auto-approve", "true"] +model_flag = "--model" +usage_parser = "none" +skill_tree = ".agents/skills" +seed_files = [ + ".cline/hooks/TaskStart.sh", + ".cline/hooks/TaskComplete.sh", + ".cline/hooks/SessionShutdown.sh", + ".cline/hooks/TaskError.sh", +] +first_run_note = "run `cline auth` once in this project and choose a provider and model" + +[hooks] +dialect = "cline-file-hooks" +config_path = ".cline/hooks.json" +events = { TaskStart = "SessionStart", TaskComplete = "Stop", SessionShutdown = "SessionEnd", TaskError = "StopFailure" } + +[user_skills] +precedence = "unordered" +roots = ["~/.cline/skills", "~/.agents/skills"] diff --git a/src/bmad_loop/install.py b/src/bmad_loop/install.py index 97297c05b..47d8d83e1 100644 --- a/src/bmad_loop/install.py +++ b/src/bmad_loop/install.py @@ -68,6 +68,7 @@ # agy's .agents/hooks.json keys by hook NAME at the top level (not a "hooks" # wrapper); bmad-loop registers all its handlers under this single group. ANTIGRAVITY_HOOK_GROUP = "bmad-loop" +CLINE_FILE_HOOKS = "cline-file-hooks" # The bmad-loop-* skills bundled in the wheel (bmad_loop/data/skills/, mapped from # the repo-root `skills/` at build time; see :func:`skills_root`) that @@ -2672,6 +2673,26 @@ def _hook_command(project: Path, profile: CLIProfile, canonical_event: str) -> s return f"{get_process_host().shell_quote(str(_relay_executable()))} relay {canonical_event}" +def _cline_hook_script(native: str, command: str) -> str: + """Executable Cline file hook that relays one native event. + + Cline sends `taskId` and a workspace-root list, not bmad-loop's `session_id` + and `cwd`; jq maps both. Cline's `agent_error` carries a message object, so + it is copied to `error_details`, the field the relay forwards on StopFailure. + """ + del native # the filename carries the native event; the command carries the canonical one + return ( + "#!/usr/bin/env bash\n" + "set -euo pipefail\n" + "payload=$(cat)\n" + "printf '%s' \"$payload\" | jq " + "'. + {sessionId: (.sessionId // .taskId // \"\"), " + "cwd: (.cwd // .workspaceRoots[0] // \"\"), " + "error_details: ((.error.message? // .error? // \"\") | tostring)}' " + f"| {command}\n" + ) + + def _warn_unsafe_relay_path() -> None: """DW-346: print one advisory line when the relay executable ``init`` just registered carries shell metacharacters this host's hook quoting leaves @@ -3549,6 +3570,22 @@ def _register_hooks(project: Path, profile: CLIProfile) -> int: atomic_write_text( config_path, json.dumps(config, indent=2) + "\n", require_writable_target=True ) + if profile.hooks.dialect == CLINE_FILE_HOOKS: + hooks_dir = project / ".cline" / "hooks" + hooks_dir.mkdir(parents=True, exist_ok=True) + for native_event, command in registrations.items(): + if not native_event.isalnum(): + raise ProfileError( + f"unsafe Cline hook event name {native_event!r}; expected alphanumeric" + ) + script = hooks_dir / f"{native_event}.sh" + atomic_write_text( + script, + _cline_hook_script(native_event, command), + require_writable_target=True, + ) + script.chmod(0o755) + print(f" Cline hook scripts written: {hooks_dir}") print(f" hooks registered ({profile.name}): {config_path}") else: print(f" hooks already registered ({profile.name})") diff --git a/tests/test_install.py b/tests/test_install.py index b18797033..edfa1caab 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -1690,6 +1690,30 @@ def test_provision_worktree_lays_down_skills_and_hook(tmp_path): assert json.loads(next((wt / "events").glob("*.json")).read_text())["event"] == "Stop" +def test_cline_registration_writes_executable_hook_scripts(tmp_path): + """Cline discovers hooks as files, so init must lay them down with the relay. + + The scripts map taskId to sessionId and workspace root to cwd, and carry the + agent_error message in error_details so StopFailure keeps its reason. + """ + cline = get_profile("cline") + assert _register_hooks(tmp_path, cline) == 0 + hooks = tmp_path / ".cline" / "hooks" + scripts = sorted(hooks.glob("*.sh")) + assert [p.name for p in scripts] == [ + "SessionShutdown.sh", + "TaskComplete.sh", + "TaskError.sh", + "TaskStart.sh", + ] + start = (hooks / "TaskStart.sh").read_text() + assert "relay SessionStart" in start + assert "sessionId" in start and "cwd" in start + error = (hooks / "TaskError.sh").read_text() + assert "relay StopFailure" in error and "error_details" in error + assert all(path.stat().st_mode & 0o111 for path in scripts) + + def test_provision_worktree_rewrites_seeded_relative_hook_to_absolute(tmp_path): """The main repo's .claude/settings.json carries a $CLAUDE_PROJECT_DIR-relative relay command. Seeded into a worktree, that variable resolves to the worktree, diff --git a/tests/test_profile.py b/tests/test_profile.py index dd7f55a8a..af5074fe8 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -72,6 +72,11 @@ def test_builtin_profiles_load(): assert profiles["copilot"].usage_parser == "copilot-events" # agy 1.4.9 writes per-step counts into its transcript assert profiles["antigravity"].usage_parser == "antigravity-transcript" + # Cline uses file hooks; init writes executable scripts beside the JSON shim + assert "cline" in profiles + assert profiles["cline"].hooks.dialect == "cline-file-hooks" + assert profiles["cline"].bypass_args == ("--auto-approve", "true") + assert ".cline/hooks/TaskStart.sh" in profiles["cline"].seed_files # copilot confirms tool calls, file paths and URL fetches separately; an # unattended session needs all three bypassed, or a URL fetch parks it assert profiles["copilot"].bypass_args == ( @@ -289,7 +294,10 @@ def test_claude_maps_its_stop_failure_types(): for name, prof in load_profiles().items(): if name != "claude": assert prof.hooks.stop_failure_types == {}, name - assert "StopFailure" not in prof.hooks.events.values(), name + # Cline maps agent_error to StopFailure and forwards its message in + # error_details; other adapters have no StopFailure carrier. + if name != "cline": + assert "StopFailure" not in prof.hooks.events.values(), name assert prof.hooks.event_min_versions == {}, name @@ -2263,6 +2271,10 @@ def test_default_profile_declares_no_extra_trees_and_no_user_skills(tmp_path): UserSkillRoot("~/.agents/skills", ("COPILOT_HOME",)), ), ), + "cline": UserSkillsSpec( + "unordered", + (UserSkillRoot("~/.cline/skills"), UserSkillRoot("~/.agents/skills")), + ), "opencode-http": UserSkillsSpec( "project", (