Term Expansion issue when consult is called from a Module (was: How to get "Consult('Test.pl'), predicateFromInsideTest." to run)

I am trying to implement a simple Prolog server with a JSON interface using this gist from @jan
from this thread. I’m running into a problem when using consult/1 to load files. I think it stems from the fact that the server is acting like one big, long running query that never halts. Here’s a simplification of the important code from the server:

% Pass in the TCP/IP Stream that commands get sent upon
serve_loop(ReadStream) :-
    read_term(ReadStream, Command, []),
    call(Command),
    serve_loop(ReadStream).

Here’s the problem: If I call the server once to consult a file and then once to call a predicate from inside that file, it fails with “ERROR: Unknown procedure”. I suspect it could be related to the behavior below. It appears as though running a single query that includes a consult/1 along with references to terms inside it doesn’t work:

% Test.pl
foo(X) :- writeln(X).
?- consult("/.../Test.pl"), foo(a).
ERROR: Unknown procedure: foo/1 (DWIM could not correct goal)

?- consult("/.../Test.pl").
true.

?- foo(a).
a
true.

Any clues as to what is going on and how to fix it?

OK, it turns out the Test.pl scenario above does work. Operator error on my part made it fail. What doesn’t work is a more complicated example using term expansion within the files that are consulted. Writing up that scenario now…

Turns out I think the issue is related to the issue described in this thread.

The server is running in a module called “prologServer”:

% PrologServer.pl
:- module(prologServer,
          [ ... ]).
...
serve_loop(ReadStream) :-
    read_term(ReadStream, Command, []),
    call(Command),
    serve_loop(ReadStream).
...

I first ask the server to consult("TermExpansion.pl") which defines a term expansion:

% TermExpansion.pl
term_expansion(expandMe(X), [(writeMe(X) :- (writeln(X)))]).

Then I then ask the server to consult("ExpandedTerms") which defines a term to expand. This file is a module and exports the term that is created by expansion:

% ExpandedTerms.pl
:- module(expandedTerms,
          [ writeMe/1
          ]).
expandMe(X).

The error I get when I run that sequence is: “ERROR: Exported procedure expandedTerms:writeMe/1 is not defined”.

I believe that when the code in the prologServer module processes consult("TermExpansion.pl"), the term_expansion/2 term is not being loaded into the user: module. I suspect it is loaded into the prologServer module? Since that is where the code that does call(Command) is? Maybe?

If I simply consult the two files from the Prolog top level, it works fine, since the term_expansion/2 term is put in the user: module and found.
[edit: also note that moving the server code out of a module also fixes it…]

Adding user: in front of term_expansion/2 fixed it when running in the server. Like this:

% TermExpansion.pl
user:term_expansion(expandMe(X), [(writeMe(X) :- (writeln(X)))]).

Does consult/1 process files differently based on where it is called from? Or is there some other explanation here?