diff --git a/.agents/skills/planning-with-files/scripts/attest-plan.ps1 b/.agents/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.agents/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.agents/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.agents/skills/planning-with-files/scripts/attest-plan.sh b/.agents/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.agents/skills/planning-with-files/scripts/attest-plan.sh +++ b/.agents/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.agents/skills/planning-with-files/scripts/check-complete.ps1 b/.agents/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.agents/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.agents/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.agents/skills/planning-with-files/scripts/check-complete.sh b/.agents/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100644 --- a/.agents/skills/planning-with-files/scripts/check-complete.sh +++ b/.agents/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.agents/skills/planning-with-files/scripts/inject-plan.py b/.agents/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.agents/skills/planning-with-files/scripts/inject-plan.py +++ b/.agents/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.agents/skills/planning-with-files/scripts/inject-plan.sh b/.agents/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.agents/skills/planning-with-files/scripts/inject-plan.sh +++ b/.agents/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.agents/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.agents/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.agents/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.agents/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.agents/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.agents/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.agents/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.agents/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.codebuddy/skills/planning-with-files/scripts/attest-plan.ps1 b/.codebuddy/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.codebuddy/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.codebuddy/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.codebuddy/skills/planning-with-files/scripts/attest-plan.sh b/.codebuddy/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.codebuddy/skills/planning-with-files/scripts/attest-plan.sh +++ b/.codebuddy/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.codebuddy/skills/planning-with-files/scripts/check-complete.ps1 b/.codebuddy/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.codebuddy/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.codebuddy/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.codebuddy/skills/planning-with-files/scripts/check-complete.sh b/.codebuddy/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/.codebuddy/skills/planning-with-files/scripts/check-complete.sh +++ b/.codebuddy/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.codebuddy/skills/planning-with-files/scripts/inject-plan.py b/.codebuddy/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.codebuddy/skills/planning-with-files/scripts/inject-plan.py +++ b/.codebuddy/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.codebuddy/skills/planning-with-files/scripts/inject-plan.sh b/.codebuddy/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.codebuddy/skills/planning-with-files/scripts/inject-plan.sh +++ b/.codebuddy/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.codebuddy/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.codex/hooks/codex_hook_adapter.py b/.codex/hooks/codex_hook_adapter.py index ca7b1b9..a876032 100644 --- a/.codex/hooks/codex_hook_adapter.py +++ b/.codex/hooks/codex_hook_adapter.py @@ -17,8 +17,8 @@ HOOK_DIR = Path(__file__).resolve().parent _SAFE_LEGACY_SESSION_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,127}\Z") _PLAN_SLUG = re.compile(r"[A-Za-z0-9_][A-Za-z0-9._-]*\Z") SESSION_PLAN_BINDING_NOTICE = ( - "[planning-with-files] Multiple plans are available while session isolation " - "is armed. Set PLAN_ID= for this session; nothing injected." + "[planning-with-files] Multiple plans are available. " + "Set PLAN_ID= for this session; nothing injected." ) _DARWIN_SYSTEM_ALIASES = { Path("/var"): Path("/private/var"), @@ -223,23 +223,21 @@ def session_plan_requires_binding(root: Path) -> bool: """Require PLAN_ID when an attached session can resolve multiple plans. Session sentinels admit a session to planning context. They do not select a - plan. Once isolation is armed, an unbound session may use legacy resolution - only when the effective root has at most one live plan candidate. + plan. Multiple named plans always require a pin. With isolation armed, + a legacy root plan also counts as a competing candidate. """ if os.environ.get("PLAN_ID", ""): return False sessions_dir = root / ".planning" / "sessions" - try: - sessions_info = sessions_dir.lstat() - if _is_reparse_or_link(sessions_dir) or not stat.S_ISDIR(sessions_info.st_mode): - return False - except (FileNotFoundError, OSError, RuntimeError): + armed = sessions_dir.exists() or sessions_dir.is_symlink() + planning_dir = root / ".planning" + if not planning_dir.exists(): return False candidates = 0 try: - if (root / "task_plan.md").is_file(): + if armed and (root / "task_plan.md").is_file(): candidates += 1 planning_dir = root / ".planning" for child in planning_dir.iterdir(): diff --git a/.codex/hooks/post-tool-use.sh b/.codex/hooks/post-tool-use.sh index 96f6043..9792222 100644 --- a/.codex/hooks/post-tool-use.sh +++ b/.codex/hooks/post-tool-use.sh @@ -6,6 +6,9 @@ HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)" +if [ -z "$PLAN_DIR" ] && [ "$(sh "${HOOK_DIR}/resolve-plan-dir.sh" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 +fi # An explicit PLAN_ID is a binding, not a hint (issue #237). When the shared # resolver rejected one it emits nothing, and the legacy-root fallback below # would answer for a plan the operator never named. Stay silent instead; the diff --git a/.codex/hooks/pre-compact.sh b/.codex/hooks/pre-compact.sh index 9d4001c..853636a 100644 --- a/.codex/hooks/pre-compact.sh +++ b/.codex/hooks/pre-compact.sh @@ -7,6 +7,9 @@ HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)" +if [ -z "$PLAN_DIR" ] && [ "$(sh "${HOOK_DIR}/resolve-plan-dir.sh" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 +fi # An explicit PLAN_ID is a binding, not a hint (issue #237). When the shared # resolver rejected one it emits nothing, and the legacy-root fallback below # would carry the wrong plan through compaction, which is the one moment the diff --git a/.codex/hooks/pre-tool-use.sh b/.codex/hooks/pre-tool-use.sh index 348eac8..ce8d772 100644 --- a/.codex/hooks/pre-tool-use.sh +++ b/.codex/hooks/pre-tool-use.sh @@ -10,6 +10,9 @@ fi HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)" +if [ -z "$PLAN_DIR" ] && [ "$(sh "${HOOK_DIR}/resolve-plan-dir.sh" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 +fi # An explicit PLAN_ID is a binding, not a hint (issue #237). When the shared # resolver rejected one it emits nothing, and the legacy-root fallback below # would recite a plan the operator never named on every tool call. Stay silent diff --git a/.codex/hooks/stop.sh b/.codex/hooks/stop.sh index 22373b1..bcf41ca 100644 --- a/.codex/hooks/stop.sh +++ b/.codex/hooks/stop.sh @@ -6,6 +6,9 @@ HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)" +if [ -z "$PLAN_DIR" ] && [ "$(sh "${HOOK_DIR}/resolve-plan-dir.sh" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 +fi # An explicit PLAN_ID is a binding, not a hint (issue #237). When the shared # resolver rejected one it emits nothing, and the legacy-root fallback below # would decide whether this run may stop from a plan the operator never named. diff --git a/.codex/hooks/user-prompt-submit.sh b/.codex/hooks/user-prompt-submit.sh index 40a505f..2bad255 100644 --- a/.codex/hooks/user-prompt-submit.sh +++ b/.codex/hooks/user-prompt-submit.sh @@ -76,6 +76,10 @@ fi # Resolve and confirm a contained plan before probing or executing any Python # candidate. No-plan hook fires remain shell-only and silent. PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)" +PLAN_AMBIGUOUS=0 +if [ -z "$PLAN_DIR" ] && [ "$(sh "${HOOK_DIR}/resolve-plan-dir.sh" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + PLAN_AMBIGUOUS=1 +fi if [ -n "$PLAN_DIR" ]; then PLAN_FILE="${PLAN_DIR}/task_plan.md" PROGRESS_FILE="${PLAN_DIR}/progress.md" @@ -92,7 +96,7 @@ else PLAN_FILE="${PLAN_PREFIX}task_plan.md" PROGRESS_FILE="${PLAN_PREFIX}progress.md" fi -[ -f "$PLAN_FILE" ] || exit 0 +[ -f "$PLAN_FILE" ] || [ "$PLAN_AMBIGUOUS" = "1" ] || exit 0 # --- Re-arm the once-per-turn PostToolUse nudge (issue #239). --- # One user message is one turn, and this hook fires once per turn, so clearing @@ -113,7 +117,7 @@ elif [ -n "${HOME:-}" ]; then else TURN_ROOT="${TMPDIR:-/tmp}/pwf-turn" fi -if [ -d "$TURN_ROOT" ]; then +if [ "$PLAN_AMBIGUOUS" = "0" ] && [ -d "$TURN_ROOT" ]; then case "$TURN_PLAN_FILE" in /*|[A-Za-z]:*|\\\\*) TURN_KEY_SRC="$TURN_PLAN_FILE" ;; *) TURN_KEY_SRC="${PWD}/${TURN_PLAN_FILE}" ;; @@ -160,6 +164,11 @@ case "$PWF_SESSION_ADMISSION" in ;; esac +if [ "$PLAN_AMBIGUOUS" = "1" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + exit 0 +fi + # Plan-id safe-identifier check. Pure-sh case patterns; shared shape with # resolve-plan-dir.sh, needed below to decide whether PLAN_ID named the plan. slug_is_valid() { diff --git a/.codex/skills/planning-with-files/scripts/attest-plan.ps1 b/.codex/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.codex/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.codex/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.codex/skills/planning-with-files/scripts/attest-plan.sh b/.codex/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.codex/skills/planning-with-files/scripts/attest-plan.sh +++ b/.codex/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.codex/skills/planning-with-files/scripts/check-complete.ps1 b/.codex/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.codex/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.codex/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.codex/skills/planning-with-files/scripts/check-complete.sh b/.codex/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/.codex/skills/planning-with-files/scripts/check-complete.sh +++ b/.codex/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.codex/skills/planning-with-files/scripts/inject-plan.py b/.codex/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.codex/skills/planning-with-files/scripts/inject-plan.py +++ b/.codex/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.codex/skills/planning-with-files/scripts/inject-plan.sh b/.codex/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.codex/skills/planning-with-files/scripts/inject-plan.sh +++ b/.codex/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.codex/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.codex/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.codex/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.codex/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.codex/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.codex/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.codex/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.codex/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.continue/skills/planning-with-files/scripts/attest-plan.ps1 b/.continue/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.continue/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.continue/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.continue/skills/planning-with-files/scripts/attest-plan.sh b/.continue/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.continue/skills/planning-with-files/scripts/attest-plan.sh +++ b/.continue/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.continue/skills/planning-with-files/scripts/check-complete.ps1 b/.continue/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.continue/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.continue/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.continue/skills/planning-with-files/scripts/check-complete.sh b/.continue/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/.continue/skills/planning-with-files/scripts/check-complete.sh +++ b/.continue/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.continue/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.continue/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.continue/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.continue/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.continue/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.continue/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.continue/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.continue/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.cursor/skills/planning-with-files/scripts/attest-plan.ps1 b/.cursor/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.cursor/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.cursor/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.cursor/skills/planning-with-files/scripts/attest-plan.sh b/.cursor/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.cursor/skills/planning-with-files/scripts/attest-plan.sh +++ b/.cursor/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.cursor/skills/planning-with-files/scripts/check-complete.ps1 b/.cursor/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.cursor/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.cursor/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.cursor/skills/planning-with-files/scripts/check-complete.sh b/.cursor/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100644 --- a/.cursor/skills/planning-with-files/scripts/check-complete.sh +++ b/.cursor/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.cursor/skills/planning-with-files/scripts/inject-plan.py b/.cursor/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.cursor/skills/planning-with-files/scripts/inject-plan.py +++ b/.cursor/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.cursor/skills/planning-with-files/scripts/inject-plan.sh b/.cursor/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.cursor/skills/planning-with-files/scripts/inject-plan.sh +++ b/.cursor/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.cursor/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.factory/skills/planning-with-files/scripts/attest-plan.ps1 b/.factory/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.factory/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.factory/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.factory/skills/planning-with-files/scripts/attest-plan.sh b/.factory/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.factory/skills/planning-with-files/scripts/attest-plan.sh +++ b/.factory/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.factory/skills/planning-with-files/scripts/check-complete.ps1 b/.factory/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.factory/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.factory/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.factory/skills/planning-with-files/scripts/check-complete.sh b/.factory/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/.factory/skills/planning-with-files/scripts/check-complete.sh +++ b/.factory/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.factory/skills/planning-with-files/scripts/inject-plan.py b/.factory/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.factory/skills/planning-with-files/scripts/inject-plan.py +++ b/.factory/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.factory/skills/planning-with-files/scripts/inject-plan.sh b/.factory/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.factory/skills/planning-with-files/scripts/inject-plan.sh +++ b/.factory/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.factory/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.factory/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.factory/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.factory/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.factory/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.factory/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.factory/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.factory/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.gemini/skills/planning-with-files/scripts/attest-plan.ps1 b/.gemini/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.gemini/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.gemini/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.gemini/skills/planning-with-files/scripts/attest-plan.sh b/.gemini/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.gemini/skills/planning-with-files/scripts/attest-plan.sh +++ b/.gemini/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.gemini/skills/planning-with-files/scripts/check-complete.ps1 b/.gemini/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.gemini/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.gemini/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.gemini/skills/planning-with-files/scripts/check-complete.sh b/.gemini/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/.gemini/skills/planning-with-files/scripts/check-complete.sh +++ b/.gemini/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.gemini/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.mastracode/skills/planning-with-files/scripts/inject-plan.py b/.mastracode/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.mastracode/skills/planning-with-files/scripts/inject-plan.py +++ b/.mastracode/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.mastracode/skills/planning-with-files/scripts/inject-plan.sh b/.mastracode/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.mastracode/skills/planning-with-files/scripts/inject-plan.sh +++ b/.mastracode/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.mastracode/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.mastracode/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.mastracode/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.mastracode/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.opencode/skills/planning-with-files/scripts/inject-plan.py b/.opencode/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.opencode/skills/planning-with-files/scripts/inject-plan.py +++ b/.opencode/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.opencode/skills/planning-with-files/scripts/inject-plan.sh b/.opencode/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.opencode/skills/planning-with-files/scripts/inject-plan.sh +++ b/.opencode/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.opencode/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.opencode/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.opencode/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.opencode/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/.pi/skills/planning-with-files/scripts/attest-plan.ps1 b/.pi/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/.pi/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/.pi/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/.pi/skills/planning-with-files/scripts/attest-plan.sh b/.pi/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/.pi/skills/planning-with-files/scripts/attest-plan.sh +++ b/.pi/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/.pi/skills/planning-with-files/scripts/check-complete.ps1 b/.pi/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/.pi/skills/planning-with-files/scripts/check-complete.ps1 +++ b/.pi/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/.pi/skills/planning-with-files/scripts/check-complete.sh b/.pi/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/.pi/skills/planning-with-files/scripts/check-complete.sh +++ b/.pi/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/.pi/skills/planning-with-files/scripts/inject-plan.py b/.pi/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/.pi/skills/planning-with-files/scripts/inject-plan.py +++ b/.pi/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/.pi/skills/planning-with-files/scripts/inject-plan.sh b/.pi/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/.pi/skills/planning-with-files/scripts/inject-plan.sh +++ b/.pi/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/.pi/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/.pi/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/.pi/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/.pi/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/.pi/skills/planning-with-files/scripts/resolve-plan-dir.sh b/.pi/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/.pi/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/.pi/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/hooks/claude-hook.sh b/hooks/claude-hook.sh index a32fa6c..f3b0012 100644 --- a/hooks/claude-hook.sh +++ b/hooks/claude-hook.sh @@ -164,6 +164,9 @@ active_plan_dir() { _resolved="" if [ -f "$RESOLVE_PLAN_DIR" ]; then _resolved=$(sh "$RESOLVE_PLAN_DIR" 2>/dev/null) || _resolved="" + if [ -z "$_resolved" ] && [ "$(sh "$RESOLVE_PLAN_DIR" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + return 0 + fi fi if [ -n "$_resolved" ] && [ -f "${_resolved}/task_plan.md" ]; then printf '%s\n' "$_resolved" diff --git a/scripts/attest-plan.ps1 b/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/scripts/attest-plan.ps1 +++ b/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/scripts/attest-plan.sh b/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/scripts/attest-plan.sh +++ b/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/scripts/check-complete.ps1 b/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/scripts/check-complete.ps1 +++ b/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/scripts/check-complete.sh b/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/scripts/check-complete.sh +++ b/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/scripts/inject-plan.py b/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/scripts/inject-plan.py +++ b/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/scripts/inject-plan.sh b/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/scripts/inject-plan.sh +++ b/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/scripts/resolve-plan-dir.ps1 b/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/scripts/resolve-plan-dir.ps1 +++ b/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/scripts/resolve-plan-dir.sh b/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100755 --- a/scripts/resolve-plan-dir.sh +++ b/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/skills/i18n/planning-with-files-ar/scripts/attest-plan.ps1 b/skills/i18n/planning-with-files-ar/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/skills/i18n/planning-with-files-ar/scripts/attest-plan.ps1 +++ b/skills/i18n/planning-with-files-ar/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/skills/i18n/planning-with-files-ar/scripts/attest-plan.sh b/skills/i18n/planning-with-files-ar/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/skills/i18n/planning-with-files-ar/scripts/attest-plan.sh +++ b/skills/i18n/planning-with-files-ar/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/skills/i18n/planning-with-files-ar/scripts/inject-plan.py b/skills/i18n/planning-with-files-ar/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/skills/i18n/planning-with-files-ar/scripts/inject-plan.py +++ b/skills/i18n/planning-with-files-ar/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/skills/i18n/planning-with-files-ar/scripts/inject-plan.sh b/skills/i18n/planning-with-files-ar/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/skills/i18n/planning-with-files-ar/scripts/inject-plan.sh +++ b/skills/i18n/planning-with-files-ar/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.ps1 b/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.ps1 +++ b/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.sh b/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.sh +++ b/skills/i18n/planning-with-files-ar/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/skills/i18n/planning-with-files-de/scripts/attest-plan.ps1 b/skills/i18n/planning-with-files-de/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/skills/i18n/planning-with-files-de/scripts/attest-plan.ps1 +++ b/skills/i18n/planning-with-files-de/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/skills/i18n/planning-with-files-de/scripts/attest-plan.sh b/skills/i18n/planning-with-files-de/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/skills/i18n/planning-with-files-de/scripts/attest-plan.sh +++ b/skills/i18n/planning-with-files-de/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/skills/i18n/planning-with-files-de/scripts/inject-plan.py b/skills/i18n/planning-with-files-de/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/skills/i18n/planning-with-files-de/scripts/inject-plan.py +++ b/skills/i18n/planning-with-files-de/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/skills/i18n/planning-with-files-de/scripts/inject-plan.sh b/skills/i18n/planning-with-files-de/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/skills/i18n/planning-with-files-de/scripts/inject-plan.sh +++ b/skills/i18n/planning-with-files-de/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.ps1 b/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.ps1 +++ b/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.sh b/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.sh +++ b/skills/i18n/planning-with-files-de/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/skills/i18n/planning-with-files-es/scripts/attest-plan.ps1 b/skills/i18n/planning-with-files-es/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/skills/i18n/planning-with-files-es/scripts/attest-plan.ps1 +++ b/skills/i18n/planning-with-files-es/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/skills/i18n/planning-with-files-es/scripts/attest-plan.sh b/skills/i18n/planning-with-files-es/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/skills/i18n/planning-with-files-es/scripts/attest-plan.sh +++ b/skills/i18n/planning-with-files-es/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/skills/i18n/planning-with-files-es/scripts/inject-plan.py b/skills/i18n/planning-with-files-es/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/skills/i18n/planning-with-files-es/scripts/inject-plan.py +++ b/skills/i18n/planning-with-files-es/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/skills/i18n/planning-with-files-es/scripts/inject-plan.sh b/skills/i18n/planning-with-files-es/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/skills/i18n/planning-with-files-es/scripts/inject-plan.sh +++ b/skills/i18n/planning-with-files-es/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.ps1 b/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.ps1 +++ b/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.sh b/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.sh +++ b/skills/i18n/planning-with-files-es/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/skills/i18n/planning-with-files-zh/scripts/attest-plan.ps1 b/skills/i18n/planning-with-files-zh/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/skills/i18n/planning-with-files-zh/scripts/attest-plan.ps1 +++ b/skills/i18n/planning-with-files-zh/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/skills/i18n/planning-with-files-zh/scripts/attest-plan.sh b/skills/i18n/planning-with-files-zh/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/skills/i18n/planning-with-files-zh/scripts/attest-plan.sh +++ b/skills/i18n/planning-with-files-zh/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/skills/i18n/planning-with-files-zh/scripts/inject-plan.py b/skills/i18n/planning-with-files-zh/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/skills/i18n/planning-with-files-zh/scripts/inject-plan.py +++ b/skills/i18n/planning-with-files-zh/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/skills/i18n/planning-with-files-zh/scripts/inject-plan.sh b/skills/i18n/planning-with-files-zh/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/skills/i18n/planning-with-files-zh/scripts/inject-plan.sh +++ b/skills/i18n/planning-with-files-zh/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.ps1 b/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.ps1 +++ b/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.sh b/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.sh +++ b/skills/i18n/planning-with-files-zh/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/skills/i18n/planning-with-files-zht/scripts/attest-plan.ps1 b/skills/i18n/planning-with-files-zht/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/skills/i18n/planning-with-files-zht/scripts/attest-plan.ps1 +++ b/skills/i18n/planning-with-files-zht/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/skills/i18n/planning-with-files-zht/scripts/attest-plan.sh b/skills/i18n/planning-with-files-zht/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/skills/i18n/planning-with-files-zht/scripts/attest-plan.sh +++ b/skills/i18n/planning-with-files-zht/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/skills/i18n/planning-with-files-zht/scripts/inject-plan.py b/skills/i18n/planning-with-files-zht/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/skills/i18n/planning-with-files-zht/scripts/inject-plan.py +++ b/skills/i18n/planning-with-files-zht/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/skills/i18n/planning-with-files-zht/scripts/inject-plan.sh b/skills/i18n/planning-with-files-zht/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/skills/i18n/planning-with-files-zht/scripts/inject-plan.sh +++ b/skills/i18n/planning-with-files-zht/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.ps1 b/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100644 --- a/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.ps1 +++ b/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.sh b/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100644 --- a/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.sh +++ b/skills/i18n/planning-with-files-zht/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/skills/planning-with-files/scripts/attest-plan.ps1 b/skills/planning-with-files/scripts/attest-plan.ps1 index 9446b40..9a58ca8 100644 --- a/skills/planning-with-files/scripts/attest-plan.ps1 +++ b/skills/planning-with-files/scripts/attest-plan.ps1 @@ -404,6 +404,7 @@ function Resolve-PlanFile { $resolver = Join-Path $PSScriptRoot "resolve-plan-dir.ps1" if (-not (Test-Path -LiteralPath $resolver -PathType Leaf)) { return $null } $resolvedDir = @(& $resolver | Where-Object { $_ }) | Select-Object -First 1 + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { return $null } if ($resolvedDir) { $planFile = Join-Path $resolvedDir "task_plan.md" return (Resolve-ContainedPlanFile -Candidate $planFile -ExpectedDirectory $resolvedDir) diff --git a/skills/planning-with-files/scripts/attest-plan.sh b/skills/planning-with-files/scripts/attest-plan.sh index ee7c499..357769c 100644 --- a/skills/planning-with-files/scripts/attest-plan.sh +++ b/skills/planning-with-files/scripts/attest-plan.sh @@ -45,6 +45,10 @@ resolve_plan_file() { plan_dir="" if [ -f "${RESOLVER}" ]; then plan_dir="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$plan_dir" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + printf "[plan-attest] Multiple plans are available. Set PLAN_ID=; nothing was attested.\n" >&2 + return 1 + fi fi if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then printf "%s\n" "${plan_dir}/task_plan.md" diff --git a/skills/planning-with-files/scripts/check-complete.ps1 b/skills/planning-with-files/scripts/check-complete.ps1 index c0d078f..39cd043 100644 --- a/skills/planning-with-files/scripts/check-complete.ps1 +++ b/skills/planning-with-files/scripts/check-complete.ps1 @@ -35,6 +35,7 @@ if ($PlanFile -ne "") { try { $resolvedDir = (& $resolver 2>$null | Select-Object -First 1) if ($null -eq $resolvedDir) { $resolvedDir = "" } + if (-not $resolvedDir -and ((& $resolver -CheckAmbiguity) -eq "PWF_PLAN_AMBIGUOUS_V1")) { exit 0 } } catch { $resolvedDir = "" } diff --git a/skills/planning-with-files/scripts/check-complete.sh b/skills/planning-with-files/scripts/check-complete.sh index e6dee66..1f15d12 100755 --- a/skills/planning-with-files/scripts/check-complete.sh +++ b/skills/planning-with-files/scripts/check-complete.sh @@ -56,6 +56,9 @@ else RESOLVED_DIR="" if [ -f "${RESOLVER}" ]; then RESOLVED_DIR="$(sh "${RESOLVER}" 2>/dev/null)" + if [ -z "$RESOLVED_DIR" ] && [ "$(sh "${RESOLVER}" --check-ambiguity 2>/dev/null)" = "PWF_PLAN_AMBIGUOUS_V1" ]; then + exit 0 + fi fi if [ -n "${RESOLVED_DIR}" ] && [ -f "${RESOLVED_DIR}/task_plan.md" ]; then PLAN_FILE="${RESOLVED_DIR}/task_plan.md" diff --git a/skills/planning-with-files/scripts/inject-plan.py b/skills/planning-with-files/scripts/inject-plan.py index b4dacac..8c2ec1f 100644 --- a/skills/planning-with-files/scripts/inject-plan.py +++ b/skills/planning-with-files/scripts/inject-plan.py @@ -59,12 +59,9 @@ Windows runs the shell chain through Git Bash and nothing else: one turn-marker slot and one progress-guard slot per plan. Known, accepted differences from the shell reference: - * Two plan directories with the same whole-second mtime, no .active_plan - and no PLAN_ID: the reference picks the first in the shell's glob order - (locale collation), this twin the first in code-point order. The same - collation difference can change which three of four or more nested - projects the ambiguity notice names. A machine runs one implementation, - so neither choice flips within a session. + * Shell glob order follows locale collation; this twin uses code-point + order. This can change which three of four or more nested projects the + ambiguity notice names. * BSD sed (macOS) appends a newline to a progress.md whose last line has none; GNU sed and this twin do not. * With PWF_PLAN_ROOT set under Git Bash the shell sees the pin as typed @@ -837,6 +834,16 @@ class Injector(object): scope = "" explicit = bool(plan_prefix) plan_id = env.get("PLAN_ID", "") + ambiguous = plan_is_ambiguous( + plan_prefix + ".planning", plan_root_pin if plan_root_pin else ".", plan_id + ) + if ambiguous and (context == "preflight" or not is_dir(plan_prefix + ".planning/sessions")): + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available. Set PLAN_ID= " + "for this session; nothing injected." + ) + raise Bail() if plan_id: if slug_is_valid(plan_id) and is_dir(plan_prefix + ".planning/" + plan_id): resolved = plan_prefix + ".planning/" + plan_id @@ -923,30 +930,14 @@ class Injector(object): "single-session mode." ) raise Bail() - if not plan_id: - plan_n = 1 if is_file(plan_prefix + "task_plan.md") else 0 - try: - names = sorted(os.listdir(plan_prefix + ".planning")) - except OSError: - names = [] - for name in names: - if name.startswith("."): - continue - if not is_file(plan_prefix + ".planning/" + name + "/task_plan.md"): - continue - if not slug_is_valid(name): - continue - plan_n += 1 - if plan_n > 1: - break - if plan_n > 1: - if context == "userprompt": - self.echo( - "[planning-with-files] Multiple plans are available while session " - "isolation is armed. Set PLAN_ID= for this session; nothing " - "injected." - ) - raise Bail() + if ambiguous: + if context == "userprompt": + self.echo( + "[planning-with-files] Multiple plans are available while session " + "isolation is armed. Set PLAN_ID= for this session; nothing " + "injected." + ) + raise Bail() if not explicit: nested = [] @@ -1326,6 +1317,23 @@ def inject(context, env=None): # Twin of resolve-plan-dir.sh (the shared resolver the dispatcher uses). # -------------------------------------------------------------------------- +def plan_is_ambiguous(plan_root, project_root, plan_id=""): + """A project pointer or mtime cannot bind a session to multiple plans.""" + if plan_id: + return False + count = 1 if is_dir(plan_root + "/sessions") and is_file(project_root + "/task_plan.md") else 0 + try: + names = os.listdir(plan_root) + except OSError: + names = [] + for name in names: + if slug_is_valid(name) and is_file(plan_root + "/" + name + "/task_plan.md"): + count += 1 + if count > 1: + return True + return False + + def resolve_plan_dir(env=None): """Return (spelled, filesystem) paths of the plan directory, or ("", ""). @@ -1360,6 +1368,9 @@ def resolve_plan_dir(env=None): return found(plan_id) return ("", "") + if plan_is_ambiguous(fs_root, pin if pin else "."): + return ("", "") + active_file = fs_root + "/.active_plan" if is_file(active_file): try: @@ -1441,6 +1452,8 @@ class ClaudeDispatcher(object): return (spelled, fs) if self.env.get("PLAN_ID", "") or self.env.get("PWF_PLAN_ROOT", ""): return ("", "") + if plan_is_ambiguous(".planning", "."): + return ("", "") if is_file("task_plan.md"): return (".", ".") return ("", "") diff --git a/skills/planning-with-files/scripts/inject-plan.sh b/skills/planning-with-files/scripts/inject-plan.sh index cd2905d..8a3c9e3 100644 --- a/skills/planning-with-files/scripts/inject-plan.sh +++ b/skills/planning-with-files/scripts/inject-plan.sh @@ -254,6 +254,29 @@ is_within_root() { # The .active_plan pointer, the newest-by-mtime fallback, and the legacy root # task_plan.md are cwd GUESSES — only guesses are subject to the nested-root # conflict check below. +# Shared .active_plan and directory mtime cannot identify this session's plan. +# Check before selection or preflight, even when isolation was never armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_PREFIX}.planning/sessions" ] && [ -f "${PLAN_PREFIX}task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$PLAN_AMBIGUOUS" = "1" ] && { [ "$CONTEXT" = "preflight" ] || [ ! -d "${PLAN_PREFIX}.planning/sessions" ]; }; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available. Set PLAN_ID= for this session; nothing injected." + fi + exit 0 +fi + RESOLVED="" SCOPE="" EXPLICIT=0 @@ -442,27 +465,13 @@ PY exit 0 fi - # An attachment admits a session but does not select one of several plans. - # When isolation is armed, require PLAN_ID if more than one live same-root - # candidate exists. PWF_PLAN_ROOT selects the project root, not a plan - # within that root. - if [ -z "${PLAN_ID:-}" ]; then - SESSION_PLAN_N=0 - [ -f "${PLAN_PREFIX}task_plan.md" ] && SESSION_PLAN_N=1 - for candidate in "${PLAN_PREFIX}".planning/*/task_plan.md; do - [ -f "$candidate" ] || continue - candidate_dir="${candidate%/task_plan.md}" - candidate_slug="${candidate_dir##*/}" - slug_is_valid "$candidate_slug" || continue - SESSION_PLAN_N=$((SESSION_PLAN_N + 1)) - [ "$SESSION_PLAN_N" -gt 1 ] && break - done - if [ "$SESSION_PLAN_N" -gt 1 ]; then - if [ "$CONTEXT" = "userprompt" ]; then - echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." - fi - exit 0 + # Attachment admits a session but does not select its plan. Preserve the + # attachment-first notice above for sessions that never opted in. + if [ "$PLAN_AMBIGUOUS" = "1" ]; then + if [ "$CONTEXT" = "userprompt" ]; then + echo "[planning-with-files] Multiple plans are available while session isolation is armed. Set PLAN_ID= for this session; nothing injected." fi + exit 0 fi fi diff --git a/skills/planning-with-files/scripts/resolve-plan-dir.ps1 b/skills/planning-with-files/scripts/resolve-plan-dir.ps1 index b14a6f6..3d98e52 100755 --- a/skills/planning-with-files/scripts/resolve-plan-dir.ps1 +++ b/skills/planning-with-files/scripts/resolve-plan-dir.ps1 @@ -13,7 +13,8 @@ # junction/symlink escape; slug validation alone blocks textual traversal. param( - [string]$PlanRoot = (Join-Path (Get-Location) ".planning") + [string]$PlanRoot = (Join-Path (Get-Location) ".planning"), + [switch]$CheckAmbiguity ) $projectRoot = (Get-Location).Path @@ -137,6 +138,30 @@ $activeFile = Join-Path $PlanRoot ".active_plan" # at rc=0. Emptiness is the fail-closed signal on this channel, matching # resolve-plan-dir.sh and the PWF_PLAN_ROOT pin. An empty $env:PLAN_ID is # falsy here and still means "unset". +# The optional probe distinguishes ambiguity from a legacy-root fallback. +# Multiple named plans require PLAN_ID even without a sessions directory. +$planCount = 0 +if (-not $env:PLAN_ID) { + if ((Test-Path -LiteralPath (Join-Path $PlanRoot "sessions") -PathType Container) -and + (Test-Path -LiteralPath (Join-Path $projectRoot "task_plan.md") -PathType Leaf)) { + $planCount = 1 + } + if (Test-Path -LiteralPath $PlanRoot -PathType Container) { + foreach ($entry in (Get-ChildItem -LiteralPath $PlanRoot -Directory -ErrorAction SilentlyContinue)) { + if ((Test-ValidSlug $entry.Name) -and + (Test-Path -LiteralPath (Join-Path $entry.FullName "task_plan.md") -PathType Leaf)) { + $planCount++ + if ($planCount -gt 1) { break } + } + } + } +} +if ($CheckAmbiguity) { + if ($planCount -gt 1) { Write-Output "PWF_PLAN_AMBIGUOUS_V1" } + exit 0 +} +if ($planCount -gt 1) { exit 0 } + if ($env:PLAN_ID) { if (Test-ValidSlug $env:PLAN_ID) { $candidate = Join-Path $PlanRoot $env:PLAN_ID diff --git a/skills/planning-with-files/scripts/resolve-plan-dir.sh b/skills/planning-with-files/scripts/resolve-plan-dir.sh index b11d03e..5a788c9 100755 --- a/skills/planning-with-files/scripts/resolve-plan-dir.sh +++ b/skills/planning-with-files/scripts/resolve-plan-dir.sh @@ -15,6 +15,13 @@ set -u +# Optional probe distinguishes ambiguity from the empty legacy-root result. +# Both modes keep stdout data-only and always exit zero. +CHECK_AMBIGUITY=0 +if [ "${1:-}" = "--check-ambiguity" ]; then + CHECK_AMBIGUITY=1 + shift +fi PLAN_ROOT="${1:-${PWD}/.planning}" # --- PWF_PLAN_ROOT: absolute plan-root binding (issue #212). --- @@ -321,6 +328,29 @@ resolve_latest_dir() { # the fail-closed signal on this channel, exactly as the PWF_PLAN_ROOT guard # above already does it; a non-zero status would kill callers running under # set -e for a condition that is not an internal error. +# A shared pointer or mtime is not a per-session binding (issue #240). +# Count conservatively, just like injection: slug-valid live plan files. +# The legacy root joins the count only when session isolation is armed. +PLAN_AMBIGUOUS=0 +if [ -z "${PLAN_ID:-}" ]; then + PLAN_COUNT=0 + if [ -d "${PLAN_ROOT}/sessions" ] && [ -f "${PWF_ROOT_PIN:-.}/task_plan.md" ]; then + PLAN_COUNT=1 + fi + for plan_candidate in "${PLAN_ROOT}"/*/task_plan.md; do + [ -f "$plan_candidate" ] || continue + plan_candidate_dir="${plan_candidate%/task_plan.md}" + slug_is_valid "${plan_candidate_dir##*/}" || continue + PLAN_COUNT=$((PLAN_COUNT + 1)) + if [ "$PLAN_COUNT" -gt 1 ]; then PLAN_AMBIGUOUS=1; break; fi + done +fi +if [ "$CHECK_AMBIGUITY" = "1" ]; then + [ "$PLAN_AMBIGUOUS" = "1" ] && printf '%s\n' 'PWF_PLAN_AMBIGUOUS_V1' + exit 0 +fi +[ "$PLAN_AMBIGUOUS" = "1" ] && exit 0 + if [ -n "${PLAN_ID:-}" ]; then resolve_from_env && exit 0 exit 0 diff --git a/tests/test_hook_resolver_integration.py b/tests/test_hook_resolver_integration.py index 6b473b6..0ef3974 100644 --- a/tests/test_hook_resolver_integration.py +++ b/tests/test_hook_resolver_integration.py @@ -310,15 +310,15 @@ class HookResolverIntegrationTests(unittest.TestCase): self.assertIn("Task A goal", result.stdout) self.assertNotIn("Task B goal", result.stdout) - def test_user_prompt_submit_accepts_utf8_bom_active_pointer(self) -> None: + def test_utf8_bom_active_pointer_cannot_bypass_multiple_plan_refusal(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) safe = root / ".planning" / "safe" write_plan_in_dir(safe, goal="BOM-safe goal") decoy = root / ".planning" / "newer-decoy" write_plan_in_dir(decoy, goal="Newest fallback goal") - # If BOM stripping regresses, newest-dir fallback must select the - # decoy and make this test fail instead of masking the bug. + # Neither pointer parsing nor newest fallback may select a task + # while another named plan competes with it. os.utime(safe, (100, 100)) os.utime(decoy, (200, 200)) (root / ".planning" / ".active_plan").write_bytes(b"\xef\xbb\xbfsafe\n") @@ -326,9 +326,15 @@ class HookResolverIntegrationTests(unittest.TestCase): result = run_hook("user-prompt-submit.sh", root) self.assertEqual(0, result.returncode, result.stderr) - self.assertIn("BOM-safe goal", result.stdout) + self.assertIn("Set PLAN_ID=", result.stdout) + self.assertNotIn("BOM-safe goal", result.stdout) self.assertNotIn("Newest fallback goal", result.stdout) + # The same BOM pointer remains usable in a single-plan project. + (decoy / "task_plan.md").unlink() + single = run_hook("user-prompt-submit.sh", root) + self.assertIn("BOM-safe goal", single.stdout) + def test_user_prompt_submit_rejects_traversal_plan_id(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) diff --git a/tests/test_inject_plan_python_parity.py b/tests/test_inject_plan_python_parity.py index 15a94ff..e148ad3 100644 --- a/tests/test_inject_plan_python_parity.py +++ b/tests/test_inject_plan_python_parity.py @@ -245,7 +245,6 @@ class InjectorParityTests(ParityCase): def test_active_plan_pointer_with_padding_selects_the_slug(self): self.slug("2026-09-01-alpha", mtime=1_700_000_000) - self.slug("2026-09-03-beta", mtime=1_700_000_100) write(self.project / ".planning" / ".active_plan", " 2026-09-01-alpha \r\n") self.assert_parity(expect={"userprompt": "Task Plan: 2026-09-01-alpha"}, forbid={"userprompt": "2026-09-03-beta"}) @@ -255,17 +254,15 @@ class InjectorParityTests(ParityCase): UTF-16LE pointer without a BOM as its ASCII slug; a UTF-16 BOM is not whitespace and still invalidates the pointer.""" self.slug("2026-09-01-alpha", mtime=1_700_000_000) - self.slug("2026-09-03-beta", mtime=1_700_000_100) for pointer in (b"2026-09-01-alpha\x00\n", "2026-09-01-alpha\n".encode("utf-16-le")): write(self.project / ".planning" / ".active_plan", pointer) self.assert_parity(expect={"userprompt": "Task Plan: 2026-09-01-alpha"}, forbid={"userprompt": "2026-09-03-beta"}) self.assert_event_parity(events=("session-start", "post-tool-use")) write(self.project / ".planning" / ".active_plan", "2026-09-01-alpha\n".encode("utf-16")) - self.assert_parity(expect={"userprompt": "Task Plan: 2026-09-03-beta"}) + self.assert_parity(expect={"userprompt": "Task Plan: 2026-09-01-alpha"}) - def test_bom_pointer_is_rejected_and_newest_wins(self): - self.slug("2026-09-01-alpha", mtime=1_700_000_000) + def test_bom_pointer_is_rejected_and_single_live_plan_wins(self): self.slug("2026-09-03-beta", mtime=1_700_000_100) write(self.project / ".planning" / ".active_plan", b"\xef\xbb\xbf2026-09-01-alpha\n") self.assert_parity(expect={"userprompt": "Task Plan: 2026-09-03-beta"}) diff --git a/tests/test_plan_selector_binding.py b/tests/test_plan_selector_binding.py index 5a0f659..caaf5cc 100644 --- a/tests/test_plan_selector_binding.py +++ b/tests/test_plan_selector_binding.py @@ -93,6 +93,8 @@ class PlanSelectorBindingTests(unittest.TestCase): def test_empty_plan_id_is_not_a_selector(self): """init-session.sh passes PLAN_ID="" into attest-plan.sh on the legacy path, so an empty value must keep meaning "unset".""" + # One live plan remains compatible; an empty pin cannot choose among two. + (self.root / ".planning" / "2026-07-21-alpha" / "task_plan.md").unlink() out = self._run("resolve-plan-dir.sh", plan_id="").stdout.strip() self.assertTrue(out.endswith("2026-07-21-beta"), out) diff --git a/tests/test_resolve_plan_dir.py b/tests/test_resolve_plan_dir.py index eb7d79e..b8bb355 100644 --- a/tests/test_resolve_plan_dir.py +++ b/tests/test_resolve_plan_dir.py @@ -66,7 +66,7 @@ class ResolvePlanDirTests(unittest.TestCase): self.assertEqual(0, result.returncode, result.stderr) self.assertTrue(result.stdout.strip().endswith("beta")) - def test_falls_back_to_newest_dir(self) -> None: + def test_multiple_live_plans_refuse_newest_fallback(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) old = root / ".planning" / "older" @@ -80,10 +80,7 @@ class ResolvePlanDirTests(unittest.TestCase): os.utime(new, None) result = self.run_resolver(root) self.assertEqual(0, result.returncode, result.stderr) - self.assertTrue( - result.stdout.strip().endswith("newer"), - f"expected newer, got {result.stdout!r}", - ) + self.assertEqual("", result.stdout.strip()) def test_legacy_root_plan_emits_empty(self) -> None: # When no .planning/ but cwd/task_plan.md exists, resolver emits empty so diff --git a/tests/test_resolver_parity.py b/tests/test_resolver_parity.py index 40753ec..5e872e2 100644 --- a/tests/test_resolver_parity.py +++ b/tests/test_resolver_parity.py @@ -155,23 +155,23 @@ class ResolverParityTests(unittest.TestCase): """PLAN_ID="" is not a selector; resolution proceeds normally. init-session.sh passes PLAN_ID="${PLAN_ID:-}" into attest-plan.sh on - the legacy path, so an empty value must keep resolving the pointer - rather than failing closed. + the legacy path. Empty remains unset, so it cannot select between + multiple named plans, even with a shared pointer. """ self._plan("2026-07-21-alpha") self._plan("2026-07-21-beta", mtime_offset=60) (self.tmp / ".planning" / ".active_plan").write_text( "2026-07-21-beta\n", encoding="utf-8" ) - self.assert_parity({"PLAN_ID": ""}, "2026-07-21-beta") + self.assert_parity({"PLAN_ID": ""}, None) - def test_active_plan_pointer(self): + def test_active_plan_pointer_cannot_select_between_multiple_plans(self): self._plan("2026-07-21-alpha", mtime_offset=60) self._plan("2026-07-21-beta") (self.tmp / ".planning" / ".active_plan").write_text( "2026-07-21-beta\n", encoding="utf-8" ) - self.assert_parity(None, "2026-07-21-beta") + self.assert_parity(None, None) def test_active_plan_invalid_slug_falls_through_to_newest(self): self._plan("2026-07-21-alpha", mtime_offset=60) diff --git a/tests/test_session_plan_ambiguity.py b/tests/test_session_plan_ambiguity.py index 8ca1ef2..d8b0592 100644 --- a/tests/test_session_plan_ambiguity.py +++ b/tests/test_session_plan_ambiguity.py @@ -274,7 +274,41 @@ class SessionPlanAmbiguityTests(unittest.TestCase): legacy = self.native( "run_sh.py", session_id="unattached", runner_arg="user-prompt-submit.sh" ) - self.assertIn("PLAN-B", legacy.stdout) + self.assertIn(NOTICE, legacy.stdout) + self.assertNotIn("PLAN-B", legacy.stdout) + + def test_unarmed_sessions_refuse_every_native_event_after_pointer_changes(self) -> None: + self.write_scoped_plan("plan-a", "PLAN-A") + self.write_scoped_plan("plan-b", "PLAN-B") + routes = ( + ("run_sh.py", "user-prompt-submit.sh"), + ("run_sh.py", "session-start.sh"), + ("run_sh.py", "pre-compact.sh"), + ("pre_tool_use.py", None), + ("permission_request.py", None), + ("post_tool_use.py", None), + ("stop.py", None), + ) + for pointer in ("plan-a", "plan-b"): + self.point(pointer) + for script, arg in routes: + with self.subTest(pointer=pointer, script=script, arg=arg): + result = self.native(script, runner_arg=arg) + self.assertEqual(0, result.returncode, result.stderr) + if arg == "user-prompt-submit.sh": + self.assertIn(NOTICE, result.stdout) + else: + self.assertEqual("", result.stdout.strip()) + self.assertNotIn("PLAN-A", result.stdout) + self.assertNotIn("PLAN-B", result.stdout) + + for plan_id, marker in (("plan-a", "PLAN-A"), ("plan-b", "PLAN-B")): + result = self.native( + "run_sh.py", runner_arg="user-prompt-submit.sh", + env_extra={"PLAN_ID": plan_id}, + ) + self.assertIn(marker, result.stdout) + self.assertNotIn(NOTICE, result.stdout) def test_unattached_session_keeps_existing_isolation_refusal(self) -> None: self.build_two_plan_tree() diff --git a/tests/test_set_active_plan.py b/tests/test_set_active_plan.py index 0b4a763..ac15efb 100644 --- a/tests/test_set_active_plan.py +++ b/tests/test_set_active_plan.py @@ -98,7 +98,15 @@ class SetActivePlanTests(unittest.TestCase): check=False, ) self.assertEqual(0, resolved.returncode, resolved.stderr) - self.assertTrue(resolved.stdout.strip().endswith("selected")) + self.assertEqual("", resolved.stdout.strip()) + # Pointer bytes are still portable; only single-plan discovery + # may use them without an explicit session pin. + (newest / "task_plan.md").unlink() + single = subprocess.run( + [SH, str(RESOLVE_SH)], cwd=str(root), text=True, + encoding="utf-8", capture_output=True, check=False, + ) + self.assertTrue(single.stdout.strip().endswith("selected")) def test_no_args_no_active_plan_prints_none(self) -> None: with tempfile.TemporaryDirectory() as tmp: @@ -141,8 +149,8 @@ class SetActivePlanTests(unittest.TestCase): self.assertNotEqual(0, result.returncode) self.assertIn("not found", result.stderr) - def test_resolver_picks_up_newly_set_plan(self) -> None: - # End-to-end: set-active-plan.sh then resolve-plan-dir.sh returns correct dir + def test_shared_pointer_changes_do_not_bind_multiple_plans(self) -> None: + # Writing a shared pointer does not bind any individual session. from pathlib import Path as P resolve_sh = REPO_ROOT / "scripts" / "resolve-plan-dir.sh" with tempfile.TemporaryDirectory() as tmp: @@ -153,7 +161,7 @@ class SetActivePlanTests(unittest.TestCase): plan_b.mkdir(parents=True) (plan_a / "task_plan.md").write_text("# A\n", encoding="utf-8") (plan_b / "task_plan.md").write_text("# B\n", encoding="utf-8") - # Pin to task-a + # Set the shared pointer to task-a. run_set_active(root, "2026-task-a") result = subprocess.run( ["sh", str(resolve_sh)], @@ -163,7 +171,7 @@ class SetActivePlanTests(unittest.TestCase): capture_output=True, check=False, ) - self.assertTrue(result.stdout.strip().endswith("2026-task-a")) + self.assertEqual("", result.stdout.strip()) # Switch to task-b run_set_active(root, "2026-task-b") result = subprocess.run( @@ -174,7 +182,13 @@ class SetActivePlanTests(unittest.TestCase): capture_output=True, check=False, ) - self.assertTrue(result.stdout.strip().endswith("2026-task-b")) + self.assertEqual("", result.stdout.strip()) + pinned = subprocess.run( + ["sh", str(resolve_sh)], cwd=str(root), text=True, + encoding="utf-8", capture_output=True, check=False, + env=dict(os.environ, PLAN_ID="2026-task-a"), + ) + self.assertTrue(pinned.stdout.strip().endswith("2026-task-a")) if __name__ == "__main__": diff --git a/tests/test_unarmed_plan_ambiguity.py b/tests/test_unarmed_plan_ambiguity.py new file mode 100644 index 0000000..19c2d06 --- /dev/null +++ b/tests/test_unarmed_plan_ambiguity.py @@ -0,0 +1,144 @@ +"""Issue #240: shared pointers and mtimes never bind multiple named plans.""" +from __future__ import annotations + +import os +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] +SCRIPTS = ROOT / "scripts" +NOTICE = "Set PLAN_ID=" +TOKEN = "PWF_PLAN_AMBIGUOUS_V1" + + +@unittest.skipUnless(shutil.which("sh"), "requires POSIX sh") +class UnarmedPlanAmbiguityTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory(prefix="pwf-unarmed-") + self.root = Path(self.temp.name) + self.env = os.environ.copy() + for key in ("PLAN_ID", "PWF_PLAN_ROOT", "PWF_SESSION_ID", "PWF_SESSION_KEY", "PLANNING_DISABLED"): + self.env.pop(key, None) + self.env.update(PWF_TRUSTED_PYTHON=sys.executable, PYTHON_BIN=sys.executable, + XDG_CACHE_HOME=str(self.root / "cache")) + + def tearDown(self): + self.temp.cleanup() + + def plan(self, name): + directory = self.root / ".planning" / name + directory.mkdir(parents=True, exist_ok=True) + (directory / "task_plan.md").write_text("# NAMED-" + name + "\n", encoding="utf-8") + return directory + + def two_plans(self, *, legacy=False): + self.plan("alpha") + self.plan("beta") + if legacy: + (self.root / "task_plan.md").write_text( + "# LEGACY-ROOT\n\n### Phase 1\n- **Status:** complete\n", encoding="utf-8" + ) + + def run_script(self, name, *args, extra=None): + env = dict(self.env) + env.update(extra or {}) + if name.endswith(".py"): + prefix = [sys.executable, "-I"] + elif name.endswith(".ps1"): + executable = shutil.which("pwsh") or shutil.which("powershell") + if not executable: + self.skipTest("requires PowerShell") + prefix = [executable, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File"] + else: + prefix = ["sh"] + return subprocess.run( + prefix + [str(SCRIPTS / name), *args], cwd=self.root, + env=env, capture_output=True, text=True, encoding="utf-8", timeout=90, + ) + + def test_shared_resolvers_refuse_pointer_and_mtime_with_diagnostic_probe(self): + self.two_plans() + pointer = self.root / ".planning" / ".active_plan" + for selected in (None, "alpha", "beta"): + if selected: + pointer.write_text(selected + "\n", encoding="utf-8") + for script, probe in (("resolve-plan-dir.sh", "--check-ambiguity"), + ("resolve-plan-dir.ps1", "-CheckAmbiguity")): + with self.subTest(pointer=selected, script=script): + result = self.run_script(script) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual("", result.stdout.strip()) + diagnostic = self.run_script(script, probe) + self.assertEqual(0, diagnostic.returncode, diagnostic.stderr) + self.assertEqual(TOKEN, diagnostic.stdout.strip()) + + def test_both_injectors_refuse_all_unbound_contexts_without_exposing_plan(self): + self.two_plans(legacy=True) + (self.root / ".planning" / ".active_plan").write_text("alpha\n", encoding="utf-8") + for context in ("userprompt", "pretool", "precompact", "preflight", "validate"): + outputs = [] + for script in ("inject-plan.sh", "inject-plan.py"): + result = self.run_script(script, "--context=" + context) + self.assertEqual(0, result.returncode, result.stderr) + self.assertNotIn("NAMED-", result.stdout) + self.assertNotIn("LEGACY-ROOT", result.stdout) + if context == "userprompt": + self.assertEqual(1, result.stdout.count(NOTICE)) + else: + self.assertEqual("", result.stdout) + outputs.append(result.stdout) + self.assertEqual(outputs[0], outputs[1]) + + def test_explicit_plan_binding_works_and_root_pin_alone_is_insufficient(self): + self.two_plans() + for script, probe in (("resolve-plan-dir.sh", "--check-ambiguity"), + ("resolve-plan-dir.ps1", "-CheckAmbiguity")): + pinned = self.run_script(script, extra={"PLAN_ID": "alpha"}) + self.assertEqual(0, pinned.returncode, pinned.stderr) + self.assertTrue(pinned.stdout.strip().endswith("alpha"), pinned.stdout) + self.assertEqual("", self.run_script(script, probe, extra={"PLAN_ID": "alpha"}).stdout) + root_only = self.run_script(script, probe, extra={"PWF_PLAN_ROOT": str(self.root)}) + self.assertEqual(TOKEN, root_only.stdout.strip()) + for script in ("inject-plan.sh", "inject-plan.py"): + accepted = self.run_script(script, "--context=validate", extra={"PLAN_ID": "beta"}) + self.assertEqual("PWF_PLAN_ACCEPTED_V1", accepted.stdout.strip(), accepted.stderr) + + def test_single_named_plan_with_legacy_root_is_compatible_until_isolation_armed(self): + self.plan("alpha") + (self.root / "task_plan.md").write_text("# LEGACY-ROOT\n", encoding="utf-8") + for script, probe in (("resolve-plan-dir.sh", "--check-ambiguity"), + ("resolve-plan-dir.ps1", "-CheckAmbiguity")): + self.assertTrue(self.run_script(script).stdout.strip().endswith("alpha")) + self.assertEqual("", self.run_script(script, probe).stdout) + (self.root / ".planning" / "sessions").mkdir() + for script, probe in (("resolve-plan-dir.sh", "--check-ambiguity"), + ("resolve-plan-dir.ps1", "-CheckAmbiguity")): + self.assertEqual("", self.run_script(script).stdout) + self.assertEqual(TOKEN, self.run_script(script, probe).stdout.strip()) + + def test_ambiguous_completion_and_attestation_do_not_fall_back_to_root(self): + self.two_plans(legacy=True) + for suffix in ("sh", "ps1"): + if suffix == "ps1" and os.name != "nt": + continue # PowerShell attestation deliberately supports Windows only. + completion = self.run_script("check-complete." + suffix) + self.assertEqual(0, completion.returncode, completion.stderr) + self.assertEqual("", completion.stdout.strip()) + attested = self.run_script("attest-plan." + suffix) + self.assertNotEqual(0, attested.returncode) + if suffix == "sh": + self.assertIn("PLAN_ID", attested.stdout + attested.stderr) + self.assertFalse((self.root / ".plan-attestation").exists()) + self.assertFalse(list((self.root / ".planning").glob("*/.attestation"))) + + def test_python_dispatcher_does_not_fall_back_to_root_for_other_events(self): + self.two_plans(legacy=True) + for event in ("session-start", "post-tool-use", "pre-tool-use", "pre-compact"): + result = self.run_script("inject-plan.py", "--claude-event=" + event) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual("", result.stdout)