This behaviour occurs both in eXist-1.4.x and trunk.
These could be two distinct issues, though both are related to in-memory nodes. Attached is a test file (persistent_in-memoryTest.xml) testing 2 issues:
-behaviour of location steps containing a variable that is bound to a node sequence (tests #1-#4)
-behaviour of last(), both in standalone predicates and predicates refining another predicate (tests #5-#16)
When a variable is bound to a node sequence (e.g. $els := //el), which is then used inside a location step (e.g. //test//$els), a wrong node is returned when in-memory nodes are involved (failing test #3). There is no problem with persistent nodes (succeeding tests #1-#2), or (with in-memory nodes) when the variable is refined with a further predicate (succeeding test #4).
The last() function behaves differently:
-in standalone predicates vs. predicates refining another predicate
-when used in isolation, or in other expressions like "position() = last()" or ". is $nodes[last()]"
-when refining persistent or in-memory nodes
Tests #5-#10 demonstrate behaviour on persistent nodes. Mostly unproblematic (succeeding tests #5-#6, #8-#9), but incorrect results when an expression like ". is $nodes[last()]" is used to refine the context nodeset. This occurs both for persistent nodes (failing test #7) and in-memory nodes (failing test #10).
Tests #11-#16 demonstrate behaviour on in-memory nodes. Mostly unproblematic (succeeding tests #11-#13, #15-#16). Only when a single predicate with the expression "last()" refines another predicate on in-memory nodes, an incorrect result is returned (failing test #14).