Skip to content

Commit bd9aa64

Browse files
committed
fix: implement chat mode tools filtering and session marker cleanup
Chat mode now properly limits tools to: file_operations, memory_operations, web_operations, todo_operations, interact (tools 1, 4, 5, 6, 8). Changes: - SimpleAIAgent: added todo_operations to chat_mode_tools list - PromptBuilder: fixed caching logic - cache now only used when no enable_tools filter is set. Previously filtering was applied but cached result returned anyway, causing all 11 tools to still appear. - Chat.pm: strip session markers from final_response before displaying. Both code paths (messages_saved_during_workflow and else branch) now clean the marker before add_to_buffer. - StreamingController: strip simple session marker format (<!--session:name-->) in addition to structured format (<!--session:{...}-->) in all buffer flush locations. Previously only structured format was stripped, leaving simple markers visible in output. Testing: --chat mode now shows only 5 tools and no session data in output.
1 parent 694a0bb commit bd9aa64

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

lib/CLIO/Core/PromptBuilder.pm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ sub generate_tools_section {
166166
my ($self) = @_;
167167

168168
# Cache the tools section since tool registrations don't change during a session
169-
# But don't cache if there's an enable_tools filter (varies per session in multi-agent scenarios)
170-
my $cache_key = $self->{enable_tools} ? '_filtered_' . $self->{enable_tools} : '';
171-
return $self->{_tools_section_cache} if $self->{_tools_section_cache} && !$cache_key;
169+
# Only use cache if there's no enable_tools filter (which varies per session)
170+
if (!$self->{enable_tools} && $self->{_tools_section_cache}) {
171+
return $self->{_tools_section_cache};
172+
}
172173

173174
# Get all registered tool OBJECTS (not just names)
174175
my $all_tools = $self->{tool_registry}->get_all_tools();

lib/CLIO/Core/SimpleAIAgent.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ sub new {
4444
bless $self, $class;
4545

4646
# Chat mode: default to limited tool set and chat system prompt
47-
my $chat_mode_tools = 'web_operations,file_operations,memory_operations,interact';
47+
my $chat_mode_tools = 'web_operations,file_operations,memory_operations,interact,todo_operations';
4848
my $chat_mode_prompt = 'chat';
4949

5050
# Apply chat mode defaults if enabled (but respect explicit --enable/--prompt overrides)

lib/CLIO/UI/Chat.pm

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,17 +765,24 @@ sub _handle_ai_response {
765765
}
766766

767767
if ($accumulated_content) {
768-
$accumulated_content =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg;
768+
$accumulated_content =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
769+
$accumulated_content =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
769770
}
770771

771772
if ($result && $result->{messages_saved_during_workflow}) {
772773
log_debug('Chat', "Skipping session save - messages already saved during workflow");
773-
$self->add_to_buffer('assistant', $result->{final_response} // '') if $result->{final_response};
774+
my $display_response = $result->{final_response} // '';
775+
$display_response =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
776+
$display_response =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
777+
$self->add_to_buffer('assistant', $display_response) if $display_response;
774778
} elsif ($result && $result->{final_response}) {
775779
log_debug('Chat', "Storing final_response in session (length=" . length($result->{final_response}) . ")");
776780
my $sanitized = sanitize_text($result->{final_response});
777781
$self->{session}->add_message('assistant', $sanitized);
778-
$self->add_to_buffer('assistant', $result->{final_response});
782+
my $display_response = $result->{final_response};
783+
$display_response =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
784+
$display_response =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
785+
$self->add_to_buffer('assistant', $display_response);
779786
} elsif ($accumulated_content) {
780787
log_debug('Chat', "Storing accumulated_content in session (length=" . length($accumulated_content) . ")");
781788
my $sanitized = sanitize_text($accumulated_content);

lib/CLIO/UI/StreamingController.pm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ sub flush {
238238

239239
# Flush markdown buffer
240240
if ($self->{markdown_buffer} =~ /\S/) {
241-
$self->{markdown_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg;
241+
$self->{markdown_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
242+
$self->{markdown_buffer} =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
242243
}
243244
if ($self->{markdown_buffer} =~ /\S/) {
244245
log_debug('Chat', "Flushing markdown_buffer (" . length($self->{markdown_buffer}) . " bytes)");
@@ -256,7 +257,8 @@ sub flush {
256257

257258
# Flush line buffer (incomplete final line)
258259
if ($self->{line_buffer} =~ /\S/) {
259-
$self->{line_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg;
260+
$self->{line_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
261+
$self->{line_buffer} =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
260262
}
261263
if ($self->{line_buffer} =~ /\S/) {
262264
log_debug('Chat', "Flushing line_buffer (" . length($self->{line_buffer}) . " bytes)");
@@ -291,6 +293,8 @@ sub flush_for_tools {
291293
my $printed = 0;
292294

293295
if ($self->{markdown_buffer} =~ /\S/) {
296+
$self->{markdown_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
297+
$self->{markdown_buffer} =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
294298
my $output = $self->{markdown_buffer};
295299
if ($ui->{enable_markdown}) {
296300
$output = $ui->render_markdown($self->{markdown_buffer});
@@ -304,7 +308,8 @@ sub flush_for_tools {
304308
}
305309

306310
if ($self->{line_buffer} =~ /\S/) {
307-
$self->{line_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg;
311+
$self->{line_buffer} =~ s/\s*<!--session:\{[^}]*\}-->\s*//sg; # Structured
312+
$self->{line_buffer} =~ s/\s*<!--session:[a-z][a-z0-9_-]{2,50}-->\s*//sgi; # Simple
308313
}
309314
if ($self->{line_buffer} =~ /\S/) {
310315
my $output = $self->{line_buffer};

0 commit comments

Comments
 (0)