@@ -21,6 +21,7 @@ for (const key of [
2121 "CLAWSWEEPER_STATE_DIR" ,
2222 "CLAWSWEEPER_PUBLISH_ROOT" ,
2323 "CLAWSWEEPER_PUBLISH_BRANCH" ,
24+ "CLAWSWEEPER_STATE_REPOSITORY" ,
2425] ) {
2526 delete process . env [ key ] ;
2627}
@@ -1646,6 +1647,68 @@ test("publishMainCommit converges after production-sized immutable ledger races"
16461647 }
16471648} ) ;
16481649
1650+ test ( "publishMainCommit escapes sustained immutable ledger races through a server merge" , ( ) => {
1651+ const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "clawsweeper-publish-server-merge-" ) ) ;
1652+ const origin = path . join ( root , "origin.git" ) ;
1653+ const work = path . join ( root , "work" ) ;
1654+ const other = path . join ( root , "other" ) ;
1655+ const fakeBin = path . join ( root , "bin" ) ;
1656+ const ledgerPath =
1657+ "ledger/v1/import-bindings/events/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff.json" ;
1658+ run ( "git" , [ "init" , "--bare" , origin ] , root ) ;
1659+ run ( "git" , [ "clone" , origin , work ] , root ) ;
1660+ configureUser ( work ) ;
1661+ write ( path . join ( work , "remote.txt" ) , "base\n" ) ;
1662+ run ( "git" , [ "add" , "." ] , work ) ;
1663+ run ( "git" , [ "commit" , "-m" , "initial" ] , work ) ;
1664+ run ( "git" , [ "push" , "origin" , "HEAD:main" ] , work ) ;
1665+ run ( "git" , [ "--git-dir" , origin , "symbolic-ref" , "HEAD" , "refs/heads/main" ] , root ) ;
1666+ run ( "git" , [ "checkout" , "-B" , "main" , "origin/main" ] , work ) ;
1667+ installCheckoutHeader ( work ) ;
1668+
1669+ run ( "git" , [ "clone" , origin , other ] , root ) ;
1670+ configureUser ( other ) ;
1671+ write ( path . join ( work , ledgerPath ) , '{"local":true}\n' ) ;
1672+ installSimplePushRaceHook ( work , other , 65 ) ;
1673+ installFakeGitHubMerge ( fakeBin , origin ) ;
1674+
1675+ const lines = [ ] ;
1676+ let result ;
1677+ captureConsoleLog ( ( ) => {
1678+ result = withEnv (
1679+ {
1680+ CLAWSWEEPER_STATE_REPOSITORY : "openclaw/clawsweeper-state" ,
1681+ GITHUB_RUN_ID : "12345" ,
1682+ GITHUB_RUN_ATTEMPT : "1" ,
1683+ PATH : `${ fakeBin } :${ process . env . PATH } ` ,
1684+ } ,
1685+ ( ) =>
1686+ withCwd ( work , ( ) =>
1687+ publishMainCommit ( {
1688+ message : "chore: append command action ledger" ,
1689+ paths : [ ledgerPath ] ,
1690+ maxAttempts : 8 ,
1691+ pushAttempts : 3 ,
1692+ } ) ,
1693+ ) ,
1694+ ) ;
1695+ } , lines ) ;
1696+
1697+ assert . equal ( result , "committed" ) ;
1698+ assert . equal (
1699+ run ( "git" , [ "--git-dir" , origin , "show" , `main:${ ledgerPath } ` ] , root ) ,
1700+ '{"local":true}\n' ,
1701+ ) ;
1702+ assert . equal (
1703+ run ( "git" , [ "--git-dir" , origin , "show" , "main:remote.txt" ] , root ) ,
1704+ "base\nrace 1\nrace 2\n" ,
1705+ "the server merge preserves state writes that beat both client pushes" ,
1706+ ) ;
1707+ const metrics = lines . find ( ( line ) => line . startsWith ( "Git publish metrics:" ) ) ;
1708+ assert . match ( metrics , / a c t i o n s = .* g h - a p i : 2 (?: , | $ ) / , "merge and cleanup use the GitHub API" ) ;
1709+ assert . match ( metrics , / a c t i o n s = .* p u s h : 2 (?: , | $ ) / , "the client stops racing the state ref" ) ;
1710+ } ) ;
1711+
16491712test ( "publishMainCommit rebuilds a production-sized flat immutable ledger tree" , ( ) => {
16501713 const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "clawsweeper-publish-large-tree-" ) ) ;
16511714 const origin = path . join ( root , "origin.git" ) ;
@@ -2749,6 +2812,85 @@ fi
27492812 fs . chmodSync ( hook , 0o755 ) ;
27502813}
27512814
2815+ function installSimplePushRaceHook ( work , other , raceCount , branch = "main" ) {
2816+ const hook = path . join ( work , ".git/hooks/pre-push" ) ;
2817+ const counter = path . join ( work , ".git/hooks/pre-push-count" ) ;
2818+ fs . writeFileSync (
2819+ hook ,
2820+ `#!/bin/sh
2821+ set -e
2822+ count=0
2823+ if test -f "${ counter } "; then count=$(cat "${ counter } "); fi
2824+ count=$((count + 1))
2825+ printf '%s\n' "$count" > "${ counter } "
2826+ if test "$count" -le ${ raceCount } ; then
2827+ printf 'race %s\n' "$count" >> "${ path . join ( other , "remote.txt" ) } "
2828+ git -C "${ other } " add remote.txt
2829+ git -C "${ other } " commit -m "concurrent state update $count"
2830+ git -C "${ other } " push origin HEAD:${ branch }
2831+ fi
2832+ ` ,
2833+ ) ;
2834+ fs . chmodSync ( hook , 0o755 ) ;
2835+ }
2836+
2837+ function installFakeGitHubMerge ( fakeBin , origin ) {
2838+ fs . mkdirSync ( fakeBin , { recursive : true } ) ;
2839+ const gh = path . join ( fakeBin , "gh" ) ;
2840+ fs . writeFileSync (
2841+ gh ,
2842+ `#!/bin/sh
2843+ set -eu
2844+ method=GET
2845+ endpoint=
2846+ base=
2847+ head=
2848+ while test "$#" -gt 0; do
2849+ case "$1" in
2850+ api) shift; endpoint="$1" ;;
2851+ --method) shift; method="$1" ;;
2852+ -f)
2853+ shift
2854+ case "$1" in
2855+ base=*) base=\${1#base=} ;;
2856+ head=*) head=\${1#head=} ;;
2857+ esac
2858+ ;;
2859+ esac
2860+ shift
2861+ done
2862+ case "$method:$endpoint" in
2863+ POST:repos/openclaw/clawsweeper-state/merges)
2864+ base_sha=$(git --git-dir "${ origin } " rev-parse "refs/heads/$base")
2865+ head_sha=$(git --git-dir "${ origin } " rev-parse "refs/heads/$head")
2866+ tree=$(git --git-dir "${ origin } " merge-tree --write-tree "$base_sha" "$head_sha")
2867+ commit=$(printf '%s\n' 'server merge' | GIT_AUTHOR_NAME=test [email protected] GIT_COMMITTER_NAME=test [email protected] git --git-dir "${ origin } " commit-tree "$tree" -p "$base_sha" -p "$head_sha") 2868+ git --git-dir "${ origin } " update-ref "refs/heads/$base" "$commit" "$base_sha"
2869+ printf '{"sha":"%s"}\n' "$commit"
2870+ ;;
2871+ DELETE:repos/openclaw/clawsweeper-state/git/refs/heads/*)
2872+ ref=\${endpoint#repos/openclaw/clawsweeper-state/git/refs/heads/}
2873+ git --git-dir "${ origin } " update-ref -d "refs/heads/$ref"
2874+ ;;
2875+ *)
2876+ printf '%s\n' "unexpected fake gh call: $method $endpoint" >&2
2877+ exit 2
2878+ ;;
2879+ esac
2880+ ` ,
2881+ ) ;
2882+ fs . chmodSync ( gh , 0o755 ) ;
2883+ }
2884+
2885+ function installCheckoutHeader ( work ) {
2886+ const encoded = Buffer . from (
2887+ [ "fixture-user" , "fixture" ] . join ( String . fromCharCode ( 58 ) ) ,
2888+ "utf8" ,
2889+ ) . toString ( "base64" ) ;
2890+ const header = [ `${ "AUTH" } ORIZATION:` , `${ "bas" } ic` , encoded ] . join ( " " ) ;
2891+ run ( "git" , [ "config" , "--local" , "http.https://github.com/.extraheader" , header ] , work ) ;
2892+ }
2893+
27522894function installCheckpointFailureHook ( work , other , branch ) {
27532895 const hook = path . join ( work , ".git/hooks/pre-push" ) ;
27542896 const counter = path . join ( work , ".git/hooks/pre-push-count" ) ;
0 commit comments