From 0480f14977af1d70084880ee0c966e862e87a43a Mon Sep 17 00:00:00 2001 From: nivSwisa1 Date: Tue, 24 Nov 2026 17:36:52 +0200 Subject: [PATCH 1/3] LINBEE-30104 | fix: per-repo verdict gate, stage-2 hold, sandbox-safe verdict cache Co-Authored-By: Claude Opus 5.5 --- .../.claude-plugin/plugin.json | 2 +- plugins/agentic-advisor/CHANGELOG.md | 9 ++ .../hooks/agentic-advisor-trigger.sh | 74 +++++++++-- .../skills/agentic-advisor/SKILL.md | 23 ++-- tests/agentic-advisor/hooks_test.sh | 117 ++++++++++++++++-- 5 files changed, 193 insertions(+), 32 deletions(-) diff --git a/plugins/agentic-advisor/.claude-plugin/plugin.json b/plugins/agentic-advisor/.claude-plugin/plugin.json index 3fcf901..0b05eb7 100644 --- a/plugins/agentic-advisor/.claude-plugin/plugin.json +++ b/plugins/agentic-advisor/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "agentic-advisor", - "version": "1.5.2", + "version": "1.5.3", "description": "Before writing code, grades how fragile the target is from LinearB health signals + local git history (rework, incidents, fix/revert density, code ownership) and reports whether it Reduced, Kept or Raised effort against the model's current level — lean on calm repos, defensive on fragile ones. Auto-triggers via a bundled hook and can report each effort decision to your LinearB org as usage telemetry.", "author": { "name": "LinearB", diff --git a/plugins/agentic-advisor/CHANGELOG.md b/plugins/agentic-advisor/CHANGELOG.md index d5a709a..c17aadb 100644 --- a/plugins/agentic-advisor/CHANGELOG.md +++ b/plugins/agentic-advisor/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [1.5.3] - 2026-11-24 + +### Fixed + +- With Claude Code's sandbox on, the 24-hour repo-health cache couldn't be written to `~/.cache` ("operation not permitted"), so every task re-ran the LinearB calls. The skill now falls back to `$TMPDIR`, which the sandbox allows. +- A verdict now holds or releases edits only in the repo it names, so grading one repo no longer blocks or unblocks edits in another in the same session. A "skipped (not a code task)" line releases edits in its own repo only. +- On a medium or higher grade with no target file named, the first edit waits once for the file-level (phase 2) line. +- A verdict containing an apostrophe is shown in full: the hook reads the printed line, not the shell source. + ## [1.5.2] - 2026-11-10 ### Fixed diff --git a/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh b/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh index 4b46c53..ec87e54 100755 --- a/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh +++ b/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh @@ -18,7 +18,7 @@ # session. Catches tasks that slipped past the prompt gate. # # PostToolUse (matcher Bash) -# SHOW - a `printf` verdict line ran: echo it as a systemMessage so the user sees it +# SHOW - a `printf` verdict (or stage 2) line ran: echo it as a systemMessage so the user sees it # even when the Bash call is collapsed. # # This NEVER does LinearB work itself and NEVER blocks. It only injects an @@ -80,6 +80,40 @@ verdict_state() { # $1 = the verdict regex required (default: a grade or a skip) then "ok" else "pending" end' "$transcript" 2>/dev/null } +# One pass over the assistant's lines (text, and `printf` Bash calls) for the latest verdict naming this repo +# ($1 = its names, one per line; none = any repo). Prints pending|skipped|graded, or stage2 when that verdict +# is medium+ with no target file, followed by the "LinearB phase 2 —" lines printed after it. Nothing on parse failure. +repo_state() { + jq -rs --arg any "$VERDICT_RE" --arg re "$GRADED_RE" --arg names "${1:-}" ' + [.[] | select(.message.role? == "assistant") | .message.content? // [] | .[]? + | if .type? == "text" then (.text? // "") | split("\n")[] + elif (.type? == "tool_use" and .name? == "Bash") then (.input.command? // "") | split("\n")[] + | select(test("(^|;|&&)\\s*printf\\s")) else empty end] as $t + | [$names | split("\n")[] | select(length > 0)] as $ns + | [$t | to_entries[] | select((.value | test($any)) and (.value as $l | ($ns | length) == 0 or any($ns[]; + . as $n | $l | contains("LinearB: " + $n + " ") or (test("LinearB: [^ ]+/") and contains("/" + $n + " ")))))] as $v + | if ($v | length) == 0 then "pending" + else $v[-1] as $l + | if ($l.value | test($re) | not) then "skipped" + elif ($l.value | test("target ")) or ($l.value | test("(→|->) low[;)]|Kept effort \\(low[;)]")) then "graded" + else "stage2", ($t[($l.key + 1):][] | select(test("LinearB phase 2 — "))) + end + end' "$transcript" 2>/dev/null +} + +# Does a phase-2 line ($1) name the edited file, this repo, or a file in it? Uses $file_path, $names and $root. +p2_here() { + local toks t n + t="${1#*LinearB phase 2 — }" + read -ra toks <<<"$(printf '%s' "${t%%: *}" | tr ',+&`' ' ')" + for t in ${toks[@]+"${toks[@]}"}; do + [ "$t" = "$(basename "$file_path")" ] || [ -e "$root/$t" ] && return 0 + while IFS= read -r n; do [ -n "$n" ] && case "$t" in "$n"|"$n"/*) return 0 ;; esac; done <<<"$names" + [ -n "$(git -C "$root" ls-files -- ":(glob)**/$t" 2>/dev/null | head -n 1)" ] && return 0 + done + return 1 +} + note() { # $1 = context for the model; the tool call still runs (no red error in the UI) printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","additionalContext":%s}}\n' "$(printf '%s' "$1" | jq -Rs .)" exit 0 @@ -139,8 +173,10 @@ fi case "$event" in PostToolUse) cmd="$(jq -r '.tool_input.command // empty' <<<"$input" 2>/dev/null || true)" - case "$cmd" in *printf*"LinearB: "*) ;; *) exit 0 ;; esac - line="$(printf '%s\n' "$cmd" | grep -oE "> ${GRADED_RE}[^']*" | head -n 1)" + case "$cmd" in *printf*"LinearB"*) ;; *) exit 0 ;; esac + # Read the printed line, not the shell source, so quoting (e.g. an apostrophe) can't cut it short. + out="$(jq -r '.tool_response.stdout // empty' <<<"$input" 2>/dev/null || true)" + line="$(printf '%s\n' "$out" | grep -m 1 -E "^> (${GRADED_RE}|LinearB phase 2 — .+)" || true)" [ -n "$line" ] || exit 0 jq -cn --arg m "$line" '{systemMessage: $m}' dbg "PostToolUse: showed the verdict line to the user" @@ -220,16 +256,32 @@ case "$event" in # setup never walls. Scoped to a git repo; fails OPEN on any uncertainty. git -C "$dir" rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0 - # Skill already graded this session -> allow the edit. A "skipped (not a code task)" line alone doesn't count: editing now is code work. - # If a verdict names this repo, mark it handled so later prompts here aren't nudged. + # Graded only by a verdict or "skipped (not a code task)" line naming THIS repo (the remote's or the folder's + # name: clones and worktrees can differ), so one repo's run can't hold or release another. # "pending" only when parsed and no grade yet; a parse failure prints nothing and fails open. - if skill_ran && [ "$(verdict_state "$GRADED_RE")" != "pending" ]; then + root="$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null)" + url="$(git -C "$dir" config --get remote.origin.url 2>/dev/null)"; url="${url%.git}" + names="$(printf '%s\n' "${url##*[/:]}" "${root:+$(basename "$root")}" | grep -v '^$')" + gate="pending" + if skill_ran; then + rs="$(repo_state "$names")"; gate="${rs%%$'\n'*}" + # No name to match: the latest skill run must also be graded, as before. + [ -n "$names" ] || [ "$(verdict_state "$GRADED_RE")" != "pending" ] || gate="pending" + fi + if [ "$gate" != "pending" ]; then set_marker "$dir" - # The verdict uses the repo's name: check the remote's name and the folder's (clones and worktrees can differ). - url="$(git -C "$dir" config --get remote.origin.url 2>/dev/null)"; url="${url%.git}" - for name in "${url##*[/:]}" "$(basename "$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null)")"; do - [ -n "$name" ] && grep -qF "LinearB: $name " "$transcript" 2>/dev/null && { : > "$marker" 2>/dev/null || true; break; } - done + # A grade naming this repo marks it handled so later prompts here aren't nudged. + case "$gate" in graded|stage2) [ -n "$names" ] && { : > "$marker" 2>/dev/null || true; } ;; esac + # Stage 2 counts only a phase-2 line printed after this repo's verdict that names its file(s). + if [ "$gate" = "stage2" ] && [ "$what" = "this edit" ]; then + while IFS= read -r p2; do [ -n "$p2" ] && p2_here "$p2" && { gate="graded"; break; }; done <<<"$(printf '%s\n' "$rs" | tail -n +2)" + fi + # Stage 2: on a medium+ grade, edits wait for the file-level line, held at most twice per repo/session (never a wall). + s2="${marker%.done}.s2"; k="$(cat "$s2" 2>/dev/null)"; case "$k" in ''|*[!0-9]*) k=0 ;; esac + if [ "$what" = "this edit" ] && [ "$k" -lt 2 ] && [ "$gate" = "stage2" ] && printf '%s' "$((k + 1))" > "$s2" 2>/dev/null; then + dbg "PreToolUse: held $tool for the stage 2 (change-area) line" + deny "Stage 2 first: before editing $(basename "$file_path"), run the change-area probe on the file(s) you'll change and print the result with one Bash call: printf '%s\\n' '> LinearB phase 2 — : ~N lines rewritten/90d over M commits — '. If it changes the effort, print the full verdict line again instead. Then make the same call again." + fi exit 0 fi # Can't read the transcript -> can't confirm skill status -> fail open (allow). diff --git a/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md b/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md index b7820fe..f3cca03 100644 --- a/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md +++ b/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md @@ -53,7 +53,7 @@ set -o pipefail # with curl -f, a failed request fails the whole pipeline inst Two checks that prevent wasted work — do them before any LinearB call: -- **Not a code task → no grade at all.** If the task will not modify any file (reading or explaining code, answering a question, reviewing a doc, ticket or screenshot, drafting a reply or message in chat), don't grade it: print `> LinearB: — skipped (not a code task)`, call nothing, and work with your normal care. A grade measures the risk of a code change; a LOW here would wrongly tell you to go light on accuracy. If the task later turns into a file edit, run the sweep then, before the first edit (a hook holds that edit until a real grade exists). +- **Not a code task → no grade at all.** If the task will not modify any file (reading or explaining code, answering a question, reviewing a doc, ticket or screenshot, drafting a reply or message in chat), don't grade it: print `> LinearB: — skipped (not a code task)`, call nothing, and work with your normal care. A grade measures the risk of a code change; a LOW here would wrongly tell you to go light on accuracy. If the task later turns into a file edit, run the sweep then, before the first edit. - **Trivial code edit → LOW, skip everything.** If the task edits any file but changes **no code behavior** (code comments, doc files, formatting/whitespace, a pure rename), skip signal gathering entirely: effort is **LOW**, add no defensive guards (there's no logic to protect), and don't call LinearB at all — this holds **even on a fragile repo**. Anything that changes behavior is **not** trivial and warrants the sweep below — note that a **constant-value change counts as behavioral** (e.g. a timeout, retry count, or feature-flag default), as does any conditional or logic edit. - **Already graded recently → reuse (persistent 24h cache).** The repo-level verdict is daily-stable, so compute it at most **once per repo per ~24h**, not once per task/session — this is the main token/time saver. Cache file, keyed by repo root: @@ -63,13 +63,16 @@ Two checks that prevent wasted work — do them before any LinearB call: root="$(git -C "" rev-parse --show-toplevel)"; api="${LINEARB_API_URL:-https://public-api.linearb.io}"; api="${api%/}" key="$root"; [ "$api" = "https://public-api.linearb.io" ] || key="$root|$api" h="$(printf '%s' "$key" | { if command -v shasum >/dev/null 2>&1; then shasum; else sha1sum; fi; } | cut -c1-12)" - f="${XDG_CACHE_HOME:-$HOME/.cache}/agentic-advisor/verdicts/$h.json" # private per-user dir, not shared /tmp + d="${XDG_CACHE_HOME:-$HOME/.cache}/agentic-advisor/verdicts" # private per-user dir + # Claude Code's sandbox blocks writes to ~/.cache ("operation not permitted"); its $TMPDIR is per user and writable + (umask 077; mkdir -p "$d") 2>/dev/null && ( : > "$d/.w.$$" ) 2>/dev/null && rm -f "$d/.w.$$" || d="${TMPDIR:-/tmp}/agentic-advisor/verdicts" + f="$d/$h.json" cat "$f" 2>/dev/null # read BEFORE any LinearB call ``` - **On a hit** (file exists, **is owned by you** — check `[ -O "$f" ]` and ignore it otherwise, since a cache file someone else wrote could fake a LOW grade — AND `computed_at` is within the last 24h — or the file's mtime is <24h old): **skip the repo-health LinearB calls** (repo list, measurements, incidents/search) and reuse `repo_effort`/`repo_evidence` **as the repo-health axis only**. You MUST still grade **task-complexity and change-area (files) fresh for the current task**, then combine — the cache replaces the phase-1 *data*, NOT the final verdict. Note it — **keep the standard verdict shape** so the reporter still captures the decision: `LinearB: — effort (; repo-health cached h ago; task + files fresh)`. (The ticket lookup under Task Complexity is per task, not repo health, so it still runs on a hit.) - **On a miss/stale:** compute the repo-health verdict fresh, then **write the cache atomically** (a per-process temp file then `mv` — atomic on the same filesystem, and the `$$` suffix means two concurrent sessions never share a temp file): ```text - (umask 077; mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache}/agentic-advisor/verdicts") + (umask 077; mkdir -p "$d") printf '%s' '{"repo":"","repo_id":,"repo_effort":"","repo_evidence":"","computed_at":""}' > "$f.$$.tmp" && mv -f "$f.$$.tmp" "$f" ``` - **Recompute (ignore cache) if:** it's older than 24h, you just merged/deployed to this repo, or a serious incident may have landed. @@ -268,16 +271,16 @@ Phase 2 was only ever deferred because it needs the target file paths. So: ``` This is the reliable path: it's part of the same up-front measurement, not a "remember later" step. - **Only if the file cannot be known without exploring** (vague prompt) — emit the repo verdict first, explore to find the files, then before your first `Edit`/`Write` report phase 2. **Which line you print depends on whether phase 2 changes the effort** (recall `effort = max(repo-health, change-area, task)`, so a fix/revert hotspot can raise it above the repo level): - - **If phase 2 CHANGES the combined effort** — re-emit the **full verdict line** with the new level, so it stands as the corrected decision (this is the line that gets recorded; without it, telemetry keeps the stale phase-1 level): - ```text - > LinearB: — effort (; repo rework X% — but target : ~N lines rewritten/90d) — . + - **If phase 2 CHANGES the combined effort** — re-emit the **full verdict line** with the new level, with the same one `printf` Bash call, so it stands as the corrected decision (this is the line that gets recorded; without it, telemetry keeps the stale phase-1 level): + ```bash + printf '%s\n' '> LinearB: — effort (; repo rework X% — but target : ~N lines rewritten/90d) — .' ``` - - **If phase 2 does NOT change the level** — it's pure targeting context; print the short follow-up: - ```text - > LinearB phase 2 — : ~N lines rewritten/90d — . + - **If phase 2 does NOT change the level** — it's pure targeting context; print the short follow-up with one `printf` Bash call (a hook shows it to the user): + ```bash + printf '%s\n' '> LinearB phase 2 — : ~N lines rewritten/90d over M commits — .' ``` -**HARD RULE either way:** on a phase-2-gated task, the file-level finding must be printed **before the first `Edit`/`Write` to a target file** — combined into the phase-1 verdict when the file is known, or as the follow-up line when it isn't. Measure and report first, edit second. Skip only for trivial edits. +**HARD RULE either way:** on a phase-2-gated task, the file-level finding must be printed **before the first `Edit`/`Write` to a target file** — combined into the phase-1 verdict when the file is known, or as the follow-up line when it isn't. Measure and report first, edit second. Skip only for trivial edits. When the grade is medium or above and no file was named, a hook holds your first edit once until the phase-2 line is printed. ## Coding Posture diff --git a/tests/agentic-advisor/hooks_test.sh b/tests/agentic-advisor/hooks_test.sh index 5750a54..16483f1 100755 --- a/tests/agentic-advisor/hooks_test.sh +++ b/tests/agentic-advisor/hooks_test.sh @@ -76,6 +76,7 @@ raised_decision() { old_shape_decision() { [ "$rc" -eq 0 ] && [ "$(ncalls)" = 1 ] && call_json | jq -e '.tags.effort_level == "LOW" and (.tags | has("effort_change") | not)' >/dev/null } +add_stage2() { jq -cn --arg c "printf '%s\\n' '> LinearB phase 2 — app.js: ~40 lines rewritten/90d over 3 commits — clean, standard care.'" '{message:{role:"assistant",content:[{type:"tool_use",name:"Bash",input:{command:$c}}]}}' >> "$TRANSCRIPT"; } add_line() { printf '{"message":{"role":"assistant","content":[{"type":"text","text":"%s"}]}}\n' "$1" >> "$TRANSCRIPT"; } # --- trigger: UserPromptSubmit --- @@ -158,6 +159,7 @@ teardown setup add_skill_read; add_skill_body; add_verdict +add_stage2 run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "trigger: grading via the SKILL.md fallback (no Skill call) releases the edit" silent teardown @@ -184,6 +186,7 @@ teardown setup add_skill; add_verdict; add_skill_read +add_stage2 run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "trigger: re-reading SKILL.md after the verdict keeps the repo graded" silent teardown @@ -238,14 +241,12 @@ teardown setup add_skill; add_skill_body; add_skipped run "$TRIGGER" PreToolUse "$(tool Edit app.js)" -check "trigger: an edit after a skipped (not a code task) run is held until a real grade" denied -add_verdict -run "$TRIGGER" PreToolUse "$(tool Edit app.js)" -check "trigger: ...and goes through once the grade is printed" silent +check "trigger: a skipped (not a code task) line for this repo releases its edits (no stage 2)" silent teardown setup add_skill; add_verdict +add_stage2 run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "trigger: verdict printed -> edit allowed" silent teardown @@ -445,11 +446,13 @@ check "matrix: printf-form Raised line is reported" matrix_ok raised high 'rewor teardown setup add_skill; add_line "> LinearB: widgets — Raised effort (medium → high; x) — y." +add_stage2 run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "matrix: trigger releases hold on Raised" silent teardown setup add_skill; add_line "> LinearB: widgets — Kept effort (max; x) — y." +add_stage2 run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "matrix: trigger releases hold on Kept (max)" silent teardown @@ -474,7 +477,7 @@ for bad in "> LinearB: widgets — Raised effort (medium" "> LinearB: widgets done # PostToolUse: a printf verdict line is shown to the user as a systemMessage. -bash_cmd() { jq -cn --arg c "$1" '{tool_name:"Bash",tool_input:{command:$c}}' | sed 's/^{/,/; s/}$//'; } +bash_cmd() { jq -cn --arg c "$1" --arg o "${2:-}" '{tool_name:"Bash",tool_input:{command:$c},tool_response:{stdout:$o}}' | sed 's/^{/,/; s/}$//'; } shown() { [ "$rc" -eq 0 ] && [ "$(jq -r '.systemMessage // empty' <<<"$out")" = "$1" ]; } asks_printf() { jq -e '.hookSpecificOutput.permissionDecisionReason | contains("printf")' <<<"$out" >/dev/null; } raised_to_high() { call_json | jq -se 'any(.[]; .tags.effort_change == "raised" and .tags.target_effort == "high")' >/dev/null; } @@ -484,19 +487,25 @@ V2='> LinearB: widgets — Kept effort (high; rework 2.3%) — focused fix.' V3='> LinearB: widgets — Reduced effort (max -> medium; rework 2.3%) — focused fix.' for v in "$V1" "$V2" "$V3"; do setup - run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$v'")" + run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$v'" "$v")" check "show: printf verdict is shown: ${v:12:40}" shown "$v" teardown done setup -run "$TRIGGER" PostToolUse "$(bash_cmd "ls -la && git status")" +run "$TRIGGER" PostToolUse "$(bash_cmd "ls -la && git status" "total 0")" check "show: an unrelated Bash call shows nothing" silent -run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '> LinearB: — effort ( → ; ) — '")" +TPL='> LinearB: — effort ( → ; ) — ' +run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$TPL'" "$TPL")" check "show: the template line itself shows nothing" silent -run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '> LinearB: widgets — Raised effort (medium'")" +run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '> LinearB: widgets — Raised effort (medium'" "> LinearB: widgets — Raised effort (medium")" check "show: an incomplete verdict shows nothing" silent -LINEARB_AGENTIC_ADVISOR_ENABLED=off run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$V1'")" +LINEARB_AGENTIC_ADVISOR_ENABLED=off run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$V1'" "$V1")" check "show: off switch shows nothing" silent +V4="> LinearB: widgets — Raised effort (low → high; rework 11.0%) — preserve the user's behavior, tests per field." +run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '${V4//\'/\'\\\'\'}'" "$V4")" +check "show: an apostrophe in the plan is shown whole" shown "$V4" +run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$V1'" "")" +check "show: no printed output shows nothing" silent teardown # Hold asks for the printf call; a printf verdict then unblocks the edit and is reported once. @@ -505,6 +514,7 @@ add_skill run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "hold: reason asks for the printf call" asks_printf add_printf "$V1" +add_stage2 run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "hold: a printf verdict unblocks the edit" silent run "$REPORT" Stop; await_calls 1 @@ -512,6 +522,93 @@ check "report: the printf verdict is reported once" one_call check "report: it is a raised decision to high" raised_to_high teardown +# Stage 2: on a medium+ grade with no target file named, the first edit waits once for the file-level line. +stage2_held() { denied && jq -e '.hookSpecificOutput.permissionDecisionReason | (contains("Stage 2 first") and contains("app.js"))' <<<"$out" >/dev/null; } +setup +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6.5%) — focused diff." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: medium grade, no file named -> first edit held for stage 2" stage2_held +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: a retried edit is held again" stage2_held +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: after two holds the edit goes through (never a wall)" silent +teardown +setup +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6.5%) — focused diff."; add_stage2 +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: a printed stage 2 line releases the edit" silent +teardown +setup +add_skill; add_line "> LinearB: widgets — Raised effort (low → high; rework 11.0%; target app.js: ~201 lines rewritten/90d) — y." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: a verdict that already names the target file is not held" silent +teardown +setup +add_skill; add_line "> LinearB: widgets — Reduced effort (max → low; rework 0.4%) — lean." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: a low grade is not held for stage 2" silent +teardown +setup +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6.5%) — focused diff." +run "$TRIGGER" PreToolUse "$(tool Edit README.md)" +check "stage2: a docs edit is not held for stage 2" silent +teardown +setup +P2L='> LinearB phase 2 — app.js: ~201 lines rewritten/90d over 30 commits — concentrate guards + tests here.' +run "$TRIGGER" PostToolUse "$(bash_cmd "printf '%s\\n' '$P2L'" "$P2L")" +check "stage2: a printed stage 2 line is shown" shown "$P2L" +teardown + +# --- multi-repo: a verdict, skip or phase-2 line counts only for the repo it names --- +setup_b() { # a second repo, gadgets, in the same session + REPO_B="$T/gadgets"; git init -q "$REPO_B"; git -C "$REPO_B" remote add origin git@github.com:acme/gadgets.git +} +edit_b() { printf ',"tool_name":"Edit","tool_input":{"file_path":"%s"}' "$REPO_B/$1"; } +stage2_any() { denied && jq -e '.hookSpecificOutput.permissionDecisionReason | contains("Stage 2 first")' <<<"$out" >/dev/null; } +setup; setup_b +add_skill; add_line "> LinearB: widgets — Kept effort (low; rework 1%) — go." +run "$TRIGGER" PreToolUse "$(edit_b b.js)" +check "multi-repo: a verdict for widgets doesn't release an edit in gadgets" denied +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "multi-repo: ...but releases widgets" silent +teardown +setup; setup_b +add_skill; add_line "> LinearB: acme/widgets — Kept effort (low; rework 1%) — go." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "multi-repo: an owner/name verdict releases its repo" silent +run "$TRIGGER" PreToolUse "$(edit_b b.js)" +check "multi-repo: ...but not another repo" denied +teardown +setup; setup_b +add_skill; add_line "> LinearB: gadgets — Kept effort (low; rework 1%) — go."; add_skill +run "$TRIGGER" PreToolUse "$(edit_b b.js)" +check "multi-repo: an unfinished re-run (for widgets) doesn't hold graded gadgets" silent +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "multi-repo: ...while ungraded widgets is still held" denied +teardown +setup; setup_b +mkdir -p "$REPO/src"; : > "$REPO/src/core.js"; git -C "$REPO" add src/core.js +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6%) — y."; add_line "> LinearB: gadgets — Kept effort (medium; rework 6%) — y." +add_printf "> LinearB phase 2 — core.js: ~40 lines rewritten/90d over 3 commits — clean, standard care." +run "$TRIGGER" PreToolUse "$(edit_b b.js)" +check "multi-repo: a phase-2 line naming a widgets file doesn't satisfy gadgets" stage2_any +run "$TRIGGER" PreToolUse "$(tool Edit src/other.js)" +check "multi-repo: ...but satisfies widgets (file found in the repo)" silent +teardown +setup +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6%) — y."; add_stage2 +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6%) — y." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: a phase-2 line before this repo's latest verdict doesn't count" stage2_held +teardown +setup; setup_b +add_skill; add_skipped +run "$TRIGGER" PreToolUse "$(edit_b b.json)" +check "multi-repo: a skipped line for widgets doesn't release gadgets" denied +run "$TRIGGER" PreToolUse "$(tool Edit config.json)" +check "multi-repo: ...but releases a non-doc edit in widgets" silent +teardown + printf '\n%d passed, %d failed\n' "$pass" "$fail" [ "$fail" -eq 0 ] From b24849cc85edb345f18c97ffc8d687d11a9ebef9 Mon Sep 17 00:00:00 2001 From: nivSwisa1 Date: Thu, 26 Nov 2026 10:22:21 +0200 Subject: [PATCH 2/3] fix: address gitstream review (round 1): document the two stage-2 holds; per-user /tmp cache fallback Co-Authored-By: Claude Opus 5.5 --- plugins/agentic-advisor/CHANGELOG.md | 4 ++-- plugins/agentic-advisor/skills/agentic-advisor/SKILL.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/agentic-advisor/CHANGELOG.md b/plugins/agentic-advisor/CHANGELOG.md index c17aadb..4d0e945 100644 --- a/plugins/agentic-advisor/CHANGELOG.md +++ b/plugins/agentic-advisor/CHANGELOG.md @@ -4,9 +4,9 @@ ### Fixed -- With Claude Code's sandbox on, the 24-hour repo-health cache couldn't be written to `~/.cache` ("operation not permitted"), so every task re-ran the LinearB calls. The skill now falls back to `$TMPDIR`, which the sandbox allows. +- With Claude Code's sandbox on, the 24-hour repo-health cache couldn't be written to `~/.cache` ("operation not permitted"), so every task re-ran the LinearB calls. The skill now falls back to `$TMPDIR`, which the sandbox allows (or a per-user folder under `/tmp` when `$TMPDIR` is unset). - A verdict now holds or releases edits only in the repo it names, so grading one repo no longer blocks or unblocks edits in another in the same session. A "skipped (not a code task)" line releases edits in its own repo only. -- On a medium or higher grade with no target file named, the first edit waits once for the file-level (phase 2) line. +- On a medium or higher grade with no target file named, edits wait for the file-level (phase 2) line, held at most twice per repo and session. - A verdict containing an apostrophe is shown in full: the hook reads the printed line, not the shell source. ## [1.5.2] - 2026-11-10 diff --git a/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md b/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md index f3cca03..f9e8529 100644 --- a/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md +++ b/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md @@ -65,7 +65,7 @@ Two checks that prevent wasted work — do them before any LinearB call: h="$(printf '%s' "$key" | { if command -v shasum >/dev/null 2>&1; then shasum; else sha1sum; fi; } | cut -c1-12)" d="${XDG_CACHE_HOME:-$HOME/.cache}/agentic-advisor/verdicts" # private per-user dir # Claude Code's sandbox blocks writes to ~/.cache ("operation not permitted"); its $TMPDIR is per user and writable - (umask 077; mkdir -p "$d") 2>/dev/null && ( : > "$d/.w.$$" ) 2>/dev/null && rm -f "$d/.w.$$" || d="${TMPDIR:-/tmp}/agentic-advisor/verdicts" + (umask 077; mkdir -p "$d") 2>/dev/null && ( : > "$d/.w.$$" ) 2>/dev/null && rm -f "$d/.w.$$" || d="${TMPDIR:-/tmp/agentic-advisor-$(id -u)}/agentic-advisor/verdicts" f="$d/$h.json" cat "$f" 2>/dev/null # read BEFORE any LinearB call ``` @@ -280,7 +280,7 @@ Phase 2 was only ever deferred because it needs the target file paths. So: printf '%s\n' '> LinearB phase 2 — : ~N lines rewritten/90d over M commits — .' ``` -**HARD RULE either way:** on a phase-2-gated task, the file-level finding must be printed **before the first `Edit`/`Write` to a target file** — combined into the phase-1 verdict when the file is known, or as the follow-up line when it isn't. Measure and report first, edit second. Skip only for trivial edits. When the grade is medium or above and no file was named, a hook holds your first edit once until the phase-2 line is printed. +**HARD RULE either way:** on a phase-2-gated task, the file-level finding must be printed **before the first `Edit`/`Write` to a target file** — combined into the phase-1 verdict when the file is known, or as the follow-up line when it isn't. Measure and report first, edit second. Skip only for trivial edits. When the grade is medium or above and no file was named, a hook holds your edit until the phase-2 line is printed (at most twice per repo and session). ## Coding Posture From 0c2012b3d630f4ed5c7d4b69502442c5e5a48834 Mon Sep 17 00:00:00 2001 From: nivSwisa1 Date: Thu, 26 Nov 2026 10:27:49 +0200 Subject: [PATCH 3/3] fix: address Copilot review (round 2): legacy LOW and bare 'target' don't skip stage 2; owner-checked cache fallback Co-Authored-By: Claude Opus 5.5 --- .../agentic-advisor/hooks/agentic-advisor-trigger.sh | 2 +- .../agentic-advisor/skills/agentic-advisor/SKILL.md | 8 ++++---- tests/agentic-advisor/hooks_test.sh | 10 ++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh b/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh index ec87e54..4b64b3f 100755 --- a/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh +++ b/plugins/agentic-advisor/hooks/agentic-advisor-trigger.sh @@ -95,7 +95,7 @@ repo_state() { | if ($v | length) == 0 then "pending" else $v[-1] as $l | if ($l.value | test($re) | not) then "skipped" - elif ($l.value | test("target ")) or ($l.value | test("(→|->) low[;)]|Kept effort \\(low[;)]")) then "graded" + elif ($l.value | test("target [^ ;)]+: ")) or ($l.value | test("(→|->) low[;)]|Kept effort \\(low[;)]| LOW effort")) then "graded" else "stage2", ($t[($l.key + 1):][] | select(test("LinearB phase 2 — "))) end end' "$transcript" 2>/dev/null diff --git a/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md b/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md index f9e8529..bc21cfa 100644 --- a/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md +++ b/plugins/agentic-advisor/skills/agentic-advisor/SKILL.md @@ -65,15 +65,15 @@ Two checks that prevent wasted work — do them before any LinearB call: h="$(printf '%s' "$key" | { if command -v shasum >/dev/null 2>&1; then shasum; else sha1sum; fi; } | cut -c1-12)" d="${XDG_CACHE_HOME:-$HOME/.cache}/agentic-advisor/verdicts" # private per-user dir # Claude Code's sandbox blocks writes to ~/.cache ("operation not permitted"); its $TMPDIR is per user and writable - (umask 077; mkdir -p "$d") 2>/dev/null && ( : > "$d/.w.$$" ) 2>/dev/null && rm -f "$d/.w.$$" || d="${TMPDIR:-/tmp/agentic-advisor-$(id -u)}/agentic-advisor/verdicts" - f="$d/$h.json" + (umask 077; mkdir -p "$d") 2>/dev/null && ( : > "$d/.w.$$" ) 2>/dev/null && rm -f "$d/.w.$$" || d="${TMPDIR:-/tmp}/agentic-advisor-verdicts-$(id -u)" + (umask 077; mkdir -p "$d") 2>/dev/null; [ -O "$d" ] || d="" # a folder someone else made is not safe to write in + f="${d:+$d/$h.json}" # empty: no safe cache folder, so skip the cache this time cat "$f" 2>/dev/null # read BEFORE any LinearB call ``` - **On a hit** (file exists, **is owned by you** — check `[ -O "$f" ]` and ignore it otherwise, since a cache file someone else wrote could fake a LOW grade — AND `computed_at` is within the last 24h — or the file's mtime is <24h old): **skip the repo-health LinearB calls** (repo list, measurements, incidents/search) and reuse `repo_effort`/`repo_evidence` **as the repo-health axis only**. You MUST still grade **task-complexity and change-area (files) fresh for the current task**, then combine — the cache replaces the phase-1 *data*, NOT the final verdict. Note it — **keep the standard verdict shape** so the reporter still captures the decision: `LinearB: — effort (; repo-health cached h ago; task + files fresh)`. (The ticket lookup under Task Complexity is per task, not repo health, so it still runs on a hit.) - **On a miss/stale:** compute the repo-health verdict fresh, then **write the cache atomically** (a per-process temp file then `mv` — atomic on the same filesystem, and the `$$` suffix means two concurrent sessions never share a temp file): ```text - (umask 077; mkdir -p "$d") - printf '%s' '{"repo":"","repo_id":,"repo_effort":"","repo_evidence":"","computed_at":""}' > "$f.$$.tmp" && mv -f "$f.$$.tmp" "$f" + [ -n "$f" ] && printf '%s' '{"repo":"","repo_id":,"repo_effort":"","repo_evidence":"","computed_at":""}' > "$f.$$.tmp" && mv -f "$f.$$.tmp" "$f" ``` - **Recompute (ignore cache) if:** it's older than 24h, you just merged/deployed to this repo, or a serious incident may have landed. - **⚠️ The cache stores ONLY the repo-health verdict (phase 1: rework / incidents / unreviewed merges) — daily-stable and task-independent. It does NOT store the final combined effort.** Both **change-area (file fix/revert + ownership)** and **task-complexity** are per-task and are **NEVER cached** — recompute them every task and combine `max(cached repo-health, fresh task-complexity, fresh change-area)`. So a **different task on the same repo within 24h** reuses only the cheap repo-health part and still re-grades the task-specific axes → correct effort for the new task. A cached-calm repo can still surface a HIGH task or file finding. diff --git a/tests/agentic-advisor/hooks_test.sh b/tests/agentic-advisor/hooks_test.sh index 16483f1..a259d53 100755 --- a/tests/agentic-advisor/hooks_test.sh +++ b/tests/agentic-advisor/hooks_test.sh @@ -549,6 +549,16 @@ run "$TRIGGER" PreToolUse "$(tool Edit app.js)" check "stage2: a low grade is not held for stage 2" silent teardown setup +add_skill; add_line "> LinearB: widgets — LOW effort (healthy)." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: the old LOW effort shape is not held for stage 2" silent +teardown +setup +add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6.5%) — inspect target files first." +run "$TRIGGER" PreToolUse "$(tool Edit app.js)" +check "stage2: the word target in the plan is not a file finding" stage2_held +teardown +setup add_skill; add_line "> LinearB: widgets — Kept effort (medium; rework 6.5%) — focused diff." run "$TRIGGER" PreToolUse "$(tool Edit README.md)" check "stage2: a docs edit is not held for stage 2" silent