Let me try to give you an answer that helps you a bit to understand what is going on. The main point is that I was sort of lucky when I gave you the previous answer. (OK, I also tried other things that did not work and then presented the one thing that did work. Yet writing this partial answer made it clear to me that I was not aware of some important things when I wrote the other answer.) I try to address your questions by rewriting my code in such a way that the decoration switches to a separate state when on the close path segments. I added several \typeouts in order to illustrate what's going on. Everything works in the way I thought it would except that
now the path does not close despite \pgfpathclose gets executed.
This what I mean by "I was very lucky when I wrote my previous answer". Why does closing not work here? Well, this is because one has to distinguish between meta decorations and decorations, i.e. between
\pgfdeclaredecoration
where if input segment is closepath is defined, and
\pgfdeclaremetadecoration
which the code is based on. (No, I do not think it is a good idea to do the same with a plain decoration because it will be very hard, at least, to make it work with general paths that are composed of straight lines of arbitrary lengths. If one had a quantization rule for the lengths of the line segments, that would be possible.) What this partial answer does here is to mix the two. This is the reason why the full path in \pgfkeys{/pgf/decoration automaton/if input segment is closepath=...} is needed. This does work in the sense that the automaton will switch to a new state, called close, when it is on the close path segment. Then it checks if it is almost at the end, and if so, it does something. However, contrary to my naive expectations, a naive \pgfpathclose does not work here. Unfortunately, I am not able to provide you with a cleaner working answer than in my previous answer (where I was quite lucky). I sort of understand why it worked there: the code switches to "ordinary decoration" mode and injects a \pgfpathclose there. I wish I could make it cleaner. Here is the code.
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{decorations}
\pgfdeclaremetadecoration{draw part of a path}{initial}{
\state{initial}[width={\pgfmetadecoratedpathlength*\pgfdecorationsegmentlength},next
state=almost final]{
\decoration{lineto}
} %/pgf/decoration automaton/if input segment is closepath=/pgf/close
\state{almost final}[next state=final,
/pgf/decoration automaton/if input segment is closepath={next
state=close}]{%
\typeout{almost\space final}}
\state{close}[next state=final]{
\typeout{I'm\space on\space close\space path\space segment\space and\space
have\space decorated\space\the\pgfdecorationsegmentlength}
\ifdim\pgfdecorationsegmentlength>0.999pt
\typeout{gonna\space close}
\pgfpathclose % <- does not work this way
\fi}
\state{final}{}
}
%
\tikzset{start segment/.style={decoration={draw part of a path,raise=2mm,segment length=#1},decorate}}
\begin{document}
\foreach \rpos in {0,.03,...,1,0.9999}{%
\begin{tikzpicture}[scale=1]
\fill[green!40](0,0) rectangle (4,4);
\draw[start segment=\rpos,blue,ultra thick] (0,0) -- (4,0) -- (4,4) -- (0,4) --cycle ;
\begin{scope}[xshift=5cm]
\fill[red!40](0,0) rectangle (4,4);
\draw[start segment=\rpos,blue,ultra thick] (0,0) -- (4,0) -- (4,4) -- (0,4);
\end{scope}
\end{tikzpicture}
}
\end{document}
Partial summary:
- You may mix to some extent
decoration stuff with meta decoration stuff but you need to be lucky that it works in the way you wish it would.
- This part of the TikZ magic seems not to be fully explored on this site, but there are certainly others who understand that much better and will be able to give you a better answer. Since I did not see Jake answering questions, I hope that @cfr and/or @percusse and/or Mark Wibrow and/or @Max see this nice question and provide an answer from which both of us can learn. ;-)
EDIT: I actually think there is a chance that the problem is deeper than outlined above. Consider the MWE
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\foreach \rpos in {0,.03,...,1,1.1}{%
\begin{tikzpicture}[scale=1]
\fill[green!40](0,0) rectangle (4,4);
\draw[dash pattern=on \rpos*16cm off 16cm,blue,ultra thick]
(0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
\begin{scope}[xshift=5cm]
\fill[red!40](0,0) rectangle (4,4);
\draw[dash pattern=on \rpos*16cm off 16cm,blue,ultra thick] (0,0) -- (4,0) -- (4,4) -- (0,4);
\end{scope}
\end{tikzpicture}
}
\end{document}
No decorations, nothing but a dash pattern to have the same effect. The final path of the first boundary is not closed

even though there the on phase is longer than the path. So for some reason TikZ does not even close this path. (If that would have worked, it would have been the basis a much simpler solution, one only would have to replace the hard coded 16cm by some \pgfdecoratedpathlength that can be read off as described e.g. here.) Of course, you could always close this simple path by hand:
\draw[dash pattern=on \rpos*16cm off 16cm,blue,ultra thick]
(0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,-\pgflinewidth/2) -- cycle;
but personally I would then think that my previous working solution is better even if I cannot rewrite it in the way you suggest.