diff --git a/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 b/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 index 2ad1609..e5689b5 100644 --- a/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 +++ b/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 @@ -133,6 +133,34 @@ Describe 'AiSdlcConfig' { @($config.dotnet.Keys) | Should -Not -Contain 'docGeneratorEndpoint' } + It 'refuses a push to the trunk unless git.allowTrunkPush is the JSON value true' -ForEach @( + @{ Json = $null; Expected = $false } + @{ Json = '{}'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": false } }'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": "true" } }'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": 1 } }'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": true } }'; Expected = $true } + ) { + Test-AiSdlcTrunkPushAllowed -RepoRoot (New-ConfigRepo -Json $Json -Git) | Should -Be $Expected + } + + It 'reads git.allowTrunkPush from the commit that -Ref names, not from the working tree' { + $root = New-ConfigRepo -Json '{ "git": { "allowTrunkPush": true } }' -Git + git -C $root config user.email probe@example.invalid + git -C $root config user.name probe + git -C $root add aisdlc.json + git -C $root commit --quiet -m on --no-verify + git -C $root update-ref refs/remotes/origin/main HEAD + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value '{}' + git -C $root commit --quiet -am off --no-verify + git -C $root update-ref refs/remotes/origin/master HEAD + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value '{ "git": { "allowTrunkPush": true } }' + + Test-AiSdlcTrunkPushAllowed -RepoRoot $root -Ref 'refs/remotes/origin/main' | Should -BeTrue + Test-AiSdlcTrunkPushAllowed -RepoRoot $root -Ref 'refs/remotes/origin/master' | Should -BeFalse + Test-AiSdlcTrunkPushAllowed -RepoRoot $root -Ref 'refs/remotes/origin/missing' | Should -BeFalse + } + It 'reads the other repositories whose issues a pull request may name from tracker.acceptedRepositories' { $config = Get-AiSdlcConfig -RepoRoot (New-ConfigRepo -Json '{ "tracker": { "acceptedRepositories": ["contoso/ai-sdlc"] } }') diff --git a/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 b/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 index afaad1b..3d3449e 100644 --- a/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 +++ b/.claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 @@ -118,6 +118,9 @@ $script:Defaults = [ordered]@{ secretScan = [ordered]@{ allowPaths = @() } + git = [ordered]@{ + allowTrunkPush = $false + } tracker = [ordered]@{ repository = '' keyPattern = '' @@ -270,6 +273,31 @@ function Test-AiSdlcPack { return @($config.packs) -contains $Name } +function Test-AiSdlcTrunkPushAllowed { + # True only when aisdlc.json sets git.allowTrunkPush to the JSON value true. A string such as "true" + # does not count, so a typo never opens the trunk. The push hooks still refuse a delete or a force push. + # With -Ref, the file is read from that commit, not from the working tree. The push hooks pass the + # remote-tracking ref of the branch they guard, such as refs/remotes/origin/main, so a change in the + # working tree cannot switch the setting on: it counts only once it is on the remote main branch. + # A missing ref, a missing file or a file that is not valid JSON counts as false. + [CmdletBinding()] + param([string]$RepoRoot, [string]$Ref) + + if ($Ref) { + if (-not $RepoRoot) { $RepoRoot = Find-AiSdlcRepoRoot } + $text = git -C $RepoRoot show "$($Ref):$($script:ConfigFileName)" 2>$null + if ($LASTEXITCODE -ne 0 -or -not $text) { return $false } + try { $values = ($text -join "`n") | ConvertFrom-Json -AsHashtable } catch { return $false } + $section = if ($values -is [System.Collections.IDictionary]) { $values['git'] } else { $null } + $value = if ($section -is [System.Collections.IDictionary]) { $section['allowTrunkPush'] } else { $null } + return ($value -is [bool]) -and $value + } + + $config = Get-AiSdlcConfig -RepoRoot $RepoRoot + $value = $config.git['allowTrunkPush'] + return ($value -is [bool]) -and $value +} + function Get-AiSdlcStoryPattern { [CmdletBinding()] param([string]$RepoRoot) @@ -383,4 +411,4 @@ function Get-AiSdlcPackage { } } -Export-ModuleMember -Function Find-AiSdlcRepoRoot, Get-AiSdlcConfig, Get-AiSdlcPath, Test-AiSdlcPack, Get-AiSdlcStoryPattern, Get-AiSdlcBranchStoryKey, Get-AiSdlcStoryKey, ConvertTo-AiSdlcStoryKey, Get-AiSdlcPackage +Export-ModuleMember -Function Find-AiSdlcRepoRoot, Get-AiSdlcConfig, Get-AiSdlcPath, Test-AiSdlcPack, Test-AiSdlcTrunkPushAllowed,Get-AiSdlcStoryPattern, Get-AiSdlcBranchStoryKey, Get-AiSdlcStoryKey, ConvertTo-AiSdlcStoryKey, Get-AiSdlcPackage diff --git a/.claude/skills/ai-sdlc-gates/sync-manifest.json b/.claude/skills/ai-sdlc-gates/sync-manifest.json index fcfc9ec..fb4e8ae 100644 --- a/.claude/skills/ai-sdlc-gates/sync-manifest.json +++ b/.claude/skills/ai-sdlc-gates/sync-manifest.json @@ -21,8 +21,8 @@ ".claude/skills/ai-sdlc-gates/assets/actions/ai-sdlc-pr/action.yml": "f80ed84d896859668d74cd47bf327a714d383f8a", ".claude/skills/ai-sdlc-gates/assets/agents-core.md": "f7e45643bf0f5447d480b588784b2c599cc7f320", ".claude/skills/ai-sdlc-gates/gates.json": "cf6f5a2d284f0b22f0f4a89292037caed3e53c19", - ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1": "afaad1b3853d71d4f1016e2f9f9df4a1ffdbc03d", - ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1": "2ad1609653980ca3bada51c7cd470e4ba6ffcbe9", + ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1": "3d3449e30e25d842f355ce74a2150775ab8b3e0d", + ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1": "e5689b5deec495e74dc991786596903d70283263", ".claude/skills/ai-sdlc-gates/scripts/apm-version.json": "fbbd8a51e60e3a7e653a35bc9b959f2bf332ab74", ".claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.ps1": "64c6d651e638b0fe15d20cde02418e8021a5cdfc", ".claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.Tests.ps1": "d9ac6b357e1fc4dbf7deacce04ec365b8515e627", @@ -114,9 +114,9 @@ ".claude/skills/ai-sdlc-handbook/references/docs-wiki.md": "038477c9b704a5b545decfe5e3f3f9970d8d1f9d", ".claude/skills/ai-sdlc-handbook/references/first-story.md": "be899eb395dafb2d704b8f0cb46bac1fb5e593c8", ".claude/skills/ai-sdlc-handbook/references/language.md": "ca703edeb9e07e28e32a39fbcc9116b64db444d8", - ".claude/skills/ai-sdlc-handbook/references/layout.md": "eaabf81a917d8b826f26318bcef7934fbbecbdfa", + ".claude/skills/ai-sdlc-handbook/references/layout.md": "b6683bbb25c614a5d0a3f8283d4e46bfa110bcd2", ".claude/skills/ai-sdlc-handbook/references/node-templates.md": "580c9c92c470a6abbaa0145cc3060ff1a11d247c", - ".claude/skills/ai-sdlc-handbook/references/orchestration.md": "5b6c8c38a5d17ca59cc38794a7750355c508d262", + ".claude/skills/ai-sdlc-handbook/references/orchestration.md": "22373827d6ecd454d93cfb652f76c5204f3215e2", ".claude/skills/ai-sdlc-handbook/references/pattern.md": "f0f2a8cf3dac267cda0fd3783d86bddba5afc7c0", ".claude/skills/ai-sdlc-handbook/references/personas.md": "083c57f3e4e88519a9d42d7e51559c5165faacff", ".claude/skills/ai-sdlc-handbook/references/plan.md": "8c6fde8e741106f8fe625b8b9a4e98b6d5ba912f", @@ -130,13 +130,13 @@ ".claude/skills/ai-sdlc-hooks/githooks/post-commit": "f18058f9a38efc164e13801d61bb05329a056109", ".claude/skills/ai-sdlc-hooks/githooks/post-merge": "bb373f33abd09294cda55d7fc4499f32672eb372", ".claude/skills/ai-sdlc-hooks/githooks/post-rewrite": "e4c187dffeeaba569901f3424fae476b17c14927", - ".claude/skills/ai-sdlc-hooks/githooks/pre-push": "ba5d1df1483ae80b7d6a009aafdb820f3629a2e3", + ".claude/skills/ai-sdlc-hooks/githooks/pre-push": "490c81fd333d26cf9f09877fecb369d531710d66", ".claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.ps1": "52ffd325d13cd403e7bfea0de8098c530c129b65", ".claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.Tests.ps1": "ef5b30f0811181462d354a61d3e64c19f0c305e2", ".claude/skills/ai-sdlc-hooks/scripts/Deny-AgentMerge.ps1": "535543b86886fcc582912ded5373b0bd3d756356", ".claude/skills/ai-sdlc-hooks/scripts/Deny-AgentMerge.Tests.ps1": "c8dfc8a4f31b6ec89a6d680e936e3563afa62bcb", - ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1": "e3a58c32a38844707aa0b2f4c14961af3ed2c870", - ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.Tests.ps1": "c7da2e83782aa42478fc3b416af5926b8bdd376c", + ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1": "117d527a075a78017b0a199703d84a6807797573", + ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.Tests.ps1": "73947a2a69aecf0e73a11640a0c21d081d43c4d8", ".claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.ps1": "ca703a9e057633e39af161d9e43a042429813acc", ".claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.Tests.ps1": "d15bf28d4191b5e2bca8ce59621ad03a1ef6198c", ".claude/skills/ai-sdlc-hooks/scripts/Find-RetiredSkillNames.ps1": "da6172d61799df3f37c5f70a103cddddb5517cb0", @@ -156,12 +156,12 @@ ".claude/skills/ai-sdlc-hooks/scripts/Invoke-PostEditCheck.Tests.ps1": "3429e18735428ab387ab58ae276d4195b32e7999", ".claude/skills/ai-sdlc-hooks/scripts/New-AiSdlcUpdatePullRequest.ps1": "b54f90deb751d0b0dbf8ee8fbc3f39b3300b3b3d", ".claude/skills/ai-sdlc-hooks/scripts/New-AiSdlcUpdatePullRequest.Tests.ps1": "77e4b627da13ea9887f3918f66bc5b33dfd91c56", - ".claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1": "10a332050267c363ee019fc1911495b8bc3bfb00", + ".claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1": "718248ded3ba695b29d4d7eb59e8f09130cf918c", ".claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.ps1": "56abaf1de1c17bea94c26e128f8c41e275a74f87", ".claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.Tests.ps1": "8db305e438bc7ee404eb5106eacbe5581738f148", ".claude/skills/ai-sdlc-hooks/scripts/WindowsLongPaths.ps1": "fa3d7f509b6f9a1a935178c1eaf0d1566b7afead", ".claude/skills/ai-sdlc-hooks/scripts/WindowsLongPaths.Tests.ps1": "21a220ca2ccc65a56ec427a6e74c5a5dbfbfd4ef", - ".claude/skills/ai-sdlc-hooks/SKILL.md": "0bb9a972fbf928e359cbdfa24cc5781d43d0ab54", + ".claude/skills/ai-sdlc-hooks/SKILL.md": "a5b99bcdf6bf93721136ee3a3e7075632fdf068d", ".claude/skills/capture-learning/SKILL.md": "32300ce3189109b0d717f975623e8d7ccec1c821", ".claude/skills/convene-personas/SKILL.md": "449708a02206bec35a83d2f48ca6366c8b78e3a2", ".claude/skills/docs-wiki-conventions/SKILL.md": "0679512fa39815b63da6373ff29bb6eadc52ff24", @@ -399,7 +399,7 @@ ".claude/skills/github-pull-requests/scripts/Update-PullRequest.Tests.ps1": "f63886f04b76fdddda7b23dd886586fb04317194", ".claude/skills/github-pull-requests/scripts/Wait-WorkflowRun.ps1": "27fd4ca10468eb3fecd2580f2f1e493fd115ae9d", ".claude/skills/github-pull-requests/scripts/Wait-WorkflowRun.Tests.ps1": "3cf5da073805761e52b2af1ea7399e71584ee342", - ".claude/skills/github-pull-requests/SKILL.md": "c2d1d10ae6721687e84a21c5bee71d1332387b76", + ".claude/skills/github-pull-requests/SKILL.md": "c42530e8588f05881da325e74a57eb79ae80fe1c", ".claude/skills/write-pipeline/examples/infrastructure/pr.yml": "b3758e9bae5a9c5dec006af329218e2e23244a78", ".claude/skills/write-pipeline/examples/infrastructure/release.yml": "967c35ded6d96c0b881b89a6ef9599960f001719", ".claude/skills/write-pipeline/examples/web-app/build-and-test.yml": "f9155849e3229580d5e91fc8ea257275807cf220", diff --git a/.claude/skills/ai-sdlc-handbook/references/layout.md b/.claude/skills/ai-sdlc-handbook/references/layout.md index eaabf81..b6683bb 100644 --- a/.claude/skills/ai-sdlc-handbook/references/layout.md +++ b/.claude/skills/ai-sdlc-handbook/references/layout.md @@ -129,6 +129,7 @@ installs. `AiSdlcConfig.psm1` takes each dependency whose path ends in `packs/ . # `git lfs pre-push` needs the same lines at the end, so they are read once and kept. +# The setting is read from aisdlc.json in the commit that is the tip of the remote branch now. git +# learns that commit from the remote while it pushes and passes it on stdin as , so neither +# an edit in the working tree nor a local ref can switch the setting on. A new branch (zero sha) or a tip +# that is not in this clone counts as off. $1 is that remote sha. +trunk_push_allowed() { + is_zero_sha "$1" && return 1 + git cat-file -e "$1^{commit}" 2>/dev/null || return 1 + command -v pwsh >/dev/null 2>&1 || return 1 + module="$(dirname "$0")/../../ai-sdlc-gates/scripts/AiSdlcConfig.psm1" + [ -f "$module" ] || return 1 + AISDLC_CONFIG_MODULE="$module" AISDLC_REPO_ROOT="$(git rev-parse --show-toplevel)" AISDLC_REF="$1" pwsh -NoProfile -NonInteractive -Command 'try { Import-Module $env:AISDLC_CONFIG_MODULE -Force; if (Test-AiSdlcTrunkPushAllowed -RepoRoot $env:AISDLC_REPO_ROOT -Ref $env:AISDLC_REF) { exit 0 } } catch { }; exit 1' >/dev/null 2>&1 +} + +is_zero_sha() { + case "$1" in + *[!0]*) return 1 ;; + *) return 0 ;; + esac +} + refs=$(cat) while read -r local_ref local_sha remote_ref remote_sha; do case "$remote_ref" in refs/heads/main|refs/heads/master) - echo "pre-push: a push to ${remote_ref#refs/heads/} is refused. A change reaches main only through a pull request that a person reviews and merges. Push the story branch and open or update its pull request instead." >&2 - exit 1 + branch=${remote_ref#refs/heads/} + if ! trunk_push_allowed "$remote_sha"; then + echo "pre-push: a push to $branch is refused. A change reaches main only through a pull request that a person reviews and merges. Push the story branch and open or update its pull request instead." >&2 + if ! is_zero_sha "$remote_sha" && ! git cat-file -e "$remote_sha^{commit}" 2>/dev/null; then + echo "pre-push: the remote tip of $branch is not in this clone, so git.allowTrunkPush cannot be read from it. If the setting is on $branch, fetch first, then push again." >&2 + fi + exit 1 + fi + if is_zero_sha "$local_sha"; then + echo "pre-push: git.allowTrunkPush in aisdlc.json allows an update of $branch, not a delete. The delete is refused." >&2 + exit 1 + fi + if ! git merge-base --is-ancestor "$remote_sha" "$local_sha"; then + echo "pre-push: git.allowTrunkPush in aisdlc.json allows a fast-forward of $branch, not a force push. The push is refused." >&2 + exit 1 + fi + echo "pre-push: git.allowTrunkPush in aisdlc.json allows this fast-forward of $branch. The blocking gates still run." ;; esac done <&1 | Out-String + return [pscustomobject]@{ Output = $out; ExitCode = $LASTEXITCODE } + } + } + + It 'allows when the change is complete' -ForEach @( + @{ Command = 'git push origin main' } + @{ Command = 'git push' } + @{ Command = 'git push -u origin HEAD:main' } + @{ Command = 'git push origin master' } + @{ Command = 'git -C C:/repo push origin main' } + @{ Command = 'git push origin main 2>&1' } + @{ Command = 'git push --set-upstream --follow-tags origin main' } + @{ Command = 'git push -uq origin main' } + ) { + $r = Invoke-OptedInHook -Command $Command + $r.Output | Should -Not -Match 'permissionDecision' + } + + It 'refuses , which may delete or rewrite the trunk' -ForEach @( + @{ Command = 'git push --force origin main' } + @{ Command = 'git push -f origin main' } + @{ Command = 'git push --force-with-lease origin main' } + @{ Command = 'git push origin +main' } + @{ Command = 'git push origin :main' } + @{ Command = 'git push --delete origin main' } + @{ Command = 'git push --mirror origin' } + @{ Command = 'git push --all origin' } + @{ Command = 'git push --no-verify origin main' } + @{ Command = 'git -c remote.origin.mirror=true push origin main' } + @{ Command = 'git push origin main && git push -f origin main' } + @{ Command = 'git push origin main; git push origin :main' } + @{ Command = 'git push origin main | tee out.txt' } + @{ Command = 'git push $(echo origin) main' } + @{ Command = 'git push --dele origin main' } + @{ Command = 'git push --no-verif origin main' } + @{ Command = 'git push --forc origin main' } + @{ Command = 'git push --mirr origin' } + @{ Command = 'git push -uf origin main' } + @{ Command = 'git push --repo=other main' } + @{ Command = 'git push origin main \--force' } + @{ Command = 'git push origin main ${X}' } + @{ Command = 'git push origin main {--force,}' } + ) { + $r = Invoke-OptedInHook -Command $Command + $r.Output | Should -Match '"permissionDecision":"deny"' + $r.Output | Should -Match 'allowTrunkPush' + } + + It 'refuses a trunk push when only the working tree switches the setting on' { + $payload = New-Payload -Command 'git push origin main' + $out = & $script:ScriptUnderTest -Payload $payload -CheckScript $script:CleanCheck -CurrentBranch 'main' -ReceiptRoot $script:Receipts -HeadTree $script:GreenTree -RepoRoot $script:LocalOnly *>&1 | Out-String + $out | Should -Match '"permissionDecision":"deny"' + $out | Should -Match 'refs/remotes/origin/main' + } + + It 'still runs the completeness check on a trunk push' { + $r = Invoke-OptedInHook -Command 'git push origin main' -CheckScript $script:GapCheck + $r.Output | Should -Match '"permissionDecision":"deny"' + $r.Output | Should -Not -Match 'main or master is refused' + } + } + Context 'a push whose commit has no green pre-flight, which Checkpoint Bravo relies on' { It 'refuses a complete change whose commit has no receipt, and names wrap-up' { $r = Invoke-Hook -Payload (New-Payload -Command 'git push -u origin feature/1-x') -CheckScript $script:CleanCheck -Tree $script:UncheckedTree diff --git a/.claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 b/.claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 index e3a58c3..117d527 100644 --- a/.claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 +++ b/.claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 @@ -26,7 +26,17 @@ script. A payload this script cannot read as a git push allows for the same reason; the pre-push git hook stays the backstop that no configuration can miss. Before the completeness check it refuses any push to main or master: a named target, a quoted one, - HEAD while on main, a bare push while on main, and --all, --mirror or --branches. A permission rule + HEAD while on main, a bare push while on main, and --all, --mirror or --branches. A repository that + sets git.allowTrunkPush to true in aisdlc.json may update main or master directly, but only in a plain + push that is the whole command: a +refspec, a delete refspec (:main), any push option outside a short + allow list (-u, -q, -v, --set-upstream, --quiet, --verbose, --progress, --no-progress, --porcelain, + --atomic, --follow-tags, each written in full), a git option before push other than -C, and a push + chained with any other command stay refused. So --force, --delete, --mirror and --no-verify are refused, + also when shortened. The setting counts only when it is in aisdlc.json on the remote-tracking ref of + that main branch (refs/remotes//main), so an edit in the working tree cannot switch it on. + That ref lives in the clone, so it is a first check only. The pre-push git hook is the backstop: it + reads the setting from the remote tip that git passes during the push, which no local ref can change. The rest of the checks still run on such a + push. A permission rule cannot see which branch a bare push targets, so this hook is the guard for Claude Code and Copilot CLI alike. It is registered for the Bash tool and for the PowerShell tool, because a repository that allows git push in PowerShell would otherwise push past it. The PowerShell payload has the same @@ -201,17 +211,77 @@ function Test-IsGitPush { function Test-TargetsProtectedBranch { param([string]$CommandLine, [string]$Branch) + # Every push in the command counts, so a second push in a chain cannot reach main unseen. + foreach ($push in [regex]::Matches($CommandLine, "$gitPrefix\s+push(?[^;&|]*)")) { + $all = @($push.Groups['rest'].Value.Trim() -split '\s+' | Where-Object { $_ } | ForEach-Object { $_ -replace '[''"]', '' }) + if (@($all | Where-Object { $_ -in @('--all', '--mirror', '--branches') }).Count -gt 0) { return $true } + $words = @($all | Where-Object { -not $_.StartsWith('-') }) + $refspecs = @($words | Select-Object -Skip 1) + if ($refspecs.Count -eq 0) { + if ($Branch -in $protectedBranches) { return $true } + continue + } + foreach ($refspec in $refspecs) { + $target = ($refspec.TrimStart('+') -split ':')[-1] -replace '^refs/heads/', '' + if ($target -in $protectedBranches) { return $true } + if ($target -eq 'HEAD' -and $Branch -in $protectedBranches) { return $true } + } + } + return $false +} + +function Get-TrunkTrackingRef { + # The remote-tracking ref of the main branch a plain push updates, such as refs/remotes/origin/main. + # git.allowTrunkPush is read from that commit, so only a setting that is already on the remote counts. + # Call it only after Test-RewritesBranch passed, so the command is one plain push. + param([string]$CommandLine, [string]$Branch, [string]$Root) + $push = [regex]::Match($CommandLine, "$gitPrefix\s+push(?[^;&|]*)") - if (-not $push.Success) { return $false } $all = @($push.Groups['rest'].Value.Trim() -split '\s+' | Where-Object { $_ } | ForEach-Object { $_ -replace '[''"]', '' }) - if (@($all | Where-Object { $_ -in @('--all', '--mirror', '--branches') }).Count -gt 0) { return $true } - $words = @($all | Where-Object { -not $_.StartsWith('-') }) - $refspecs = @($words | Select-Object -Skip 1) - if ($refspecs.Count -eq 0) { return $Branch -in $protectedBranches } + $words = @($all | Where-Object { $_ -ne '2>&1' -and -not $_.StartsWith('-') }) + $remote = if ($words.Count -gt 0) { $words[0] } else { '' } + if (-not $remote -and $Branch) { $remote = try { [string](git -C $Root config "branch.$Branch.remote" 2>$null) } catch { '' } } + if (-not $remote) { $remote = 'origin' } + $target = $Branch + foreach ($refspec in @($words | Select-Object -Skip 1)) { + $name = ($refspec -split ':')[-1] -replace '^refs/heads/', '' + if ($name -eq 'HEAD') { $name = $Branch } + if ($name -in $protectedBranches) { $target = $name; break } + } + return "refs/remotes/$($remote.Trim())/$target" +} + +function Test-RewritesBranch { + # True when the push may delete or rewrite a branch, or when the hook cannot judge it: a force option, + # a +refspec, a delete, a push of every branch, --no-verify (which skips the pre-push git hook), a git + # option before push other than -C, or a command that holds anything besides this one push. Only a + # plain push that is the whole command may update the trunk under git.allowTrunkPush. + param([string]$CommandLine) + + $single = $CommandLine.Trim() -replace '\s+2>&1(?:\s*\|\s*(?i:Out-String))?$', '' + if ($single -match '[;&|<>\r\n`]|\$\(') { return $true } + if ([regex]::Matches($single, '(?:^|\s)' + $gitName + '(?:\s|$)').Count -ne 1) { return $true } + $push = [regex]::Match($single, '^&?\s*' + $gitName + '(?:\s+-C\s+(?:"[^"]*"|''[^'']*''|\S+))?\s+push(?(?:\s.*)?)$') + if (-not $push.Success) { return $true } + $all = @($push.Groups['rest'].Value.Trim() -split '\s+' | Where-Object { $_ } | ForEach-Object { $_ -replace '[''"]', '' }) + # Options pass only from a short allow list, written in full. git accepts a shortened long option, + # such as --dele for --delete, so a deny list of names would miss one. + $safeLong = @('--set-upstream', '--quiet', '--verbose', '--progress', '--no-progress', '--porcelain', '--atomic', '--follow-tags') + foreach ($word in $all) { + # A remote or refspec holds only plain name characters. A backslash, $ or brace could hide an + # option from this check (\--force reaches git as --force), so such a word is refused. + if (-not $word.StartsWith('-')) { + if ($word -notmatch '^[A-Za-z0-9._/:@^~+-]+$') { return $true } + continue + } + if ($word.StartsWith('--')) { + if ($word -notin $safeLong) { return $true } + } + elseif ($word -notmatch '^-[uqv]+$') { return $true } + } + $refspecs = @($all | Where-Object { -not $_.StartsWith('-') } | Select-Object -Skip 1) foreach ($refspec in $refspecs) { - $target = ($refspec.TrimStart('+') -split ':')[-1] -replace '^refs/heads/', '' - if ($target -in $protectedBranches) { return $true } - if ($target -eq 'HEAD' -and $Branch -in $protectedBranches) { return $true } + if ($refspec.StartsWith('+') -or $refspec.StartsWith(':')) { return $true } } return $false } @@ -296,10 +366,26 @@ if (-not $PSBoundParameters.ContainsKey('CurrentBranch')) { $CurrentBranch = try { [string](git -C $targetRoot rev-parse --abbrev-ref HEAD 2>$null) } catch { '' } } if (Test-TargetsProtectedBranch -CommandLine $command -Branch ([string]$CurrentBranch).Trim()) { - Write-Deny ('A push to main or master is refused. A change reaches main only through a pull request that a ' + - 'person reviews and merges (.claude/skills/ai-sdlc-handbook/references/orchestration.md). Push the story ' + - 'branch and open or update its pull request instead.') - exit 0 + $trunkPushAllowed = $targetInstalls -and (Test-AiSdlcTrunkPushAllowed -RepoRoot $targetRoot) + if (-not $trunkPushAllowed) { + Write-Deny ('A push to main or master is refused. A change reaches main only through a pull request that a ' + + 'person reviews and merges (.claude/skills/ai-sdlc-handbook/references/orchestration.md). Push the story ' + + 'branch and open or update its pull request instead.') + exit 0 + } + if (Test-RewritesBranch -CommandLine $command) { + Write-Deny ('git.allowTrunkPush in aisdlc.json allows a plain push to main or master, not a force push, a ' + + 'delete, a push of every branch, --no-verify, a git option before push, or a push chained with other ' + + 'commands. Run the push of main on its own, as git push [remote] [branch].') + exit 0 + } + $trunkRef = Get-TrunkTrackingRef -CommandLine $command -Branch ([string]$CurrentBranch).Trim() -Root $targetRoot + if (-not (Test-AiSdlcTrunkPushAllowed -RepoRoot $targetRoot -Ref $trunkRef)) { + Write-Deny ("git.allowTrunkPush counts only when it is on the remote main branch ($trunkRef), not in the " + + 'working tree alone. Get the setting onto main through a pull request first, or fetch when it is ' + + 'already there.') + exit 0 + } } if (-not $targetInstalls) { exit 0 } diff --git a/.claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 b/.claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 index 10a3320..718248d 100644 --- a/.claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 +++ b/.claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 @@ -68,6 +68,87 @@ Describe 'The pre-push git hook' { $r.Code | Should -Be 0 -Because $r.Output } + Context 'a repository that sets git.allowTrunkPush, which may fast-forward its trunk' { + BeforeAll { + $script:NoPwshHere = -not (Get-Command pwsh -ErrorAction SilentlyContinue) + + # A repository with two commits on main, so a push can be a fast-forward or a rewrite. + function Invoke-TrunkPush { + param([string]$Json, [ValidateSet('FastForward', 'Rewrite', 'Delete', 'UnknownRemote')][string]$Kind, [string]$WorkTreeJson, [string]$SecondJson) + $root = Join-Path ([System.IO.Path]::GetTempPath()) ('pre-push-' + [guid]::NewGuid().ToString('n')) + New-Item -ItemType Directory -Path $root | Out-Null + git -C $root init --quiet --initial-branch main + git -C $root config user.email probe@example.invalid + git -C $root config user.name probe + if ($Json) { + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value $Json + git -C $root add aisdlc.json + } + git -C $root commit --quiet --allow-empty -m one --no-verify + $first = (git -C $root rev-parse HEAD).Trim() + if ($SecondJson) { + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value $SecondJson + git -C $root add aisdlc.json + } + git -C $root commit --quiet --allow-empty -m two --no-verify + $second = (git -C $root rev-parse HEAD).Trim() + # The remote-tracking ref may point anywhere; the hook must read the remote sha from stdin. + git -C $root update-ref refs/remotes/origin/main $(if ($SecondJson) { $second } else { $first }) + if ($WorkTreeJson) { Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value $WorkTreeJson } + $line = switch ($Kind) { + 'FastForward' { "refs/heads/main $second refs/heads/main $first" } + 'Rewrite' { "refs/heads/main $first refs/heads/main $second" } + 'Delete' { "(delete) $script:Zero refs/heads/main $second" } + 'UnknownRemote' { "refs/heads/main $second refs/heads/main $script:Sha" } + } + Push-Location $root + try { + $output = "$line`n" | & $script:Sh $script:Hook origin 'https://example.invalid/repo' 2>&1 | Out-String + [pscustomobject]@{ Code = $LASTEXITCODE; Output = $output } + } + finally { Pop-Location; Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue } + } + $script:OptIn = '{ "git": { "allowTrunkPush": true } }' + } + + It 'allows a fast-forward of main and says which setting allowed it' -Skip:($script:NoSh -or $script:NoPwshHere) { + $r = Invoke-TrunkPush -Json $script:OptIn -Kind FastForward + $r.Code | Should -Be 0 -Because $r.Output + $r.Output | Should -Match 'git.allowTrunkPush in aisdlc.json allows this fast-forward of main' + } + + It 'refuses a of main' -Skip:($script:NoSh -or $script:NoPwshHere) -ForEach @( + @{ Kind = 'Rewrite'; Reason = 'not a force push' } + @{ Kind = 'Delete'; Reason = 'not a delete' } + @{ Kind = 'UnknownRemote'; Reason = 'Fetch first' } + ) { + $r = Invoke-TrunkPush -Json $script:OptIn -Kind $Kind + $r.Code | Should -Be 1 + $r.Output | Should -Match $Reason + } + + It 'still refuses a fast-forward when the setting is ' -Skip:($script:NoSh -or $script:NoPwshHere) -ForEach @( + @{ Name = 'missing'; Json = '{}' } + @{ Name = 'the string "true"'; Json = '{ "git": { "allowTrunkPush": "true" } }' } + ) { + $r = Invoke-TrunkPush -Json $Json -Kind FastForward + $r.Code | Should -Be 1 + $r.Output | Should -Match 'a push to main is refused' + } + + It 'reads the setting from the remote sha git passes, not from a local remote-tracking ref' -Skip:($script:NoSh -or $script:NoPwshHere) { + $r = Invoke-TrunkPush -Json '{}' -SecondJson $script:OptIn -Kind FastForward + $r.Code | Should -Be 1 + $r.Output | Should -Match 'a push to main is refused' + } + + It 'refuses a fast-forward when only the working tree switches the setting on' -Skip:($script:NoSh -or $script:NoPwshHere) { + $r = Invoke-TrunkPush -Json '{}' -WorkTreeJson $script:OptIn -Kind FastForward + $r.Code | Should -Be 1 + $r.Output | Should -Match 'a push to main is refused' + } + } + It 'never falls back to Windows PowerShell, which cannot run a gate that needs PowerShell 7' { foreach ($name in 'pre-push', 'graphify-refresh') { $text = Get-Content -LiteralPath (Join-Path $PSScriptRoot '..' 'githooks' $name) -Raw diff --git a/.claude/skills/github-pull-requests/SKILL.md b/.claude/skills/github-pull-requests/SKILL.md index c2d1d10..c42530e 100644 --- a/.claude/skills/github-pull-requests/SKILL.md +++ b/.claude/skills/github-pull-requests/SKILL.md @@ -20,7 +20,7 @@ Use this at the PR preflight and at the end of the loop. `implement`, `refactor` `write-platform-note`, `write-pipeline`, `write-prd`, `write-hypothesis`, `draw-customer-journey` and `write-research-summary` load it before they push, and `fix-bug` through `implement`. After a standalone run, `write-adr`, `update-constitution`, `write-docs`, `add-gate`, `capture-learning` and `write-api-spec` load it too. The push and the pull request run with no prompt. Checkpoint Bravo is the person's review of the pull request on GitHub ([orchestration.md](/.claude/skills/ai-sdlc-handbook/references/orchestration.md)). -A push to `main` or `master` is always refused. The link from the PR to its story is a tracker update, so it joins the +A push to `main` or `master` is refused, unless the repository sets `git.allowTrunkPush` in `aisdlc.json`. The link from the PR to its story is a tracker update, so it joins the one list of tracker updates for the run. The agent opens the pull request as a request for review; it never approves, reviews with approval, or merges one (C09). A hook refuses `gh pr merge` and a merge call through `gh api`. diff --git a/CHANGELOG.md b/CHANGELOG.md index d6ad234..1fae576 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,18 @@ merge is ready to tag, and no second pull request is needed to name the version. Each entry names its issue, as `#42`. +## v1.3.0 - 2026-11-09 + +- A repository can set `git.allowTrunkPush` to `true` in `aisdlc.json` to update `main` or `master` + with a plain push, without a pull request (#16). The pre-push git hook and `Deny-IncompletePush.ps1` + still refuse a delete and a force push of `main`, and the blocking gates still run. The git hook + prints one line that names the setting when it lets a push through. The agent hook now checks every + push in a command, and with the setting it refuses `--no-verify`, a git option before `push`, and a + push to `main` that is chained with other commands. Both hooks read the setting from `aisdlc.json` on + the remote `main` branch, not from the working tree, so an edit that is not on `main` cannot switch + it on. The git hook reads it from the remote tip that git passes during the push, so a local ref + cannot switch it on either. + ## v1.2.0 - 2026-11-07 - A reply, work item or page gives the full link to every pull request, issue or page that the reader diff --git a/apm.lock.yaml b/apm.lock.yaml index e06eead..3543dfa 100644 --- a/apm.lock.yaml +++ b/apm.lock.yaml @@ -3,7 +3,7 @@ apm_version: 0.31.0 dependencies: - repo_url: _local/core name: ai-sdlc-core - version: 1.2.0 + version: 1.3.0 package_type: apm_package deployed_files: - .claude/agents/architect.md @@ -248,8 +248,8 @@ dependencies: .claude/skills/ai-sdlc-gates/assets/actions/ai-sdlc-pr/action.yml: sha256:ee6430668f2f44b6256275645e10f8ab2167b8b6d49990465c5527d1b334abac .claude/skills/ai-sdlc-gates/assets/agents-core.md: sha256:544bb5b78ceaa2ee9eb20203ecdef5d380ecd9b10265cc90d10385d96ed72509 .claude/skills/ai-sdlc-gates/gates.json: sha256:160f09f14ff6591b024f827c879802cf9218b37785d209da6a1323602d6153e5 - .claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1: sha256:dabc8f1c63a903818c4eb46f27eb35769914dc6a4330dc2865a4b756300cb706 - .claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1: sha256:63d1161e057b49953e5704e55a0d6091c4a1d6526d8818b26ff96cd69aad133c + .claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1: sha256:669ce124af8ce839ddd1f463236777d682196ebda0a0175fe362185f033779a4 + .claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1: sha256:59a69f20f41fdcde013a4654e6532730acb1da42408ff2d825747b8c6891e401 .claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.Tests.ps1: sha256:0b95e6de39c913f3bdebc38c6003f12565a225b165c19e73167cf30391ec1a8b .claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.ps1: sha256:fe0a4432d2e5cb3d1830bc178a086c721eea3cbdd744def18586e1d0a9c0a4c1 .claude/skills/ai-sdlc-gates/scripts/Check-AgentHygiene.Tests.ps1: sha256:d4115307bda4dc2794d395bf41cdc4169896f1255e37b5393d71ad3fb6e6495b @@ -331,7 +331,7 @@ dependencies: .claude/skills/ai-sdlc-gates/scripts/gitleaks-rules.json: sha256:6a8a75c5dbcf2378a1c52d046e34ce275aac7ce08a0ae61408d25c18e1e27014 .claude/skills/ai-sdlc-gates/scripts/gitleaks-version.json: sha256:c54acb854f87a1bbdf0e90c670b72b5de3cd37f7e460d82eca599c3c7af23a84 .claude/skills/ai-sdlc-gates/skill-catalog.json: sha256:5537684656bf189a740ba7e9e7050083af05668d00501ce1e9c6909cb8b91203 - .claude/skills/ai-sdlc-gates/sync-manifest.json: sha256:6661094ee0cf38a1bccde9a42b3daa41de7ed7ca115e52f7f18cb7ac62f74f82 + .claude/skills/ai-sdlc-gates/sync-manifest.json: sha256:02e70e5febf8f5c377da8f209aebae347f1dd5c54abdb3c47c65e097b5551cda .claude/skills/ai-sdlc-handbook/SKILL.md: sha256:548e6b9ad79d3d2afa2f5779835988eeb12c49cda08917c4bbaa9ac0486a47a0 .claude/skills/ai-sdlc-handbook/references/README.md: sha256:654afcaa2b81f560eff93dbdef521b09ced06137a1dbb85d70a4e14ef7502248 .claude/skills/ai-sdlc-handbook/references/artifact-graph.md: sha256:d7ddfe20e5ee6565bcf25f9cef0f578da2a4ab1590033d791b8814d4688fd1f0 @@ -343,14 +343,14 @@ dependencies: .claude/skills/ai-sdlc-handbook/references/docs-wiki.md: sha256:2bd6f3a8e64f9db16163519767b822e4ebcffd740e3c5cbff17281ad71d264b7 .claude/skills/ai-sdlc-handbook/references/first-story.md: sha256:831552230cc16386ee01d7c730e288ef19aca5a66fafb00e1dfbbf4e9788caf2 .claude/skills/ai-sdlc-handbook/references/language.md: sha256:e0e32fae9c9fcdd23380d92ec910128254ce248538940541f5789ca78228a023 - .claude/skills/ai-sdlc-handbook/references/layout.md: sha256:a0f86702edb9caf4b52ccda28b3256a17cf30cd008b2200220901699d84896a9 + .claude/skills/ai-sdlc-handbook/references/layout.md: sha256:b37f18f29cdbef58bdf34ed17367b49d5a4a7ccd2e220fc3a1615effd4a8be31 .claude/skills/ai-sdlc-handbook/references/node-templates.md: sha256:8664ad1f85bff48d6230ed22e3f25683f378822e566c2a1454cbb204f4bb8fad - .claude/skills/ai-sdlc-handbook/references/orchestration.md: sha256:05513a9f99f14f281a91da176e9eb39c95f6d939ff33ecca2390c784190546c4 + .claude/skills/ai-sdlc-handbook/references/orchestration.md: sha256:6a1d6b6cc434f2e312994bd77daaa1fef4e007e03777ac63f3ca74edaa0cc485 .claude/skills/ai-sdlc-handbook/references/pattern.md: sha256:7908f80ee28ff1f7cd9f85b22fcf3e455fe74225d33ff201f8fbce1623d672eb .claude/skills/ai-sdlc-handbook/references/personas.md: sha256:7913c3dca6bad93b45624349a12bf976f894eb6ca05c7baa1fa7e3ba802ca69a .claude/skills/ai-sdlc-handbook/references/plan.md: sha256:b632fe2da2369f494ebe2606c4bdc8726eabe3b7615b6ddb0b58fa9b3897a216 .claude/skills/ai-sdlc-handbook/references/skill-usage.md: sha256:9d5414b38d9d6f5bd7dfa9857e8e441bf040b0020f46ac8d3b29b0a476161c61 - .claude/skills/ai-sdlc-hooks/SKILL.md: sha256:68e5154f4e734b93e56b0c2d575b427096dc57c56d217c84a61e27041417ee3f + .claude/skills/ai-sdlc-hooks/SKILL.md: sha256:2b7cddccaff175fd0ffd44524dfddc2b1894f74c1fbdf91d02659573a25b07f8 .claude/skills/ai-sdlc-hooks/gates.json: sha256:dfc8b0003e930167e0fccc366867f423bc0850f3c69477c0e7119aed710bf902 .claude/skills/ai-sdlc-hooks/githooks/git-lfs-hook: sha256:3265310500749e7181a5625517f94e2efa62d48be987ebc9b0396d38e2eeffa6 .claude/skills/ai-sdlc-hooks/githooks/graphify-refresh: sha256:1bc6c7f181aab3a0fdd34bf8907a4884bea2412a56ebdc77a673707e17c046f3 @@ -358,13 +358,13 @@ dependencies: .claude/skills/ai-sdlc-hooks/githooks/post-commit: sha256:4f73a74133b0272725117fa9d962353abb6ed98d9cf5e731cd212fd5ace23271 .claude/skills/ai-sdlc-hooks/githooks/post-merge: sha256:b9f1f47cd3894406451e00c177912baeed66470b0cc18d581a5461330c7c0868 .claude/skills/ai-sdlc-hooks/githooks/post-rewrite: sha256:b7ee1a97b08a9826678829b6c49457fdc4b5ebd53eaf8cdd70f33d26e61c4a79 - .claude/skills/ai-sdlc-hooks/githooks/pre-push: sha256:26e2917ccf0bab590b04ee318a877b22f22245b2d451ee3b23e96b420ed67582 + .claude/skills/ai-sdlc-hooks/githooks/pre-push: sha256:8083ee4f6a7d6c688a73d66ed31428cc602f87d7742b29c1c2210804ac9d04c5 .claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.Tests.ps1: sha256:004bc7f1a68e953c8e657b49e74755b69a14138dfb0db61162ab15420f2b24be .claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.ps1: sha256:b3569de6e27c3dcaffbc082311e6882bd722d440e5e855245da2410033202c7e .claude/skills/ai-sdlc-hooks/scripts/Deny-AgentMerge.Tests.ps1: sha256:4c4e1f6b673671125290d78815c72277665a8a618b55fd589f75f3a36e3b80aa .claude/skills/ai-sdlc-hooks/scripts/Deny-AgentMerge.ps1: sha256:46d1d83e38e75db41b1f370bc6f49f4800f0234765eac63c53e37b5af7000453 - .claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.Tests.ps1: sha256:af45b08394b26ff61cbee86d213615ac1243a53cb28a61277ee79bfdfe075505 - .claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1: sha256:c75085d41a94f60036ff18b9bcaa642c4220c3617925b345a1177b9bc557e2ff + .claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.Tests.ps1: sha256:ea9062bd8b20ad47d488b9838eb5a6c735d73b3148cdcfdd9ebdd60ef708f897 + .claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1: sha256:72d8b73859a105ebbf86838a016b0b61a630e4f28eec5d6d1d86e03fb085c6e4 .claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.Tests.ps1: sha256:1c46576b9633bd12286efa11cc9f7c6c2f982f29859f9a33272f76452888e77b .claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.ps1: sha256:fccaa2da3a0254cc8a7740974f07b1c0fc1e67106ae7191d9710cbf699da1ba7 .claude/skills/ai-sdlc-hooks/scripts/Find-RetiredSkillNames.Tests.ps1: sha256:151c3ee69c88db0d85cf4c94207b04dfc4eabe857e178051044ed4fdcee58f33 @@ -384,7 +384,7 @@ dependencies: .claude/skills/ai-sdlc-hooks/scripts/Invoke-PostEditCheck.ps1: sha256:d60cdd2d341487ded3b9fafe40da49f2adfb1cf8f20cf3f8559ea2e80a5f93c6 .claude/skills/ai-sdlc-hooks/scripts/New-AiSdlcUpdatePullRequest.Tests.ps1: sha256:9c9859767d2534c45e1c72b47f4b5c64aaa16c32c580df3b5d8bc3dce5cdb2c4 .claude/skills/ai-sdlc-hooks/scripts/New-AiSdlcUpdatePullRequest.ps1: sha256:abf763c6cde97dbfc83b25f140e768d66fd9edd3961605b7d058b08a31882875 - .claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1: sha256:a40742b677d79726a6206b7b2dc232bff1df0e663f741af03ced2141016eaba0 + .claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1: sha256:de023a2c91558728f72e02091986607941ef6b7ef0e6df7602d944e20219ae4b .claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.Tests.ps1: sha256:4f2fe5313c8763f8128ad0e0347da1f2dd485393d20dc5e3a5d75e08aa157ad3 .claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.ps1: sha256:0381e47c6b1070c7b230f81f243e25b1a8bfa68b5e8e9ae2ad6e225b0ddb36b6 .claude/skills/ai-sdlc-hooks/scripts/WindowsLongPaths.Tests.ps1: sha256:52f81e945390e61222a2668c8525cb17f17226267e24c52d7792263a07576543 @@ -427,7 +427,7 @@ dependencies: local_path: ./core - repo_url: _local/discovery name: ai-sdlc-discovery - version: 1.2.0 + version: 1.3.0 package_type: apm_package deployed_files: - .claude/skills/draw-customer-journey @@ -444,7 +444,7 @@ dependencies: local_path: ./packs/discovery - repo_url: _local/github name: ai-sdlc-github - version: 1.2.0 + version: 1.3.0 package_type: apm_package deployed_files: - .claude/skills/fix-pr-feedback @@ -493,7 +493,7 @@ dependencies: deployed_file_hashes: .claude/skills/fix-pr-feedback/SKILL.md: sha256:8257f6f84ec21ee12016fc27e0f3508bcf16cf59b9dd91f35ace0682fbaa3c2c .claude/skills/github-actions-conventions/SKILL.md: sha256:0ebf2363a0ff9aa3b8fdecef9d9ffb817cf3aba483d8efae0611b2c5c3b65d87 - .claude/skills/github-pull-requests/SKILL.md: sha256:36ef2c7fff5c21ee87893b1c066e1b150c3cbbf3cecabfbc13f81ec3d89cf68b + .claude/skills/github-pull-requests/SKILL.md: sha256:1e5cba7164be10f0cacc91b2ed3abc64dc6a91fc447d0b079d55f174a0432f5c .claude/skills/github-pull-requests/gates.json: sha256:ac2029c1077f9ff7ba5892b815e009e0470bd924c8b361321bfd2bd1a26f5f89 .claude/skills/github-pull-requests/references/delivery-metrics.md: sha256:e68cda7b7772b71f456a3c48f316ae80ae8fbd7c63275a1f9780234376c68ca4 .claude/skills/github-pull-requests/scripts/Get-PrMetrics.Tests.ps1: sha256:1a0f480b221481d73bf6d54d463dd0251b89d88b959b545d7e7d3b420e090d2c @@ -534,7 +534,7 @@ dependencies: local_path: ./packs/github - repo_url: _local/github-issues name: ai-sdlc-github-issues - version: 1.2.0 + version: 1.3.0 package_type: apm_package deployed_files: - .claude/agents/draft-reviewer.md @@ -804,7 +804,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:dabc8f1c63a903818c4eb46f27eb35769914dc6a4330dc2865a4b756300cb706 + content_hash: sha256:669ce124af8ce839ddd1f463236777d682196ebda0a0175fe362185f033779a4 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 @@ -813,7 +813,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:63d1161e057b49953e5704e55a0d6091c4a1d6526d8818b26ff96cd69aad133c + content_hash: sha256:59a69f20f41fdcde013a4654e6532730acb1da42408ff2d825747b8c6891e401 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.Tests.ps1 @@ -1551,7 +1551,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:6661094ee0cf38a1bccde9a42b3daa41de7ed7ca115e52f7f18cb7ac62f74f82 + content_hash: sha256:02e70e5febf8f5c377da8f209aebae347f1dd5c54abdb3c47c65e097b5551cda - kind: project-relative target: claude value: .claude/skills/ai-sdlc-handbook @@ -1668,7 +1668,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:a0f86702edb9caf4b52ccda28b3256a17cf30cd008b2200220901699d84896a9 + content_hash: sha256:b37f18f29cdbef58bdf34ed17367b49d5a4a7ccd2e220fc3a1615effd4a8be31 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-handbook/references/node-templates.md @@ -1686,7 +1686,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:05513a9f99f14f281a91da176e9eb39c95f6d939ff33ecca2390c784190546c4 + content_hash: sha256:6a1d6b6cc434f2e312994bd77daaa1fef4e007e03777ac63f3ca74edaa0cc485 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-handbook/references/pattern.md @@ -1740,7 +1740,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:68e5154f4e734b93e56b0c2d575b427096dc57c56d217c84a61e27041417ee3f + content_hash: sha256:2b7cddccaff175fd0ffd44524dfddc2b1894f74c1fbdf91d02659573a25b07f8 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-hooks/gates.json @@ -1812,7 +1812,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:26e2917ccf0bab590b04ee318a877b22f22245b2d451ee3b23e96b420ed67582 + content_hash: sha256:8083ee4f6a7d6c688a73d66ed31428cc602f87d7742b29c1c2210804ac9d04c5 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.Tests.ps1 @@ -1857,7 +1857,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:af45b08394b26ff61cbee86d213615ac1243a53cb28a61277ee79bfdfe075505 + content_hash: sha256:ea9062bd8b20ad47d488b9838eb5a6c735d73b3148cdcfdd9ebdd60ef708f897 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 @@ -1866,7 +1866,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:c75085d41a94f60036ff18b9bcaa642c4220c3617925b345a1177b9bc557e2ff + content_hash: sha256:72d8b73859a105ebbf86838a016b0b61a630e4f28eec5d6d1d86e03fb085c6e4 - kind: project-relative target: claude value: .claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.Tests.ps1 @@ -2046,7 +2046,7 @@ deployments: owners: - ./core active_owner: ./core - content_hash: sha256:a40742b677d79726a6206b7b2dc232bff1df0e663f741af03ced2141016eaba0 + content_hash: sha256:de023a2c91558728f72e02091986607941ef6b7ef0e6df7602d944e20219ae4b - kind: project-relative target: claude value: .claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.Tests.ps1 @@ -2325,7 +2325,7 @@ deployments: owners: - ./packs/github active_owner: ./packs/github - content_hash: sha256:36ef2c7fff5c21ee87893b1c066e1b150c3cbbf3cecabfbc13f81ec3d89cf68b + content_hash: sha256:1e5cba7164be10f0cacc91b2ed3abc64dc6a91fc447d0b079d55f174a0432f5c - kind: project-relative target: claude value: .claude/skills/github-pull-requests/gates.json diff --git a/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 b/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 index 2ad1609..e5689b5 100644 --- a/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 +++ b/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1 @@ -133,6 +133,34 @@ Describe 'AiSdlcConfig' { @($config.dotnet.Keys) | Should -Not -Contain 'docGeneratorEndpoint' } + It 'refuses a push to the trunk unless git.allowTrunkPush is the JSON value true' -ForEach @( + @{ Json = $null; Expected = $false } + @{ Json = '{}'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": false } }'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": "true" } }'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": 1 } }'; Expected = $false } + @{ Json = '{ "git": { "allowTrunkPush": true } }'; Expected = $true } + ) { + Test-AiSdlcTrunkPushAllowed -RepoRoot (New-ConfigRepo -Json $Json -Git) | Should -Be $Expected + } + + It 'reads git.allowTrunkPush from the commit that -Ref names, not from the working tree' { + $root = New-ConfigRepo -Json '{ "git": { "allowTrunkPush": true } }' -Git + git -C $root config user.email probe@example.invalid + git -C $root config user.name probe + git -C $root add aisdlc.json + git -C $root commit --quiet -m on --no-verify + git -C $root update-ref refs/remotes/origin/main HEAD + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value '{}' + git -C $root commit --quiet -am off --no-verify + git -C $root update-ref refs/remotes/origin/master HEAD + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value '{ "git": { "allowTrunkPush": true } }' + + Test-AiSdlcTrunkPushAllowed -RepoRoot $root -Ref 'refs/remotes/origin/main' | Should -BeTrue + Test-AiSdlcTrunkPushAllowed -RepoRoot $root -Ref 'refs/remotes/origin/master' | Should -BeFalse + Test-AiSdlcTrunkPushAllowed -RepoRoot $root -Ref 'refs/remotes/origin/missing' | Should -BeFalse + } + It 'reads the other repositories whose issues a pull request may name from tracker.acceptedRepositories' { $config = Get-AiSdlcConfig -RepoRoot (New-ConfigRepo -Json '{ "tracker": { "acceptedRepositories": ["contoso/ai-sdlc"] } }') diff --git a/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 b/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 index afaad1b..3d3449e 100644 --- a/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 +++ b/core/.apm/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1 @@ -118,6 +118,9 @@ $script:Defaults = [ordered]@{ secretScan = [ordered]@{ allowPaths = @() } + git = [ordered]@{ + allowTrunkPush = $false + } tracker = [ordered]@{ repository = '' keyPattern = '' @@ -270,6 +273,31 @@ function Test-AiSdlcPack { return @($config.packs) -contains $Name } +function Test-AiSdlcTrunkPushAllowed { + # True only when aisdlc.json sets git.allowTrunkPush to the JSON value true. A string such as "true" + # does not count, so a typo never opens the trunk. The push hooks still refuse a delete or a force push. + # With -Ref, the file is read from that commit, not from the working tree. The push hooks pass the + # remote-tracking ref of the branch they guard, such as refs/remotes/origin/main, so a change in the + # working tree cannot switch the setting on: it counts only once it is on the remote main branch. + # A missing ref, a missing file or a file that is not valid JSON counts as false. + [CmdletBinding()] + param([string]$RepoRoot, [string]$Ref) + + if ($Ref) { + if (-not $RepoRoot) { $RepoRoot = Find-AiSdlcRepoRoot } + $text = git -C $RepoRoot show "$($Ref):$($script:ConfigFileName)" 2>$null + if ($LASTEXITCODE -ne 0 -or -not $text) { return $false } + try { $values = ($text -join "`n") | ConvertFrom-Json -AsHashtable } catch { return $false } + $section = if ($values -is [System.Collections.IDictionary]) { $values['git'] } else { $null } + $value = if ($section -is [System.Collections.IDictionary]) { $section['allowTrunkPush'] } else { $null } + return ($value -is [bool]) -and $value + } + + $config = Get-AiSdlcConfig -RepoRoot $RepoRoot + $value = $config.git['allowTrunkPush'] + return ($value -is [bool]) -and $value +} + function Get-AiSdlcStoryPattern { [CmdletBinding()] param([string]$RepoRoot) @@ -383,4 +411,4 @@ function Get-AiSdlcPackage { } } -Export-ModuleMember -Function Find-AiSdlcRepoRoot, Get-AiSdlcConfig, Get-AiSdlcPath, Test-AiSdlcPack, Get-AiSdlcStoryPattern, Get-AiSdlcBranchStoryKey, Get-AiSdlcStoryKey, ConvertTo-AiSdlcStoryKey, Get-AiSdlcPackage +Export-ModuleMember -Function Find-AiSdlcRepoRoot, Get-AiSdlcConfig, Get-AiSdlcPath, Test-AiSdlcPack, Test-AiSdlcTrunkPushAllowed,Get-AiSdlcStoryPattern, Get-AiSdlcBranchStoryKey, Get-AiSdlcStoryKey, ConvertTo-AiSdlcStoryKey, Get-AiSdlcPackage diff --git a/core/.apm/skills/ai-sdlc-gates/sync-manifest.json b/core/.apm/skills/ai-sdlc-gates/sync-manifest.json index fcfc9ec..fb4e8ae 100644 --- a/core/.apm/skills/ai-sdlc-gates/sync-manifest.json +++ b/core/.apm/skills/ai-sdlc-gates/sync-manifest.json @@ -21,8 +21,8 @@ ".claude/skills/ai-sdlc-gates/assets/actions/ai-sdlc-pr/action.yml": "f80ed84d896859668d74cd47bf327a714d383f8a", ".claude/skills/ai-sdlc-gates/assets/agents-core.md": "f7e45643bf0f5447d480b588784b2c599cc7f320", ".claude/skills/ai-sdlc-gates/gates.json": "cf6f5a2d284f0b22f0f4a89292037caed3e53c19", - ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1": "afaad1b3853d71d4f1016e2f9f9df4a1ffdbc03d", - ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1": "2ad1609653980ca3bada51c7cd470e4ba6ffcbe9", + ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.psm1": "3d3449e30e25d842f355ce74a2150775ab8b3e0d", + ".claude/skills/ai-sdlc-gates/scripts/AiSdlcConfig.Tests.ps1": "e5689b5deec495e74dc991786596903d70283263", ".claude/skills/ai-sdlc-gates/scripts/apm-version.json": "fbbd8a51e60e3a7e653a35bc9b959f2bf332ab74", ".claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.ps1": "64c6d651e638b0fe15d20cde02418e8021a5cdfc", ".claude/skills/ai-sdlc-gates/scripts/Build-DocsIndex.Tests.ps1": "d9ac6b357e1fc4dbf7deacce04ec365b8515e627", @@ -114,9 +114,9 @@ ".claude/skills/ai-sdlc-handbook/references/docs-wiki.md": "038477c9b704a5b545decfe5e3f3f9970d8d1f9d", ".claude/skills/ai-sdlc-handbook/references/first-story.md": "be899eb395dafb2d704b8f0cb46bac1fb5e593c8", ".claude/skills/ai-sdlc-handbook/references/language.md": "ca703edeb9e07e28e32a39fbcc9116b64db444d8", - ".claude/skills/ai-sdlc-handbook/references/layout.md": "eaabf81a917d8b826f26318bcef7934fbbecbdfa", + ".claude/skills/ai-sdlc-handbook/references/layout.md": "b6683bbb25c614a5d0a3f8283d4e46bfa110bcd2", ".claude/skills/ai-sdlc-handbook/references/node-templates.md": "580c9c92c470a6abbaa0145cc3060ff1a11d247c", - ".claude/skills/ai-sdlc-handbook/references/orchestration.md": "5b6c8c38a5d17ca59cc38794a7750355c508d262", + ".claude/skills/ai-sdlc-handbook/references/orchestration.md": "22373827d6ecd454d93cfb652f76c5204f3215e2", ".claude/skills/ai-sdlc-handbook/references/pattern.md": "f0f2a8cf3dac267cda0fd3783d86bddba5afc7c0", ".claude/skills/ai-sdlc-handbook/references/personas.md": "083c57f3e4e88519a9d42d7e51559c5165faacff", ".claude/skills/ai-sdlc-handbook/references/plan.md": "8c6fde8e741106f8fe625b8b9a4e98b6d5ba912f", @@ -130,13 +130,13 @@ ".claude/skills/ai-sdlc-hooks/githooks/post-commit": "f18058f9a38efc164e13801d61bb05329a056109", ".claude/skills/ai-sdlc-hooks/githooks/post-merge": "bb373f33abd09294cda55d7fc4499f32672eb372", ".claude/skills/ai-sdlc-hooks/githooks/post-rewrite": "e4c187dffeeaba569901f3424fae476b17c14927", - ".claude/skills/ai-sdlc-hooks/githooks/pre-push": "ba5d1df1483ae80b7d6a009aafdb820f3629a2e3", + ".claude/skills/ai-sdlc-hooks/githooks/pre-push": "490c81fd333d26cf9f09877fecb369d531710d66", ".claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.ps1": "52ffd325d13cd403e7bfea0de8098c530c129b65", ".claude/skills/ai-sdlc-hooks/scripts/AiSdlcSetup.Tests.ps1": "ef5b30f0811181462d354a61d3e64c19f0c305e2", ".claude/skills/ai-sdlc-hooks/scripts/Deny-AgentMerge.ps1": "535543b86886fcc582912ded5373b0bd3d756356", ".claude/skills/ai-sdlc-hooks/scripts/Deny-AgentMerge.Tests.ps1": "c8dfc8a4f31b6ec89a6d680e936e3563afa62bcb", - ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1": "e3a58c32a38844707aa0b2f4c14961af3ed2c870", - ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.Tests.ps1": "c7da2e83782aa42478fc3b416af5926b8bdd376c", + ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1": "117d527a075a78017b0a199703d84a6807797573", + ".claude/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.Tests.ps1": "73947a2a69aecf0e73a11640a0c21d081d43c4d8", ".claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.ps1": "ca703a9e057633e39af161d9e43a042429813acc", ".claude/skills/ai-sdlc-hooks/scripts/Deny-SharedFileEdit.Tests.ps1": "d15bf28d4191b5e2bca8ce59621ad03a1ef6198c", ".claude/skills/ai-sdlc-hooks/scripts/Find-RetiredSkillNames.ps1": "da6172d61799df3f37c5f70a103cddddb5517cb0", @@ -156,12 +156,12 @@ ".claude/skills/ai-sdlc-hooks/scripts/Invoke-PostEditCheck.Tests.ps1": "3429e18735428ab387ab58ae276d4195b32e7999", ".claude/skills/ai-sdlc-hooks/scripts/New-AiSdlcUpdatePullRequest.ps1": "b54f90deb751d0b0dbf8ee8fbc3f39b3300b3b3d", ".claude/skills/ai-sdlc-hooks/scripts/New-AiSdlcUpdatePullRequest.Tests.ps1": "77e4b627da13ea9887f3918f66bc5b33dfd91c56", - ".claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1": "10a332050267c363ee019fc1911495b8bc3bfb00", + ".claude/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1": "718248ded3ba695b29d4d7eb59e8f09130cf918c", ".claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.ps1": "56abaf1de1c17bea94c26e128f8c41e275a74f87", ".claude/skills/ai-sdlc-hooks/scripts/Update-AiSdlc.Tests.ps1": "8db305e438bc7ee404eb5106eacbe5581738f148", ".claude/skills/ai-sdlc-hooks/scripts/WindowsLongPaths.ps1": "fa3d7f509b6f9a1a935178c1eaf0d1566b7afead", ".claude/skills/ai-sdlc-hooks/scripts/WindowsLongPaths.Tests.ps1": "21a220ca2ccc65a56ec427a6e74c5a5dbfbfd4ef", - ".claude/skills/ai-sdlc-hooks/SKILL.md": "0bb9a972fbf928e359cbdfa24cc5781d43d0ab54", + ".claude/skills/ai-sdlc-hooks/SKILL.md": "a5b99bcdf6bf93721136ee3a3e7075632fdf068d", ".claude/skills/capture-learning/SKILL.md": "32300ce3189109b0d717f975623e8d7ccec1c821", ".claude/skills/convene-personas/SKILL.md": "449708a02206bec35a83d2f48ca6366c8b78e3a2", ".claude/skills/docs-wiki-conventions/SKILL.md": "0679512fa39815b63da6373ff29bb6eadc52ff24", @@ -399,7 +399,7 @@ ".claude/skills/github-pull-requests/scripts/Update-PullRequest.Tests.ps1": "f63886f04b76fdddda7b23dd886586fb04317194", ".claude/skills/github-pull-requests/scripts/Wait-WorkflowRun.ps1": "27fd4ca10468eb3fecd2580f2f1e493fd115ae9d", ".claude/skills/github-pull-requests/scripts/Wait-WorkflowRun.Tests.ps1": "3cf5da073805761e52b2af1ea7399e71584ee342", - ".claude/skills/github-pull-requests/SKILL.md": "c2d1d10ae6721687e84a21c5bee71d1332387b76", + ".claude/skills/github-pull-requests/SKILL.md": "c42530e8588f05881da325e74a57eb79ae80fe1c", ".claude/skills/write-pipeline/examples/infrastructure/pr.yml": "b3758e9bae5a9c5dec006af329218e2e23244a78", ".claude/skills/write-pipeline/examples/infrastructure/release.yml": "967c35ded6d96c0b881b89a6ef9599960f001719", ".claude/skills/write-pipeline/examples/web-app/build-and-test.yml": "f9155849e3229580d5e91fc8ea257275807cf220", diff --git a/core/.apm/skills/ai-sdlc-handbook/references/layout.md b/core/.apm/skills/ai-sdlc-handbook/references/layout.md index eaabf81..b6683bb 100644 --- a/core/.apm/skills/ai-sdlc-handbook/references/layout.md +++ b/core/.apm/skills/ai-sdlc-handbook/references/layout.md @@ -129,6 +129,7 @@ installs. `AiSdlcConfig.psm1` takes each dependency whose path ends in `packs/ . # `git lfs pre-push` needs the same lines at the end, so they are read once and kept. +# The setting is read from aisdlc.json in the commit that is the tip of the remote branch now. git +# learns that commit from the remote while it pushes and passes it on stdin as , so neither +# an edit in the working tree nor a local ref can switch the setting on. A new branch (zero sha) or a tip +# that is not in this clone counts as off. $1 is that remote sha. +trunk_push_allowed() { + is_zero_sha "$1" && return 1 + git cat-file -e "$1^{commit}" 2>/dev/null || return 1 + command -v pwsh >/dev/null 2>&1 || return 1 + module="$(dirname "$0")/../../ai-sdlc-gates/scripts/AiSdlcConfig.psm1" + [ -f "$module" ] || return 1 + AISDLC_CONFIG_MODULE="$module" AISDLC_REPO_ROOT="$(git rev-parse --show-toplevel)" AISDLC_REF="$1" pwsh -NoProfile -NonInteractive -Command 'try { Import-Module $env:AISDLC_CONFIG_MODULE -Force; if (Test-AiSdlcTrunkPushAllowed -RepoRoot $env:AISDLC_REPO_ROOT -Ref $env:AISDLC_REF) { exit 0 } } catch { }; exit 1' >/dev/null 2>&1 +} + +is_zero_sha() { + case "$1" in + *[!0]*) return 1 ;; + *) return 0 ;; + esac +} + refs=$(cat) while read -r local_ref local_sha remote_ref remote_sha; do case "$remote_ref" in refs/heads/main|refs/heads/master) - echo "pre-push: a push to ${remote_ref#refs/heads/} is refused. A change reaches main only through a pull request that a person reviews and merges. Push the story branch and open or update its pull request instead." >&2 - exit 1 + branch=${remote_ref#refs/heads/} + if ! trunk_push_allowed "$remote_sha"; then + echo "pre-push: a push to $branch is refused. A change reaches main only through a pull request that a person reviews and merges. Push the story branch and open or update its pull request instead." >&2 + if ! is_zero_sha "$remote_sha" && ! git cat-file -e "$remote_sha^{commit}" 2>/dev/null; then + echo "pre-push: the remote tip of $branch is not in this clone, so git.allowTrunkPush cannot be read from it. If the setting is on $branch, fetch first, then push again." >&2 + fi + exit 1 + fi + if is_zero_sha "$local_sha"; then + echo "pre-push: git.allowTrunkPush in aisdlc.json allows an update of $branch, not a delete. The delete is refused." >&2 + exit 1 + fi + if ! git merge-base --is-ancestor "$remote_sha" "$local_sha"; then + echo "pre-push: git.allowTrunkPush in aisdlc.json allows a fast-forward of $branch, not a force push. The push is refused." >&2 + exit 1 + fi + echo "pre-push: git.allowTrunkPush in aisdlc.json allows this fast-forward of $branch. The blocking gates still run." ;; esac done <&1 | Out-String + return [pscustomobject]@{ Output = $out; ExitCode = $LASTEXITCODE } + } + } + + It 'allows when the change is complete' -ForEach @( + @{ Command = 'git push origin main' } + @{ Command = 'git push' } + @{ Command = 'git push -u origin HEAD:main' } + @{ Command = 'git push origin master' } + @{ Command = 'git -C C:/repo push origin main' } + @{ Command = 'git push origin main 2>&1' } + @{ Command = 'git push --set-upstream --follow-tags origin main' } + @{ Command = 'git push -uq origin main' } + ) { + $r = Invoke-OptedInHook -Command $Command + $r.Output | Should -Not -Match 'permissionDecision' + } + + It 'refuses , which may delete or rewrite the trunk' -ForEach @( + @{ Command = 'git push --force origin main' } + @{ Command = 'git push -f origin main' } + @{ Command = 'git push --force-with-lease origin main' } + @{ Command = 'git push origin +main' } + @{ Command = 'git push origin :main' } + @{ Command = 'git push --delete origin main' } + @{ Command = 'git push --mirror origin' } + @{ Command = 'git push --all origin' } + @{ Command = 'git push --no-verify origin main' } + @{ Command = 'git -c remote.origin.mirror=true push origin main' } + @{ Command = 'git push origin main && git push -f origin main' } + @{ Command = 'git push origin main; git push origin :main' } + @{ Command = 'git push origin main | tee out.txt' } + @{ Command = 'git push $(echo origin) main' } + @{ Command = 'git push --dele origin main' } + @{ Command = 'git push --no-verif origin main' } + @{ Command = 'git push --forc origin main' } + @{ Command = 'git push --mirr origin' } + @{ Command = 'git push -uf origin main' } + @{ Command = 'git push --repo=other main' } + @{ Command = 'git push origin main \--force' } + @{ Command = 'git push origin main ${X}' } + @{ Command = 'git push origin main {--force,}' } + ) { + $r = Invoke-OptedInHook -Command $Command + $r.Output | Should -Match '"permissionDecision":"deny"' + $r.Output | Should -Match 'allowTrunkPush' + } + + It 'refuses a trunk push when only the working tree switches the setting on' { + $payload = New-Payload -Command 'git push origin main' + $out = & $script:ScriptUnderTest -Payload $payload -CheckScript $script:CleanCheck -CurrentBranch 'main' -ReceiptRoot $script:Receipts -HeadTree $script:GreenTree -RepoRoot $script:LocalOnly *>&1 | Out-String + $out | Should -Match '"permissionDecision":"deny"' + $out | Should -Match 'refs/remotes/origin/main' + } + + It 'still runs the completeness check on a trunk push' { + $r = Invoke-OptedInHook -Command 'git push origin main' -CheckScript $script:GapCheck + $r.Output | Should -Match '"permissionDecision":"deny"' + $r.Output | Should -Not -Match 'main or master is refused' + } + } + Context 'a push whose commit has no green pre-flight, which Checkpoint Bravo relies on' { It 'refuses a complete change whose commit has no receipt, and names wrap-up' { $r = Invoke-Hook -Payload (New-Payload -Command 'git push -u origin feature/1-x') -CheckScript $script:CleanCheck -Tree $script:UncheckedTree diff --git a/core/.apm/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 b/core/.apm/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 index e3a58c3..117d527 100644 --- a/core/.apm/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 +++ b/core/.apm/skills/ai-sdlc-hooks/scripts/Deny-IncompletePush.ps1 @@ -26,7 +26,17 @@ script. A payload this script cannot read as a git push allows for the same reason; the pre-push git hook stays the backstop that no configuration can miss. Before the completeness check it refuses any push to main or master: a named target, a quoted one, - HEAD while on main, a bare push while on main, and --all, --mirror or --branches. A permission rule + HEAD while on main, a bare push while on main, and --all, --mirror or --branches. A repository that + sets git.allowTrunkPush to true in aisdlc.json may update main or master directly, but only in a plain + push that is the whole command: a +refspec, a delete refspec (:main), any push option outside a short + allow list (-u, -q, -v, --set-upstream, --quiet, --verbose, --progress, --no-progress, --porcelain, + --atomic, --follow-tags, each written in full), a git option before push other than -C, and a push + chained with any other command stay refused. So --force, --delete, --mirror and --no-verify are refused, + also when shortened. The setting counts only when it is in aisdlc.json on the remote-tracking ref of + that main branch (refs/remotes//main), so an edit in the working tree cannot switch it on. + That ref lives in the clone, so it is a first check only. The pre-push git hook is the backstop: it + reads the setting from the remote tip that git passes during the push, which no local ref can change. The rest of the checks still run on such a + push. A permission rule cannot see which branch a bare push targets, so this hook is the guard for Claude Code and Copilot CLI alike. It is registered for the Bash tool and for the PowerShell tool, because a repository that allows git push in PowerShell would otherwise push past it. The PowerShell payload has the same @@ -201,17 +211,77 @@ function Test-IsGitPush { function Test-TargetsProtectedBranch { param([string]$CommandLine, [string]$Branch) + # Every push in the command counts, so a second push in a chain cannot reach main unseen. + foreach ($push in [regex]::Matches($CommandLine, "$gitPrefix\s+push(?[^;&|]*)")) { + $all = @($push.Groups['rest'].Value.Trim() -split '\s+' | Where-Object { $_ } | ForEach-Object { $_ -replace '[''"]', '' }) + if (@($all | Where-Object { $_ -in @('--all', '--mirror', '--branches') }).Count -gt 0) { return $true } + $words = @($all | Where-Object { -not $_.StartsWith('-') }) + $refspecs = @($words | Select-Object -Skip 1) + if ($refspecs.Count -eq 0) { + if ($Branch -in $protectedBranches) { return $true } + continue + } + foreach ($refspec in $refspecs) { + $target = ($refspec.TrimStart('+') -split ':')[-1] -replace '^refs/heads/', '' + if ($target -in $protectedBranches) { return $true } + if ($target -eq 'HEAD' -and $Branch -in $protectedBranches) { return $true } + } + } + return $false +} + +function Get-TrunkTrackingRef { + # The remote-tracking ref of the main branch a plain push updates, such as refs/remotes/origin/main. + # git.allowTrunkPush is read from that commit, so only a setting that is already on the remote counts. + # Call it only after Test-RewritesBranch passed, so the command is one plain push. + param([string]$CommandLine, [string]$Branch, [string]$Root) + $push = [regex]::Match($CommandLine, "$gitPrefix\s+push(?[^;&|]*)") - if (-not $push.Success) { return $false } $all = @($push.Groups['rest'].Value.Trim() -split '\s+' | Where-Object { $_ } | ForEach-Object { $_ -replace '[''"]', '' }) - if (@($all | Where-Object { $_ -in @('--all', '--mirror', '--branches') }).Count -gt 0) { return $true } - $words = @($all | Where-Object { -not $_.StartsWith('-') }) - $refspecs = @($words | Select-Object -Skip 1) - if ($refspecs.Count -eq 0) { return $Branch -in $protectedBranches } + $words = @($all | Where-Object { $_ -ne '2>&1' -and -not $_.StartsWith('-') }) + $remote = if ($words.Count -gt 0) { $words[0] } else { '' } + if (-not $remote -and $Branch) { $remote = try { [string](git -C $Root config "branch.$Branch.remote" 2>$null) } catch { '' } } + if (-not $remote) { $remote = 'origin' } + $target = $Branch + foreach ($refspec in @($words | Select-Object -Skip 1)) { + $name = ($refspec -split ':')[-1] -replace '^refs/heads/', '' + if ($name -eq 'HEAD') { $name = $Branch } + if ($name -in $protectedBranches) { $target = $name; break } + } + return "refs/remotes/$($remote.Trim())/$target" +} + +function Test-RewritesBranch { + # True when the push may delete or rewrite a branch, or when the hook cannot judge it: a force option, + # a +refspec, a delete, a push of every branch, --no-verify (which skips the pre-push git hook), a git + # option before push other than -C, or a command that holds anything besides this one push. Only a + # plain push that is the whole command may update the trunk under git.allowTrunkPush. + param([string]$CommandLine) + + $single = $CommandLine.Trim() -replace '\s+2>&1(?:\s*\|\s*(?i:Out-String))?$', '' + if ($single -match '[;&|<>\r\n`]|\$\(') { return $true } + if ([regex]::Matches($single, '(?:^|\s)' + $gitName + '(?:\s|$)').Count -ne 1) { return $true } + $push = [regex]::Match($single, '^&?\s*' + $gitName + '(?:\s+-C\s+(?:"[^"]*"|''[^'']*''|\S+))?\s+push(?(?:\s.*)?)$') + if (-not $push.Success) { return $true } + $all = @($push.Groups['rest'].Value.Trim() -split '\s+' | Where-Object { $_ } | ForEach-Object { $_ -replace '[''"]', '' }) + # Options pass only from a short allow list, written in full. git accepts a shortened long option, + # such as --dele for --delete, so a deny list of names would miss one. + $safeLong = @('--set-upstream', '--quiet', '--verbose', '--progress', '--no-progress', '--porcelain', '--atomic', '--follow-tags') + foreach ($word in $all) { + # A remote or refspec holds only plain name characters. A backslash, $ or brace could hide an + # option from this check (\--force reaches git as --force), so such a word is refused. + if (-not $word.StartsWith('-')) { + if ($word -notmatch '^[A-Za-z0-9._/:@^~+-]+$') { return $true } + continue + } + if ($word.StartsWith('--')) { + if ($word -notin $safeLong) { return $true } + } + elseif ($word -notmatch '^-[uqv]+$') { return $true } + } + $refspecs = @($all | Where-Object { -not $_.StartsWith('-') } | Select-Object -Skip 1) foreach ($refspec in $refspecs) { - $target = ($refspec.TrimStart('+') -split ':')[-1] -replace '^refs/heads/', '' - if ($target -in $protectedBranches) { return $true } - if ($target -eq 'HEAD' -and $Branch -in $protectedBranches) { return $true } + if ($refspec.StartsWith('+') -or $refspec.StartsWith(':')) { return $true } } return $false } @@ -296,10 +366,26 @@ if (-not $PSBoundParameters.ContainsKey('CurrentBranch')) { $CurrentBranch = try { [string](git -C $targetRoot rev-parse --abbrev-ref HEAD 2>$null) } catch { '' } } if (Test-TargetsProtectedBranch -CommandLine $command -Branch ([string]$CurrentBranch).Trim()) { - Write-Deny ('A push to main or master is refused. A change reaches main only through a pull request that a ' + - 'person reviews and merges (.claude/skills/ai-sdlc-handbook/references/orchestration.md). Push the story ' + - 'branch and open or update its pull request instead.') - exit 0 + $trunkPushAllowed = $targetInstalls -and (Test-AiSdlcTrunkPushAllowed -RepoRoot $targetRoot) + if (-not $trunkPushAllowed) { + Write-Deny ('A push to main or master is refused. A change reaches main only through a pull request that a ' + + 'person reviews and merges (.claude/skills/ai-sdlc-handbook/references/orchestration.md). Push the story ' + + 'branch and open or update its pull request instead.') + exit 0 + } + if (Test-RewritesBranch -CommandLine $command) { + Write-Deny ('git.allowTrunkPush in aisdlc.json allows a plain push to main or master, not a force push, a ' + + 'delete, a push of every branch, --no-verify, a git option before push, or a push chained with other ' + + 'commands. Run the push of main on its own, as git push [remote] [branch].') + exit 0 + } + $trunkRef = Get-TrunkTrackingRef -CommandLine $command -Branch ([string]$CurrentBranch).Trim() -Root $targetRoot + if (-not (Test-AiSdlcTrunkPushAllowed -RepoRoot $targetRoot -Ref $trunkRef)) { + Write-Deny ("git.allowTrunkPush counts only when it is on the remote main branch ($trunkRef), not in the " + + 'working tree alone. Get the setting onto main through a pull request first, or fetch when it is ' + + 'already there.') + exit 0 + } } if (-not $targetInstalls) { exit 0 } diff --git a/core/.apm/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 b/core/.apm/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 index 10a3320..718248d 100644 --- a/core/.apm/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 +++ b/core/.apm/skills/ai-sdlc-hooks/scripts/PrePushHook.Tests.ps1 @@ -68,6 +68,87 @@ Describe 'The pre-push git hook' { $r.Code | Should -Be 0 -Because $r.Output } + Context 'a repository that sets git.allowTrunkPush, which may fast-forward its trunk' { + BeforeAll { + $script:NoPwshHere = -not (Get-Command pwsh -ErrorAction SilentlyContinue) + + # A repository with two commits on main, so a push can be a fast-forward or a rewrite. + function Invoke-TrunkPush { + param([string]$Json, [ValidateSet('FastForward', 'Rewrite', 'Delete', 'UnknownRemote')][string]$Kind, [string]$WorkTreeJson, [string]$SecondJson) + $root = Join-Path ([System.IO.Path]::GetTempPath()) ('pre-push-' + [guid]::NewGuid().ToString('n')) + New-Item -ItemType Directory -Path $root | Out-Null + git -C $root init --quiet --initial-branch main + git -C $root config user.email probe@example.invalid + git -C $root config user.name probe + if ($Json) { + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value $Json + git -C $root add aisdlc.json + } + git -C $root commit --quiet --allow-empty -m one --no-verify + $first = (git -C $root rev-parse HEAD).Trim() + if ($SecondJson) { + Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value $SecondJson + git -C $root add aisdlc.json + } + git -C $root commit --quiet --allow-empty -m two --no-verify + $second = (git -C $root rev-parse HEAD).Trim() + # The remote-tracking ref may point anywhere; the hook must read the remote sha from stdin. + git -C $root update-ref refs/remotes/origin/main $(if ($SecondJson) { $second } else { $first }) + if ($WorkTreeJson) { Set-Content -LiteralPath (Join-Path $root 'aisdlc.json') -Value $WorkTreeJson } + $line = switch ($Kind) { + 'FastForward' { "refs/heads/main $second refs/heads/main $first" } + 'Rewrite' { "refs/heads/main $first refs/heads/main $second" } + 'Delete' { "(delete) $script:Zero refs/heads/main $second" } + 'UnknownRemote' { "refs/heads/main $second refs/heads/main $script:Sha" } + } + Push-Location $root + try { + $output = "$line`n" | & $script:Sh $script:Hook origin 'https://example.invalid/repo' 2>&1 | Out-String + [pscustomobject]@{ Code = $LASTEXITCODE; Output = $output } + } + finally { Pop-Location; Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue } + } + $script:OptIn = '{ "git": { "allowTrunkPush": true } }' + } + + It 'allows a fast-forward of main and says which setting allowed it' -Skip:($script:NoSh -or $script:NoPwshHere) { + $r = Invoke-TrunkPush -Json $script:OptIn -Kind FastForward + $r.Code | Should -Be 0 -Because $r.Output + $r.Output | Should -Match 'git.allowTrunkPush in aisdlc.json allows this fast-forward of main' + } + + It 'refuses a of main' -Skip:($script:NoSh -or $script:NoPwshHere) -ForEach @( + @{ Kind = 'Rewrite'; Reason = 'not a force push' } + @{ Kind = 'Delete'; Reason = 'not a delete' } + @{ Kind = 'UnknownRemote'; Reason = 'Fetch first' } + ) { + $r = Invoke-TrunkPush -Json $script:OptIn -Kind $Kind + $r.Code | Should -Be 1 + $r.Output | Should -Match $Reason + } + + It 'still refuses a fast-forward when the setting is ' -Skip:($script:NoSh -or $script:NoPwshHere) -ForEach @( + @{ Name = 'missing'; Json = '{}' } + @{ Name = 'the string "true"'; Json = '{ "git": { "allowTrunkPush": "true" } }' } + ) { + $r = Invoke-TrunkPush -Json $Json -Kind FastForward + $r.Code | Should -Be 1 + $r.Output | Should -Match 'a push to main is refused' + } + + It 'reads the setting from the remote sha git passes, not from a local remote-tracking ref' -Skip:($script:NoSh -or $script:NoPwshHere) { + $r = Invoke-TrunkPush -Json '{}' -SecondJson $script:OptIn -Kind FastForward + $r.Code | Should -Be 1 + $r.Output | Should -Match 'a push to main is refused' + } + + It 'refuses a fast-forward when only the working tree switches the setting on' -Skip:($script:NoSh -or $script:NoPwshHere) { + $r = Invoke-TrunkPush -Json '{}' -WorkTreeJson $script:OptIn -Kind FastForward + $r.Code | Should -Be 1 + $r.Output | Should -Match 'a push to main is refused' + } + } + It 'never falls back to Windows PowerShell, which cannot run a gate that needs PowerShell 7' { foreach ($name in 'pre-push', 'graphify-refresh') { $text = Get-Content -LiteralPath (Join-Path $PSScriptRoot '..' 'githooks' $name) -Raw diff --git a/core/apm.yml b/core/apm.yml index 67577ca..e1afdc7 100644 --- a/core/apm.yml +++ b/core/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-core -version: "1.2.0" +version: "1.3.0" description: The AI-SDLC core for every repository - workflow skills, review agents, hooks, core gates and the handbook. diff --git a/packs/bicep/apm.yml b/packs/bicep/apm.yml index c7f228c..c1ac72e 100644 --- a/packs/bicep/apm.yml +++ b/packs/bicep/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-bicep -version: "1.2.0" +version: "1.3.0" description: The Bicep pack - Bicep conventions for infrastructure templates. diff --git a/packs/blazor/apm.yml b/packs/blazor/apm.yml index 3bd8572..43124e6 100644 --- a/packs/blazor/apm.yml +++ b/packs/blazor/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-blazor -version: "1.2.0" +version: "1.3.0" description: The Blazor pack - portal UI conventions, component authorization and the Razor gates. diff --git a/packs/discovery/apm.yml b/packs/discovery/apm.yml index ad872ca..8144e4d 100644 --- a/packs/discovery/apm.yml +++ b/packs/discovery/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-discovery -version: "1.2.0" +version: "1.3.0" description: The discovery pack - the discovery workflows that write a research summary, a journey map and a hypothesis canvas as markdown files in the repository. diff --git a/packs/dotnet/apm.yml b/packs/dotnet/apm.yml index b4b8d41..a490cde 100644 --- a/packs/dotnet/apm.yml +++ b/packs/dotnet/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-dotnet -version: "1.2.0" +version: "1.3.0" description: The .NET pack - C# conventions, test-first, the .NET gates and the code graph. diff --git a/packs/github-issues/apm.yml b/packs/github-issues/apm.yml index 8133401..5cd691b 100644 --- a/packs/github-issues/apm.yml +++ b/packs/github-issues/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-github-issues -version: "1.2.0" +version: "1.3.0" description: The GitHub Issues pack - issue conventions and the issue contract gates. diff --git a/packs/github/.apm/skills/github-pull-requests/SKILL.md b/packs/github/.apm/skills/github-pull-requests/SKILL.md index c2d1d10..c42530e 100644 --- a/packs/github/.apm/skills/github-pull-requests/SKILL.md +++ b/packs/github/.apm/skills/github-pull-requests/SKILL.md @@ -20,7 +20,7 @@ Use this at the PR preflight and at the end of the loop. `implement`, `refactor` `write-platform-note`, `write-pipeline`, `write-prd`, `write-hypothesis`, `draw-customer-journey` and `write-research-summary` load it before they push, and `fix-bug` through `implement`. After a standalone run, `write-adr`, `update-constitution`, `write-docs`, `add-gate`, `capture-learning` and `write-api-spec` load it too. The push and the pull request run with no prompt. Checkpoint Bravo is the person's review of the pull request on GitHub ([orchestration.md](/.claude/skills/ai-sdlc-handbook/references/orchestration.md)). -A push to `main` or `master` is always refused. The link from the PR to its story is a tracker update, so it joins the +A push to `main` or `master` is refused, unless the repository sets `git.allowTrunkPush` in `aisdlc.json`. The link from the PR to its story is a tracker update, so it joins the one list of tracker updates for the run. The agent opens the pull request as a request for review; it never approves, reviews with approval, or merges one (C09). A hook refuses `gh pr merge` and a merge call through `gh api`. diff --git a/packs/github/apm.yml b/packs/github/apm.yml index d6ea747..c892d1f 100644 --- a/packs/github/apm.yml +++ b/packs/github/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-github -version: "1.2.0" +version: "1.3.0" description: The GitHub pack - pull requests, review threads, pull request delivery metrics and the GitHub Actions workflows. diff --git a/packs/knowledge/apm.yml b/packs/knowledge/apm.yml index 92e0efe..f125836 100644 --- a/packs/knowledge/apm.yml +++ b/packs/knowledge/apm.yml @@ -1,3 +1,3 @@ name: ai-sdlc-knowledge -version: "1.2.0" +version: "1.3.0" description: The knowledge pack - the conventions and the workflow for platform knowledge notes in the knowledge repository. diff --git a/packs/sonarcloud/apm.yml b/packs/sonarcloud/apm.yml index 35e8520..ecff446 100644 --- a/packs/sonarcloud/apm.yml +++ b/packs/sonarcloud/apm.yml @@ -1,5 +1,5 @@ name: ai-sdlc-sonarcloud -version: "1.2.0" +version: "1.3.0" description: The SonarQube Cloud pack - the SonarQube MCP server for the agent session, and the gate that keeps the token and the organization out of the repository. dependencies: mcp: