From 6d5f96b787c6dd84a2bbc65f1b9f9bbcd972de6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Sun, 22 Nov 2026 22:15:31 +0100 Subject: [PATCH 1/2] feat: create and maintain config.toml once per plugin version (YOU-43) A SessionStart hook creates ~/.config/youtrack-task/config.toml from config.example.toml, appends options added by an update (commented out) and removes options the plugin dropped, judged against a snapshot of the previous example. The work runs once per plugin version, so normal sessions are unaffected. --- .claude-plugin/marketplace.json | 2 +- .claude-plugin/plugin.json | 2 +- CHANGELOG.md | 14 +++ README.md | 2 +- SPEC.md | 22 +++- config.example.toml | 7 +- docs/configuration.md | 28 ++++- docs/troubleshooting.md | 1 + hooks/hooks.json | 14 ++- hooks/sync-config | 152 ++++++++++++++++++++++++++ tests/README.md | 7 +- tests/sync-config.sh | 186 ++++++++++++++++++++++++++++++++ 12 files changed, 425 insertions(+), 12 deletions(-) create mode 100755 hooks/sync-config create mode 100755 tests/sync-config.sh diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index 7be93c7..fd6419a 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -8,7 +8,7 @@ { "name": "youtrack-task", "description": "Create or pick up a JetBrains YouTrack issue from any repo: branch, move to In Progress, plan, ship a PR, and write everything back to the issue.", - "version": "0.20.0", + "version": "0.21.0", "source": "./", "author": { "name": "Jeffry Wuermli" diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index 5ded09d..b923998 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "youtrack-task", "description": "Create or pick up a JetBrains YouTrack issue from any repo: branch, move to In Progress, plan, ship a PR, and write everything back to the issue.", - "version": "0.20.0", + "version": "0.21.0", "author": { "name": "Jeffry Wuermli" }, diff --git a/CHANGELOG.md b/CHANGELOG.md index 4129a11..bc87386 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ All notable changes to this project are documented here. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.21.0 — 2026-11-22 + +- **The config file creates and maintains itself.** A `SessionStart` hook (`hooks/sync-config`) + runs once per plugin version, on the first Claude Code start after an install or update. + Without `~/.config/youtrack-task/config.toml` it creates the file from `config.example.toml`, + so you start from one file with every option. With one, it appends the options the new version + added (commented out, with their comment) and removes the ones it dropped, judged against a + snapshot of the previous example. Your own values, keys and comments are never touched, a + `config.toml.bak` is written before any change, and one line tells you what happened. An + unchanged version exits at once; `read-config` stays read-only. The first update after + adopting this removes nothing, because there is no earlier snapshot. +- `tests/sync-config.sh` covers create, no-op, add, prune, idempotence, backup, the + `[type_prefix]` block, the real example and the read-only case. + ## 0.20.0 — 2026-11-22 - **Chat language follows the session, else the issue.** The skill now has a rule for what it diff --git a/README.md b/README.md index 2177698..66b7c85 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,7 @@ On pickup the plugin: |:-:|------|--------| | 🚀 | [Usage](docs/usage.md) | Pickup steps, flags, creating issues, parallel worktrees, sub-commands | | 🤖 | [Batch (`all`)](docs/batch.md) | Triage, workers, review loop, `--auto-merge`, guard hook | -| ⚙️ | [Configuration](docs/configuration.md) | Config keys with defaults, extra reviewer commands, what lives where, MCP tools used | +| ⚙️ | [Configuration](docs/configuration.md) | Config keys with defaults, how the config file is created and kept up to date, extra reviewer commands, what lives where, MCP tools used | | 🧩 | [Other agents](docs/other-agents.md) | Cursor, Copilot, Codex and the Agensi package: MCP setup and what differs | | 🩺 | [Troubleshooting](docs/troubleshooting.md) | Symptoms and fixes, grouped by topic | diff --git a/SPEC.md b/SPEC.md index 52380cc..e1bbcdb 100644 --- a/SPEC.md +++ b/SPEC.md @@ -32,7 +32,8 @@ youtrack-task/ │ └── marketplace.json # so `/plugin marketplace add JtheGunner/youtrack-task` works ├── .mcp.json # YouTrack HTTP MCP server, values from env vars ├── hooks/ -│ ├── hooks.json # PreToolUse: YouTrack `create_issue`; Bash, Edit, Write, MultiEdit from `all` subagents (timeout 10 s) +│ ├── hooks.json # SessionStart (startup): sync-config; PreToolUse: YouTrack `create_issue`; Bash, Edit, Write, MultiEdit from `all` subagents (timeout 10 s) +│ ├── sync-config # once per plugin version: create config.toml from config.example.toml, or add new options (commented) and remove obsolete ones │ ├── require-assignee # rejects a create_issue without Assignee (Claude Code only) │ ├── guard-subagent # confines ticket-worker / ticket-reviewer (push, merge, read-only; worker file writes only inside its worktree) │ └── require-review # opt-in (review_gate_hook): no push / gh pr create of an issue branch without a review stamp @@ -596,6 +597,25 @@ the user. `config.example.toml` in the repo is this block with every line commented and dummy values — no real query, no real state names beyond YouTrack defaults. +**Keeping the file in step (`hooks/sync-config`).** A plugin `SessionStart` hook +(matcher `startup`) runs its work once per plugin version: the last synced version is +stamped in `${CLAUDE_PLUGIN_DATA}`, and an unchanged version exits at once, so normal +sessions are unaffected. On a new version (first install or an update): + +| `config.toml` | The hook | +| --- | --- | +| missing | copies `config.example.toml` there | +| present | appends the example's options it lacks, commented out (`# key = default` with their comment; the `[type_prefix]` table as a block when absent altogether), and removes options the example no longer has | + +An option counts as present when it appears active or commented. Removal uses a +snapshot of the previous example kept next to the stamp: only options that snapshot +shipped are removed (the active and the commented line, plus the comment lines directly +above that match the snapshot). Your own keys and comments are never touched, and the +first run without a snapshot removes nothing. `config.toml.bak` is written before any +change, and the hook shows one line saying what it created, added or removed. It never +fails a session: when it cannot write it warns and leaves the stamp unset, so the next +session tries again. `read-config` stays read-only. + ## Error handling | Situation | Behavior | diff --git a/config.example.toml b/config.example.toml index d9af9eb..d8f2a89 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,8 +1,9 @@ # youtrack-task — optional configuration # -# Copy to ~/.config/youtrack-task/config.toml and uncomment what you want to -# override. Every key is optional; the defaults below are what the skill uses if -# this file is absent. +# The plugin creates ~/.config/youtrack-task/config.toml from this file on first +# start and, after each update, adds new options (commented out) and removes +# obsolete ones. Uncomment what you want to override. Every key is optional; the +# defaults below are what the skill uses if the file is absent. # # The YouTrack URL and API token are NOT configured here — they are environment # variables (YOUTRACK_MCP_URL, YOUTRACK_TOKEN) read by the bundled MCP server. diff --git a/docs/configuration.md b/docs/configuration.md index 63ffa58..1247260 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -14,9 +14,10 @@ and `${YOUTRACK_TOKEN}`. Nothing instance-specific is stored in this repo. ## Config overrides -Optional and only needed if your workflow differs from the defaults. Copy -[`config.example.toml`](../config.example.toml) to -`~/.config/youtrack-task/config.toml` and uncomment what you need. +Optional and only needed if your workflow differs from the defaults. The plugin +creates `~/.config/youtrack-task/config.toml` for you from +[`config.example.toml`](../config.example.toml) (see +[Config file upkeep](#config-file-upkeep)); uncomment what you need. > [!NOTE] > Arrays such as `worktree_clone` must be written on a **single line**. A multi-line @@ -93,6 +94,26 @@ ddev exec vendor/bin/pest make verify ``` +## Config file upkeep + +The plugin keeps `~/.config/youtrack-task/config.toml` in step with +[`config.example.toml`](../config.example.toml), once per plugin version: the first +Claude Code start after you install or update it (a `SessionStart` hook, +`hooks/sync-config`). Sessions on an unchanged version are not affected. + +| Your `config.toml` | What happens | +|--------------------|--------------| +| missing | It is created from the example, with every option and its comment, all commented out. | +| present, options added in the new version | They are appended at the end, commented out (`# key = default`) with their comment, under a `# --- added by youtrack-task … ---` line. | +| present, options removed in the new version | Those options (active or commented) are removed, with the comment lines the plugin wrote above them. | + +Options count as present when they appear active or commented, so a second run changes +nothing. Your own values, keys and comments are never touched. Before any change the +hook writes `config.toml.bak`, and it shows one line saying what it created, added or +removed. The first update after you adopt this feature removes nothing, because there +is no earlier example to compare against. If the config directory is not writable, the +hook warns and tries again next session. + ## How config is resolved `scripts/read-config` is step 0 of every invocation. It reads your `config.toml`, @@ -110,6 +131,7 @@ read the file. | 🔐 | YouTrack URL | `YOUTRACK_MCP_URL` env var | 🚫 | | 🔐 | API token | `YOUTRACK_TOKEN` env var | 🚫 | | 🔐 | Which project an issue is in | Encoded in the issue ID (`INFRA-42`) | 🚫 | +| ⚙️ | The config file itself: created, extended and pruned once per plugin version | `hooks/sync-config`, state in the plugin data dir | ✅ | | ⚙️ | State names, list query, prefix map, branch-name and PR confirmation, superpowers / review / worktree settings | Defaults in the skill; overrides in `~/.config/youtrack-task/config.toml` | ✅ defaults only | | ⚙️ | Extra reviewer commands (`reviewer_commands`; per project: `.youtrack-task/reviewer-commands`, read from the base branch) | `~/.config/youtrack-task/config.toml`; the project file in the repository | ✅ defaults only | | ⚙️ | `all` settings (`batch_query`, `batch_max_parallel`, `batch_skip_tags`; review rounds via `review_rounds`) | Defaults in the skill; overrides in `~/.config/youtrack-task/config.toml` | ✅ defaults only | diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index faa19bd..bf80c74 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -17,6 +17,7 @@ Find your symptom, apply the fix. Back to the [README](../README.md). | "YouTrack MCP server isn't connected" | `YOUTRACK_MCP_URL` / `YOUTRACK_TOKEN` are not set in the shell that launched Claude Code, or your proxy blocks `/mcp`. Check `curl -H "Authorization: Bearer $YOUTRACK_TOKEN" "$YOUTRACK_MCP_URL"`. | | MCP connects but writes fail with 403 | The token's user lacks permission on that project, or the token scope is wrong. | | "There is no project named …" although the project exists | Fixed in 0.15.1: the skill searches by repo name first and, if needed, pages through *all* projects (the YouTrack MCP server returns only 20 per page) before claiming a project is missing. Update the plugin (`/plugin marketplace update youtrack-task`, then `/plugin update youtrack-task@youtrack-task`) and restart Claude Code. A project whose name differs from the repo name can always be named with `--project KEY`. | +| `config.toml` was not created or has no new options after an update | The sync runs once per plugin version at the start of a Claude Code session, so restart Claude Code after the update. A warning *"could not update …"* at the start means the config directory is not writable; fix that and start a new session, it tries again. Your previous file is kept as `config.toml.bak` whenever the sync changed something. | | Scripts reported as missing (`SCRIPTS=missing`) | The skill was loaded without its `scripts/` directory, so it falls back to plain git in place. Install it as a plugin (or copy the whole skill directory) so `‹skill-dir›/scripts/read-config` exists. | | Issues still come out in English although you write German | Almost always a stale plugin: third-party marketplaces don't auto-update. Run `/plugin marketplace update youtrack-task` then `/plugin update youtrack-task@youtrack-task`, restart Claude Code, and check the version under `/plugin`. Enable *Auto-update* for the marketplace in `/plugin` → Marketplaces to stop it recurring. If it persists, name the language in your input ("auf Deutsch"): an explicit instruction always wins. | diff --git a/hooks/hooks.json b/hooks/hooks.json index 2fe66ba..564f66f 100644 --- a/hooks/hooks.json +++ b/hooks/hooks.json @@ -1,6 +1,18 @@ { - "description": "Guards direct YouTrack create_issue calls (an Assignee must always be set, text in the user's language), confines the `all` sub-command's worker and reviewer subagents (no pushes or merges, read-only reviewer, worker file writes only inside its worktree), and — only when review_gate_hook is on — blocks pushing or opening a PR for an issue branch without a review stamp.", + "description": "Once per plugin version, creates ~/.config/youtrack-task/config.toml from config.example.toml or syncs it (adds new options commented out, removes obsolete ones). Guards direct YouTrack create_issue calls (an Assignee must always be set, text in the user's language), confines the `all` sub-command's worker and reviewer subagents (no pushes or merges, read-only reviewer, worker file writes only inside its worktree), and — only when review_gate_hook is on — blocks pushing or opening a PR for an issue branch without a review stamp.", "hooks": { + "SessionStart": [ + { + "matcher": "startup", + "hooks": [ + { + "type": "command", + "command": "bash \"${CLAUDE_PLUGIN_ROOT}/hooks/sync-config\"", + "timeout": 10 + } + ] + } + ], "PreToolUse": [ { "matcher": "mcp__.*youtrack.*__create_issue", diff --git a/hooks/sync-config b/hooks/sync-config new file mode 100755 index 0000000..d267f60 --- /dev/null +++ b/hooks/sync-config @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# SessionStart hook: keep the user's config.toml in step with config.example.toml. +# +# Runs its work once per plugin version: the last synced version is stamped in the +# plugin data dir, which survives updates. An unchanged version exits at once +# without touching anything, so the hook has no effect on normal sessions. +# +# On a new version (first install or an update): +# - config.toml missing -> copy config.example.toml there. +# - config.toml present -> append the example's options it lacks, commented out +# (`# key = default`, with their comment), and remove the +# options the example no longer has. An option counts as +# present when it appears active or commented. Removal is +# driven by a snapshot of the previous example, so only +# options this plugin once shipped are ever removed. +# Your own keys and comments are never touched. A backup +# (config.toml.bak) is written before any change. +# +# Best effort: it never fails a session. When it cannot write, it shows a warning +# and leaves the stamp unset so the next session tries again. Needs bash, sed, awk, +# cmp; no jq. +# +# Environment: CLAUDE_PLUGIN_ROOT, CLAUDE_PLUGIN_DATA (set by Claude Code), +# YOUTRACK_TASK_CONFIG (config path override, as in scripts/read-config). +set -u + +ROOT="${CLAUDE_PLUGIN_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}" +DATA="${CLAUDE_PLUGIN_DATA:-${XDG_STATE_HOME:-$HOME/.local/state}/youtrack-task}" +CFG="${YOUTRACK_TASK_CONFIG:-$HOME/.config/youtrack-task/config.toml}" +EXAMPLE="$ROOT/config.example.toml" + +version=$(sed -nE 's/.*"version"[[:space:]]*:[[:space:]]*"([^"]+)".*/\1/p' "$ROOT/.claude-plugin/plugin.json" 2>/dev/null | head -1) +[ -n "$version" ] && [ -f "$EXAMPLE" ] || exit 0 + +STAMP="$DATA/config-synced-version" +SNAPSHOT="$DATA/config.example.snapshot" +[ "$(cat "$STAMP" 2>/dev/null)" = "$version" ] && exit 0 + +tmp=$(mktemp -d 2>/dev/null) || exit 0 +trap 'rm -rf "$tmp"' EXIT + +say() { # $1 = message ; shown to the user, never to the model's task + local m=${1//\\/\\\\} + m=${m//\"/\\\"} + printf '{"systemMessage":"youtrack-task: %s"}\n' "$m" +} + +give_up() { + say "could not update $CFG ($1); will try again next session." + exit 0 +} + +# entries : the options of a config file as a stream. Each option starts with +# "@@KEY ", followed by its comment lines and its own line. The [type_prefix] +# table is one option whose body runs to the next blank line. +entries() { + awk ' + /^[[:space:]]*$/ { pending = ""; intable = 0; next } + intable { print; next } + /^#[[:space:]]*\[[a-z_]+\][[:space:]]*$/ { + key = $0; sub(/^#[[:space:]]*/, "", key); sub(/[[:space:]]*$/, "", key) + print "@@KEY " key; printf "%s", pending; print; pending = ""; intable = 1; next + } + /^#?[[:space:]]*[a-z_]+[[:space:]]*=/ { + key = $0; sub(/^#?[[:space:]]*/, "", key); sub(/[[:space:]]*=.*/, "", key) + print "@@KEY " key; printf "%s", pending; print; pending = ""; next + } + { pending = pending $0 "\n" } + ' "$1" +} + +keys_of() { entries "$1" | sed -n 's/^@@KEY //p'; } + +entry_text() { # $1 = file, $2 = key + entries "$1" | awk -v k="$2" '/^@@KEY /{ on = ($0 == "@@KEY " k); next } on' +} + +is_present() { # $1 = file, $2 = key + case "$2" in + \[*) grep -Eq "^#?[[:space:]]*\\[${2#[}[[:space:]]*" "$1" ;; + *) grep -Eq "^#?[[:space:]]*$2[[:space:]]*=" "$1" ;; + esac +} + +remove_key() { # $1 = file (rewritten in place), $2 = key, $3 = file with the key's old entry text + awk -v key="$2" -v entry="$3" ' + BEGIN { while ((getline l < entry) > 0) T[++m] = l } + { U[NR] = $0 } + END { + kre = "^#?[[:space:]]*" key "[[:space:]]*=" + for (i = 1; i <= NR; i++) if (U[i] ~ kre) { + gone[i] = 1 + j = i - 1; t = m - 1 + while (t >= 1 && j >= 1 && !(j in gone) && U[j] == T[t]) { gone[j] = 1; j--; t-- } + } + for (i = 1; i <= NR; i++) if (!(i in gone)) print U[i] + } + ' "$1" > "$1.out" && cat "$1.out" > "$1" +} + +join() { local IFS=,; echo "$*" | sed 's/,/, /g'; } + +created=""; added=(); removed=() + +if [ ! -f "$CFG" ]; then + mkdir -p "$(dirname "$CFG")" 2>/dev/null && cp "$EXAMPLE" "$CFG" 2>/dev/null || give_up "cannot create it" + created=1 +else + cp "$CFG" "$tmp/new" || give_up "cannot read it" + + if [ -f "$SNAPSHOT" ]; then + keys_of "$EXAMPLE" | sort > "$tmp/current-keys" + while IFS= read -r key; do + case "$key" in \[*) continue ;; esac + grep -qxF -- "$key" "$tmp/current-keys" && continue + is_present "$tmp/new" "$key" || continue + entry_text "$SNAPSHOT" "$key" > "$tmp/entry" + remove_key "$tmp/new" "$key" "$tmp/entry" && removed+=("$key") + done < <(keys_of "$SNAPSHOT") + fi + + block="" + while IFS= read -r key; do + is_present "$tmp/new" "$key" && continue + block="${block} +$(entry_text "$EXAMPLE" "$key") +" + case "$key" in \[*) ;; *) added+=("$key") ;; esac + done < <(keys_of "$EXAMPLE") + + if [ -n "$block" ]; then + [ -z "$(tail -c1 "$tmp/new")" ] || echo >> "$tmp/new" + { printf '\n# --- added by youtrack-task %s (config sync) ---\n' "$version"; printf '%s\n' "$block" | sed '1{/^$/d;}'; } >> "$tmp/new" + fi + + if ! cmp -s "$CFG" "$tmp/new"; then + cp "$CFG" "$CFG.bak" 2>/dev/null || give_up "cannot write a backup next to it" + cat "$tmp/new" > "$CFG" 2>/dev/null || give_up "cannot write it" + else + added=(); removed=() + fi +fi + +mkdir -p "$DATA" 2>/dev/null && cp "$EXAMPLE" "$SNAPSHOT" 2>/dev/null && printf '%s\n' "$version" > "$STAMP" 2>/dev/null \ + || give_up "cannot write the sync state to $DATA" + +parts=() +[ -n "$created" ] && parts+=("created $CFG with all options.") +[ "${#added[@]}" -gt 0 ] && parts+=("added new options to $CFG, commented out: $(join "${added[@]}").") +[ "${#removed[@]}" -gt 0 ] && parts+=("removed obsolete options from $CFG: $(join "${removed[@]}") (backup: $CFG.bak).") +[ "${#parts[@]}" -gt 0 ] && say "${parts[*]}" +exit 0 diff --git a/tests/README.md b/tests/README.md index 87e840c..04107bf 100644 --- a/tests/README.md +++ b/tests/README.md @@ -132,7 +132,7 @@ Use a throwaway or low-stakes issue. | 75 | more than 20 matching tickets, `all --dry-run` | `search_issues` is called repeatedly with `offset` 0, 20, … until a short page; all tickets are in the table | | 76 | no candidate is *ready*, `all` | the table is printed and the run stops; no question such as "should I run X instead?" | | 77 | `tests/setup-workspace.sh` | every case prints `ok`, ends with `ALL OK`; exit 0 (a base that exists only locally is the start point in place, in a linked worktree and in a new worktree, even when HEAD is another branch; `origin/` beats a diverged local ``; an unresolvable base gives `ERROR=cannot-resolve-base` (exit 2) with no branch or worktree created; a custom `worktree_dir` is created and resumed) | -| 78 | `shellcheck -S warning hooks/guard-subagent hooks/require-review hooks/require-assignee skills/youtrack-task/scripts/*` | no output, exit 0 (intentional exceptions carry a `# shellcheck disable=… # reason` comment) | +| 78 | `shellcheck -S warning hooks/guard-subagent hooks/require-review hooks/require-assignee hooks/sync-config skills/youtrack-task/scripts/*` | no output, exit 0 (intentional exceptions carry a `# shellcheck disable=… # reason` comment) | | 79 | open a PR against `main` | the `ci` workflow runs `tests/*.sh`, ShellCheck, `jq empty` on all JSON files and the plugin/marketplace version check; all green | | 80 | bump only `plugin.json` (not `marketplace.json`) and push | the `ci` job fails on the version-consistency step | | 81 | push a `v*` tag whose version differs from either manifest, or with a failing `ci` | the release workflow stops before building the ZIP | @@ -179,3 +179,8 @@ Use a throwaway or low-stakes issue. | 122 | session where you first wrote in English, then `/youtrack-task ` for a German issue | `→ Chat language: English (session)`; replies stay English; the plan comment on the issue is German | | 123 | fresh session, `/youtrack-task ` plus the instruction "antworte auf Englisch" in the same message, German issue | `→ Chat language: English (your instruction)` | | 124 | `tests/skill-text.sh` (chat language) | the Language section has the `Chat replies` rule keyed on an earlier user message, step 2 prints `→ Chat language:`, no `reference/*.md` repeats the rule, `SKILL.md` stays within 400 lines | +| 125 | `tests/sync-config.sh` | every case prints `ok`; exit 0 (fresh create, same version is a no-op, new options appended commented with their comment, idempotent re-run, removed options pruned only when the previous example shipped them, own keys / comments / values untouched, `.bak`, `[type_prefix]` block, the real `config.example.toml` stays stable, read-only dir warns without stamp, broken plugin root exits 0) | +| 126 | install the plugin with no `~/.config/youtrack-task/config.toml`, restart Claude Code | one line at session start says the config was created; the file has every option, commented out, with its comment | +| 127 | after 126, edit one value, then update to a version that adds an option and drops another, restart | one line names the added and removed options; the new option is appended commented, the dropped one is gone, your edited value and own comments are intact, `config.toml.bak` holds the old file; a second restart prints nothing | +| 128 | make `~/.config/youtrack-task` read-only, install or update, restart | a warning *"could not update …"* appears and the session works normally; after fixing the permissions the next start syncs | +| 129 | `jq empty hooks/hooks.json`, then inspect it | a `SessionStart` entry with matcher `startup` runs `hooks/sync-config`, `timeout: 10` | diff --git a/tests/sync-config.sh b/tests/sync-config.sh new file mode 100755 index 0000000..0b6deeb --- /dev/null +++ b/tests/sync-config.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +# Cases for hooks/sync-config. +# Usage: tests/sync-config.sh [path-to-hook] +# Prints one line per case and exits non-zero if any case differs from the +# expectation. Every case runs against a throwaway plugin root, data dir and +# config file under a temp dir; nothing outside it is touched. +set -u +root=$(cd "$(dirname "$0")/.." && pwd) +hook=${1:-$root/hooks/sync-config} +fail=0 +tmp=$(mktemp -d) +trap 'chmod -R u+rwx "$tmp" 2>/dev/null; rm -rf "$tmp"' EXIT + +ok() { echo "ok $1"; } +bad() { echo "FAIL $1"; [ $# -gt 1 ] && printf '%s\n' "$2" | sed 's/^/ | /'; fail=1; } + +# fixture -> sets plugin / data / cfg for a fresh sandbox +fixture() { + plugin=$tmp/$1/plugin; data=$tmp/$1/data; cfg=$tmp/$1/home/config.toml + mkdir -p "$plugin/.claude-plugin" "$data" "$tmp/$1/home" +} + +# release -- writes plugin.json; the example comes from "$example" +release() { + printf '{\n "name": "youtrack-task",\n "version": "%s"\n}\n' "$1" > "$plugin/.claude-plugin/plugin.json" + printf '%s\n' "$example" > "$plugin/config.example.toml" +} + +# run_hook -> sets out, status +run_hook() { + out=$(CLAUDE_PLUGIN_ROOT=$plugin CLAUDE_PLUGIN_DATA=$data YOUTRACK_TASK_CONFIG=$cfg bash "$hook" &1) + status=$? +} + +has() { grep -qxF -- "$2" "$1" && ok "$3" || bad "$3 (want line '$2' in $1)" "$(cat "$1" 2>/dev/null)"; } +lacks() { grep -qF -- "$2" "$1" && bad "$3 (unexpected '$2')" "$(cat "$1")" || ok "$3"; } +says() { printf '%s' "$out" | grep -qF -- "$1" && ok "$2" || bad "$2 (want '$1')" "$out"; } +silent() { [ -z "$out" ] && ok "$1" || bad "$1 (got output)" "$out"; } + +example_v1='# youtrack-task — optional configuration +# +# Every key is optional. + +# Query used to list candidate issues. +# list_query = "for: me" + +# State names on your ladder. +# in_progress_state = "In Progress" # set on pickup +# done_state = "Done" # target of done + +# Isolate each pickup in a worktree. +# worktree = "auto" + +# Branch prefix per Type. +# [type_prefix] +# Bug = "fix" +# Task = "chore"' + +# 1. A missing config is created from the example ------------------------------ +fixture fresh; example=$example_v1; release 0.1.0 +run_hook +[ "$status" -eq 0 ] && ok "fresh: exit 0" || bad "fresh: exit 0 (got $status)" +cmp -s "$cfg" "$plugin/config.example.toml" && ok "fresh: config equals the example" || bad "fresh: config equals the example" "$(cat "$cfg" 2>/dev/null)" +says "created" "fresh: message says created" +has "$data/config-synced-version" "0.1.0" "fresh: version stamp written" +cmp -s "$data/config.example.snapshot" "$plugin/config.example.toml" && ok "fresh: snapshot written" || bad "fresh: snapshot written" + +# 2. Same version: nothing happens, not even a read of the config --------------- +printf '# my own note\nworktree = "always"\n' > "$cfg" +run_hook +silent "same version: no output" +[ "$(cat "$cfg")" = "$(printf '# my own note\nworktree = "always"')" ] && ok "same version: config untouched" || bad "same version: config untouched" + +# 3. Update adds missing keys, commented, with their comment; nothing else moves +fixture update; example=$example_v1; release 0.1.0 +cat > "$cfg" <<'EOF' +# my own note +list_query = "for: me #Mine" +worktree = "always" +EOF +run_hook # first run on an existing config: no snapshot yet +example="$example_v1 + +# Ask before the PR. +# confirm_pr = true" +release 0.2.0 +run_hook +says "confirm_pr" "update: message names the added key" +has "$cfg" "# confirm_pr = true" "update: new key appended commented" +has "$cfg" "# Ask before the PR." "update: its comment comes along" +has "$cfg" 'list_query = "for: me #Mine"' "update: user value kept" +has "$cfg" 'worktree = "always"' "update: second user value kept" +has "$cfg" "# my own note" "update: user comment kept" +[ "$(grep -c 'worktree = "auto"' "$cfg")" -eq 0 ] && ok "update: present key is not added again" || bad "update: present key is not added again" "$(cat "$cfg")" +[ -f "$cfg.bak" ] && ok "update: backup written" || bad "update: backup written" +has "$data/config-synced-version" "0.2.0" "update: version stamp moved" + +# 4. Idempotent: a new version with an unchanged example changes nothing -------- +before=$(cat "$cfg"); bak_before=$(cat "$cfg.bak") +release 0.3.0 +run_hook +silent "idempotent: no output" +[ "$(cat "$cfg")" = "$before" ] && ok "idempotent: config unchanged" || bad "idempotent: config unchanged" "$(cat "$cfg")" +[ "$(cat "$cfg.bak")" = "$bak_before" ] && ok "idempotent: backup not rewritten" || bad "idempotent: backup not rewritten" +has "$data/config-synced-version" "0.3.0" "idempotent: stamp still moves" + +# 5. A key dropped from the example is removed, with its own comment only ------ +fixture remove +example="# header + +# Old thing. +# old_key = 1 + +# Kept thing. +# kept_key = 2" +release 1.0.0 +cat > "$cfg" <<'EOF' +# my own note about old_key +# Old thing. +old_key = 5 +# Old thing. +# old_key = 1 + +# Kept thing. +kept_key = 7 +my_key = 9 +EOF +run_hook # no snapshot yet: nothing may be removed +has "$cfg" "old_key = 5" "no snapshot: nothing removed" +example="# header + +# Kept thing. +# kept_key = 2" +release 1.1.0 +run_hook +says "old_key" "remove: message names the removed key" +lacks "$cfg" "old_key = 5" "remove: active line gone" +lacks "$cfg" "# old_key = 1" "remove: commented line gone" +lacks "$cfg" "# Old thing." "remove: its comment gone" +has "$cfg" "# my own note about old_key" "remove: user's own comment kept" +has "$cfg" "kept_key = 7" "remove: kept key keeps the user value" +has "$cfg" "my_key = 9" "remove: user's own key kept" +grep -qxF "old_key = 5" "$cfg.bak" && ok "remove: backup still has the old value" || bad "remove: backup still has the old value" + +# 6. The [type_prefix] table ---------------------------------------------------- +fixture table; example=$example_v1; release 0.1.0 +printf 'worktree = "always"\n' > "$cfg" +run_hook +[ "$(grep -c 'type_prefix' "$cfg")" -eq 1 ] && ok "table: missing block appended once" || bad "table: missing block appended once" "$(cat "$cfg")" +has "$cfg" '# Bug = "fix"' "table: block carries its rows" +fixture table2; example=$example_v1; release 0.1.0 +printf '[type_prefix]\nBug = "feat"\n' > "$cfg" +run_hook +lacks "$cfg" "# Bug" "table: an existing table is left alone" + +# 7. The real example: complete on creation, stable on re-run ------------------- +fixture real; example=$(cat "$root/config.example.toml"); release 9.0.0 +run_hook +cmp -s "$cfg" "$plugin/config.example.toml" && ok "real example: created verbatim" || bad "real example: created verbatim" +release 9.0.1 +run_hook +silent "real example: every key is recognised as present" +cmp -s "$cfg" "$plugin/config.example.toml" && ok "real example: re-run changes nothing" || bad "real example: re-run changes nothing" + +# 8. Not writable: warn, exit 0, no stamp so the next session retries ---------- +if [ "$(id -u)" -ne 0 ]; then + fixture ro; example=$example_v1; release 0.1.0 + chmod 555 "$tmp/ro/home" + run_hook + [ "$status" -eq 0 ] && ok "read-only dir: exit 0" || bad "read-only dir: exit 0 (got $status)" + says "could not" "read-only dir: warning shown" + [ ! -e "$data/config-synced-version" ] && ok "read-only dir: no stamp written" || bad "read-only dir: no stamp written" + chmod 755 "$tmp/ro/home" + run_hook + [ -f "$cfg" ] && ok "read-only dir: next run succeeds" || bad "read-only dir: next run succeeds" +else + echo "skip read-only cases (running as root)" +fi + +# 9. A broken plugin root never breaks the session ----------------------------- +fixture broken; example=$example_v1; release 0.1.0; rm "$plugin/config.example.toml" +run_hook +[ "$status" -eq 0 ] && ok "missing example: exit 0" || bad "missing example: exit 0 (got $status)" +[ ! -e "$cfg" ] && ok "missing example: no config invented" || bad "missing example: no config invented" + +exit $fail From b1d2807535a201e05c1d71e0ca6bf6af9cbb77e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Sun, 22 Nov 2026 22:18:28 +0100 Subject: [PATCH 2/2] fix: place synced options before the first config table (YOU-43) read-config ignores everything from the first active table header on, so an option appended after it would stop working once uncommented. Top-level options are now inserted before that header. --- CHANGELOG.md | 2 +- SPEC.md | 2 +- docs/configuration.md | 2 +- hooks/sync-config | 34 +++++++++++++++++++++++++++------- tests/README.md | 2 +- tests/sync-config.sh | 15 +++++++++++++++ 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc87386..669a302 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). runs once per plugin version, on the first Claude Code start after an install or update. Without `~/.config/youtrack-task/config.toml` it creates the file from `config.example.toml`, so you start from one file with every option. With one, it appends the options the new version - added (commented out, with their comment) and removes the ones it dropped, judged against a + added (commented out, with their comment, placed before any `[table]` header so they still work when uncommented) and removes the ones it dropped, judged against a snapshot of the previous example. Your own values, keys and comments are never touched, a `config.toml.bak` is written before any change, and one line tells you what happened. An unchanged version exits at once; `read-config` stays read-only. The first update after diff --git a/SPEC.md b/SPEC.md index e1bbcdb..738077d 100644 --- a/SPEC.md +++ b/SPEC.md @@ -605,7 +605,7 @@ sessions are unaffected. On a new version (first install or an update): | `config.toml` | The hook | | --- | --- | | missing | copies `config.example.toml` there | -| present | appends the example's options it lacks, commented out (`# key = default` with their comment; the `[type_prefix]` table as a block when absent altogether), and removes options the example no longer has | +| present | adds the example's options it lacks, commented out (`# key = default` with their comment) before the first active `[table]` header, since `read-config` ignores everything after it (the `[type_prefix]` table is appended as a block at the end when absent altogether), and removes options the example no longer has | An option counts as present when it appears active or commented. Removal uses a snapshot of the previous example kept next to the stamp: only options that snapshot diff --git a/docs/configuration.md b/docs/configuration.md index 1247260..5fd9a4f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -104,7 +104,7 @@ Claude Code start after you install or update it (a `SessionStart` hook, | Your `config.toml` | What happens | |--------------------|--------------| | missing | It is created from the example, with every option and its comment, all commented out. | -| present, options added in the new version | They are appended at the end, commented out (`# key = default`) with their comment, under a `# --- added by youtrack-task … ---` line. | +| present, options added in the new version | They are added commented out (`# key = default`) with their comment, under a `# --- added by youtrack-task … ---` line. They go before your first `[table]` header, because options below a table header are not read. | | present, options removed in the new version | Those options (active or commented) are removed, with the comment lines the plugin wrote above them. | Options count as present when they appear active or commented, so a second run changes diff --git a/hooks/sync-config b/hooks/sync-config index d267f60..6b23b43 100755 --- a/hooks/sync-config +++ b/hooks/sync-config @@ -119,18 +119,38 @@ else done < <(keys_of "$SNAPSHOT") fi - block="" + top=""; tables="" while IFS= read -r key; do is_present "$tmp/new" "$key" && continue - block="${block} -$(entry_text "$EXAMPLE" "$key") -" - case "$key" in \[*) ;; *) added+=("$key") ;; esac + text=$(entry_text "$EXAMPLE" "$key") + case "$key" in + \[*) tables="${tables}${text}"$'\n\n' ;; + *) top="${top}${text}"$'\n\n'; added+=("$key") ;; + esac done < <(keys_of "$EXAMPLE") - if [ -n "$block" ]; then + marker="# --- added by youtrack-task $version (config sync) ---" + + # Top-level options go before the first active [table] header: read-config ignores + # everything from there on, so an option appended after it would stop working the + # moment it is uncommented. + if [ -n "$top" ]; then + printf '%s\n%s' "$marker" "$top" > "$tmp/top" + if grep -Eq '^[[:space:]]*\[' "$tmp/new"; then + awk -v ins="$tmp/top" ' + BEGIN { while ((getline l < ins) > 0) block = block l "\n" } + !done && /^[[:space:]]*\[/ { if (NR > 1 && prev != "") printf "\n"; printf "%s", block; done = 1 } + { print; prev = $0 } + ' "$tmp/new" > "$tmp/new.out" && cat "$tmp/new.out" > "$tmp/new" + else + [ -z "$(tail -c1 "$tmp/new")" ] || echo >> "$tmp/new" + { echo; cat "$tmp/top"; } >> "$tmp/new" + fi + fi + + if [ -n "$tables" ]; then [ -z "$(tail -c1 "$tmp/new")" ] || echo >> "$tmp/new" - { printf '\n# --- added by youtrack-task %s (config sync) ---\n' "$version"; printf '%s\n' "$block" | sed '1{/^$/d;}'; } >> "$tmp/new" + { echo; echo "$marker"; printf '%s' "$tables"; } >> "$tmp/new" fi if ! cmp -s "$CFG" "$tmp/new"; then diff --git a/tests/README.md b/tests/README.md index 04107bf..b57d836 100644 --- a/tests/README.md +++ b/tests/README.md @@ -179,7 +179,7 @@ Use a throwaway or low-stakes issue. | 122 | session where you first wrote in English, then `/youtrack-task ` for a German issue | `→ Chat language: English (session)`; replies stay English; the plan comment on the issue is German | | 123 | fresh session, `/youtrack-task ` plus the instruction "antworte auf Englisch" in the same message, German issue | `→ Chat language: English (your instruction)` | | 124 | `tests/skill-text.sh` (chat language) | the Language section has the `Chat replies` rule keyed on an earlier user message, step 2 prints `→ Chat language:`, no `reference/*.md` repeats the rule, `SKILL.md` stays within 400 lines | -| 125 | `tests/sync-config.sh` | every case prints `ok`; exit 0 (fresh create, same version is a no-op, new options appended commented with their comment, idempotent re-run, removed options pruned only when the previous example shipped them, own keys / comments / values untouched, `.bak`, `[type_prefix]` block, the real `config.example.toml` stays stable, read-only dir warns without stamp, broken plugin root exits 0) | +| 125 | `tests/sync-config.sh` | every case prints `ok`; exit 0 (fresh create, same version is a no-op, new options appended commented with their comment, idempotent re-run, removed options pruned only when the previous example shipped them, own keys / comments / values untouched, `.bak`, `[type_prefix]` block, new options placed before an active table and readable by `read-config` once uncommented, the real `config.example.toml` stays stable, read-only dir warns without stamp, broken plugin root exits 0) | | 126 | install the plugin with no `~/.config/youtrack-task/config.toml`, restart Claude Code | one line at session start says the config was created; the file has every option, commented out, with its comment | | 127 | after 126, edit one value, then update to a version that adds an option and drops another, restart | one line names the added and removed options; the new option is appended commented, the dropped one is gone, your edited value and own comments are intact, `config.toml.bak` holds the old file; a second restart prints nothing | | 128 | make `~/.config/youtrack-task` read-only, install or update, restart | a warning *"could not update …"* appears and the session works normally; after fixing the permissions the next start syncs | diff --git a/tests/sync-config.sh b/tests/sync-config.sh index 0b6deeb..9c06ca1 100755 --- a/tests/sync-config.sh +++ b/tests/sync-config.sh @@ -153,6 +153,21 @@ printf '[type_prefix]\nBug = "feat"\n' > "$cfg" run_hook lacks "$cfg" "# Bug" "table: an existing table is left alone" +# 6b. New options go before an active table, so uncommenting them still works ---- +fixture early; example=$(cat "$root/config.example.toml"); release 5.0.0 +printf 'worktree = "always"\n\n[type_prefix]\nBug = "feat"\n' > "$cfg" +run_hook +header_line=$(grep -nx '\[type_prefix\]' "$cfg" | head -1 | cut -d: -f1) +gate_line=$(grep -nx '# review_gate_hook = "off"' "$cfg" | head -1 | cut -d: -f1) +{ [ -n "$gate_line" ] && [ -n "$header_line" ] && [ "$gate_line" -lt "$header_line" ]; } \ + && ok "active table: new option lands before the table header" \ + || bad "active table: new option lands before the table header" "$(cat "$cfg")" +[ "$(grep -c '^# --- added by' "$cfg")" -eq 1 ] && ok "active table: one marker" || bad "active table: one marker" "$(cat "$cfg")" +sed -i.tmp 's/^# review_gate_hook = "off"/review_gate_hook = "on"/' "$cfg" && rm -f "$cfg.tmp" +got=$(YOUTRACK_TASK_CONFIG=$cfg bash "$root/skills/youtrack-task/scripts/read-config" review_gate_hook) +[ "$got" = "on" ] && ok "active table: an uncommented new option is read by read-config" || bad "active table: an uncommented new option is read by read-config (got '$got')" +grep -qxF 'Bug = "feat"' "$cfg" && ok "active table: table rows untouched" || bad "active table: table rows untouched" + # 7. The real example: complete on creation, stable on re-run ------------------- fixture real; example=$(cat "$root/config.example.toml"); release 9.0.0 run_hook