Langertha-Raider
view release on metacpan or search on metacpan
t/86_raider_self_tools.t view on Meta::CPAN
like($result->{content}[0]{text}, qr/Perl is a programming/, 'last_n returns recent');
like($result->{content}[0]{text}, qr/Python/, 'last_n returns most recent');
};
# --- Test: session history rendering across wire formats (karr #96) ---
# session_history holds whatever the engine's format_tool_results() put on the
# wire, so the element shape follows tool_wire_format. Only openai/ollama/hermes
# hand every element a {role} plus a plain-string {content}; anthropic content
# is an ArrayRef of blocks, gemini keeps blocks under {parts}, and responses
# items have no {role} at all. Rendering must stay readable for all of them and
# must not emit uninitialized warnings.
{
package MockToolServer;
use Moose;
has tools => (is => 'ro', default => sub { {} });
sub tool {
my ( $self, %args ) = @_;
$self->tools->{$args{name}} = \%args;
return;
}
__PACKAGE__->meta->make_immutable;
}
{
package MockToolHandle;
use Moose;
sub text_result {
my ( $self, $text ) = @_;
return { content => [ { type => 'text', text => $text } ] };
}
__PACKAGE__->meta->make_immutable;
}
sub mixed_wire_history {
return (
# --- anthropic: content is an ArrayRef of blocks ---
{ role => 'user', content => 'What is the weather in Berlin?' },
{ role => 'assistant', content => [
{ type => 'text', text => 'Let me check the weather.' },
{ type => 'tool_use', id => 'toolu_01', name => 'get_weather',
input => { city => 'Berlin' } },
] },
{ role => 'user', content => [
{ type => 'tool_result', tool_use_id => 'toolu_01',
content => [ { type => 'text', text => 'Berlin: sunny, 22C' } ] },
] },
# --- responses: envelope items, discriminated by {type}, no {role} ---
{ type => 'reasoning', id => 'rs_1',
summary => [ { type => 'summary_text', text => 'Need the weather tool.' } ] },
{ type => 'message', role => 'assistant', status => 'completed',
content => [ { type => 'output_text', text => 'Checking Hamburg now.' } ] },
{ type => 'function_call', call_id => 'call_1', name => 'get_weather',
arguments => '{"city":"Hamburg"}' },
{ type => 'function_call_output', call_id => 'call_1',
output => '[{"text":"Hamburg: rainy, 14C","type":"text"}]' },
# --- openai: assistant echo has content => undef when it only calls tools ---
{ role => 'assistant', content => undef, tool_calls => [
{ id => 'call_2', type => 'function',
function => { name => 'get_time', arguments => '{"tz":"CET"}' } },
] },
{ role => 'tool', tool_call_id => 'call_2',
content => '[{"type":"text","text":"14:05"}]' },
# --- gemini: blocks live under {parts}, there is no {content} at all ---
{ role => 'model', parts => [
{ text => 'One moment.' },
{ functionCall => { name => 'get_weather', args => { city => 'Kiel' } } },
] },
{ role => 'user', parts => [
{ functionResponse => { name => 'get_weather',
response => { result => 'Kiel: windy, 17C' } } },
] },
);
}
subtest 'session history rendering across wire formats' => sub {
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
raider_mcp => 1,
);
push @{$raider->session_history}, mixed_wire_history();
my @warnings;
my $text;
{
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
$text = $raider->_query_session_history_f({})->get;
}
is(scalar @warnings, 0, 'no warnings while rendering mixed-wire history')
or diag("warnings: @warnings");
unlike($text, qr/ARRAY\(0x/, 'ArrayRef content is flattened, not stringified');
unlike($text, qr/HASH\(0x/, 'HashRef payloads are flattened, not stringified');
unlike($text, qr/^\[[^\]]*\]\s*$/m, 'no history element renders as a bare label');
# anthropic
like($text, qr/\[assistant\] Let me check the weather\./, 'anthropic text block rendered');
like($text, qr/get_weather.*Berlin/, 'anthropic tool_use is visible with its arguments');
like($text, qr/Berlin: sunny, 22C/, 'anthropic tool_result content flattened to text');
# responses
like($text, qr/\[reasoning\] Need the weather tool\./, 'responses reasoning summary rendered');
like($text, qr/\[assistant\] Checking Hamburg now\./, 'responses message item uses its role');
like($text, qr/\[function_call\] .*get_weather.*Hamburg/, 'responses function_call labelled by type');
like($text, qr/\[function_call_output\] .*Hamburg: rainy, 14C/,
'responses function_call_output labelled by type and carries its output');
# openai
like($text, qr/\[assistant\] .*get_time.*CET/, 'openai tool-only assistant echo shows the call');
like($text, qr/14:05/, 'openai tool result content kept');
# gemini
like($text, qr/\[model\] One moment\./, 'gemini text part rendered');
like($text, qr/get_weather.*Kiel/, 'gemini functionCall part is visible');
like($text, qr/Kiel: windy, 17C/, 'gemini functionResponse flattened');
};
subtest 'session history query filters on rendered payload' => sub {
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
raider_mcp => 1,
);
push @{$raider->session_history}, mixed_wire_history();
my @warnings;
my ( $hamburg, $kiel );
{
local $SIG{__WARN__} = sub { push @warnings, $_[0] };
$hamburg = $raider->_query_session_history_f({ query => 'Hamburg' })->get;
$kiel = $raider->_query_session_history_f({ query => 'Kiel: windy' })->get;
}
is(scalar @warnings, 0, 'no warnings while filtering mixed-wire history')
or diag("warnings: @warnings");
like($hamburg, qr/Hamburg/, 'filter matches inside responses items');
unlike($hamburg, qr/Berlin/, 'filter excludes non-matching elements');
like($kiel, qr/Kiel: windy, 17C/, 'filter reaches into gemini functionResponse text');
};
# Since langertha k344 a tool result that carries an image goes out natively on
# two wires when the model claims image_input (Langertha::ToolResult
# to_responses / to_gemini): the Responses function_call_output.output becomes
# an input_text / input_image part array instead of a string, and Gemini 3
# moves the image into functionResponse.parts[].inlineData next to the
# `result` string. Neither may render as an empty "tool_result:" -- the text
# stays readable and the image shows up as a marker, never as base64.
subtest 'tool-result images from native image wires (k344)' => sub {
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
raider_mcp => 1,
);
my $b64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYGD4DwABBAEAwS2OUAAAAABJRU5ErkJggg==';
push @{$raider->session_history},
{ type => 'function_call_output', call_id => 'call_img',
output => [
{ type => 'input_text', text => 'Screenshot of Luebeck harbour' },
{ type => 'input_image', image_url => "data:image/png;base64,$b64" },
] },
{ role => 'user', parts => [
{ functionResponse => { name => 'screenshot', id => 'fc_1',
response => { result => 'Screenshot of Rostock harbour' },
parts => [ { inlineData => { mimeType => 'image/png', data => $b64 } } ] } },
] },
{ role => 'user', parts => [
{ functionResponse => { name => 'screenshot', id => 'fc_2',
response => { result => '' },
parts => [ { inlineData => { mimeType => 'image/png', data => $b64 } } ] } },
] };
t/86_raider_self_tools.t view on Meta::CPAN
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
raider_mcp => 1,
);
is($raider->_query_session_history_f({})->get, 'No messages in session history.',
'empty history keeps its placeholder');
};
# --- Test: session-history embeddings (karr #99) ---
#
# _query_session_history_f looks the vector of history element $i up as
# _session_embeddings->[$i], so the two arrays must stay 1:1: a message
# without embeddable text still needs its slot, filled with undef. A missing
# slot shifts every later vector onto the wrong message and the similarity
# search then answers with that wrong message, silently. The embedded text is
# the same rendered payload the grep fallback matches, so both search modes
# see one history element the same way.
{
package MockEmbeddingEngine;
use Moose;
use Future;
# Deterministic bag-of-words vector over a fixed vocabulary â similar
# enough to real embeddings to prove the search lands on the right
# message, without touching the network.
my @VOCAB = qw( aardvark zebra narwhal tusk berlin weather time );
has calls => (is => 'ro', default => sub { [] });
has die_on => (is => 'ro', predicate => 'has_die_on');
sub simple_embedding {
my ( $self, $text ) = @_;
push @{$self->calls}, $text;
die "embedding backend unavailable\n"
if $self->has_die_on && index($text, $self->die_on) >= 0;
my $lc_text = lc $text;
return [ map { scalar( () = $lc_text =~ /\Q$_\E/g ) } @VOCAB ];
}
# What raider calls: an already-completed Future, so the slot fills at once.
sub simple_embedding_f {
my ( $self, $text ) = @_;
return Future->call(sub { Future->done($self->simple_embedding($text)) });
}
__PACKAGE__->meta->make_immutable;
}
# Positions 1 and 3 carry no plain-string {content}: the empty assistant turn
# renders to nothing at all, the responses function_call item has no
# {content} key. Position 2 is anthropic ArrayRef content, which must be
# rendered rather than embedded as "ARRAY(0x...)".
sub embedding_probe_history {
return (
{ role => 'user', content => 'Aardvark migration question' },
{ role => 'assistant', content => '' },
{ role => 'assistant', content => [
{ type => 'text', text => 'The zebra crossing report' } ] },
{ type => 'function_call', call_id => 'call_9', name => 'get_time',
arguments => '{"tz":"CET"}' },
{ role => 'assistant', content => 'Narwhal tusk measurements are stable' },
);
}
subtest 'session embeddings keep one slot per history message' => sub {
my $embedder = MockEmbeddingEngine->new;
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
embedding_engine => $embedder,
raider_mcp => 1,
);
$raider->_push_session_history(embedding_probe_history());
my @embedded = @{$embedder->calls};
my @hist = @{$raider->session_history};
my $slots = $raider->_session_embeddings;
is(scalar @$slots, scalar @hist, 'one embedding slot per history message');
for my $i (0..$#hist) {
my $text = Langertha::Raider::_render_history_payload($hist[$i]);
if (length $text) {
is_deeply($slots->[$i], $embedder->simple_embedding($text),
"slot $i holds the vector of the message at position $i");
} else {
is($slots->[$i], undef, "slot $i is undef for a message without text");
}
}
unlike($_, qr/ARRAY\(0x|HASH\(0x/,
'embedded text is the rendered payload, never a stringified ref')
for @embedded;
ok(scalar(grep { /get_time/ } @embedded),
'an element that is only a tool call is embedded by its rendered call');
};
subtest 'session embedding search returns the matching message' => sub {
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
embedding_engine => MockEmbeddingEngine->new,
raider_mcp => 1,
);
$raider->_push_session_history(embedding_probe_history());
my $text = $raider->_query_session_history_f({ search => 'narwhal tusk' })->get;
my ( $top ) = split /\n\n/, $text;
like($top, qr/Narwhal tusk measurements/, 'top hit is the message that matches');
unlike($top, qr/zebra/, 'not the message a drifted index would have returned');
};
subtest 'drifted embeddings degrade to text search instead of lying' => sub {
my $raider = Langertha::Raider->new(
engine => MockEngine->new,
embedding_engine => MockEmbeddingEngine->new,
raider_mcp => 1,
);
$raider->_push_session_history(embedding_probe_history());
# session_history is public: code outside the pusher can append to it and
# leave the two arrays out of step.
( run in 0.502 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )