Sed: deleting last line prevents '$' address from working in the multi-script invocation

It looks like if matching and deleting the last line confuses 'sed' so it does not recognize '$' address. Consider:

sed -e '/^3/d' -e '$ a text'

supposed to delete a line starting with '3' and then append 'text' after the last line of input. But, if it is the last line of input which starts with '3' it gets deleted but nothing is appended.

echo "1\n2\n3" | sed -e '/^3/d' -e '$ a text'
1
2

versus

echo "1\n2\n3\n4" | sed -e '/^3/d' -e '$ a text'
1
2
4
text

Is this the expected behavior or a bug?

regards, Michal.

Difficult to say. Switching the two

echo -e "1\n2\n3"| sed -e '$atext
> /^3/d'
1
2
text

you get the desired behaviour, but this doesn't explain the observed.

Expected behavior. The d command jumps to the next cycle; the following sed commands are skipped.