2026-03-11 11:22:20 +00:00
|
|
|
package tools
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
"fmt"
|
2026-03-11 11:22:20 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2026-03-17 10:56:52 +00:00
|
|
|
"time"
|
2026-03-11 11:22:20 +00:00
|
|
|
|
|
|
|
|
"github.com/sipeed/picoclaw/pkg/bus"
|
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
|
|
|
"github.com/sipeed/picoclaw/pkg/cron"
|
|
|
|
|
)
|
|
|
|
|
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
type stubJobExecutor struct {
|
|
|
|
|
response string
|
|
|
|
|
err error
|
|
|
|
|
alreadySent bool // simulate message tool having already sent in this round
|
|
|
|
|
lastPrompt string
|
|
|
|
|
lastKey string
|
|
|
|
|
lastChan string
|
|
|
|
|
lastChatID string
|
|
|
|
|
publishedResp string
|
|
|
|
|
publishedChan string
|
|
|
|
|
publishedChatID string
|
2026-04-27 11:17:25 +00:00
|
|
|
publishedKey string
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stubJobExecutor) ProcessDirectWithChannel(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
content, sessionKey, channel, chatID string,
|
|
|
|
|
) (string, error) {
|
|
|
|
|
s.lastPrompt = content
|
|
|
|
|
s.lastKey = sessionKey
|
|
|
|
|
s.lastChan = channel
|
|
|
|
|
s.lastChatID = chatID
|
|
|
|
|
return s.response, s.err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stubJobExecutor) PublishResponseIfNeeded(
|
|
|
|
|
_ context.Context,
|
2026-04-13 08:19:24 +00:00
|
|
|
channel, chatID, sessionKey, response string,
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
) {
|
|
|
|
|
if s.alreadySent {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.publishedResp = response
|
|
|
|
|
s.publishedChan = channel
|
|
|
|
|
s.publishedChatID = chatID
|
2026-04-27 11:17:25 +00:00
|
|
|
s.publishedKey = sessionKey
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newTestCronToolWithExecutorAndConfig(t *testing.T, executor JobExecutor, cfg *config.Config) *CronTool {
|
2026-03-11 11:22:20 +00:00
|
|
|
t.Helper()
|
|
|
|
|
storePath := filepath.Join(t.TempDir(), "cron.json")
|
|
|
|
|
cronService := cron.NewCronService(storePath, nil)
|
|
|
|
|
msgBus := bus.NewMessageBus()
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
tool, err := NewCronTool(cronService, executor, msgBus, t.TempDir(), true, 0, cfg)
|
2026-03-11 11:22:20 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("NewCronTool() error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
return tool
|
|
|
|
|
}
|
|
|
|
|
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
func newTestCronToolWithConfig(t *testing.T, cfg *config.Config) *CronTool {
|
|
|
|
|
t.Helper()
|
|
|
|
|
return newTestCronToolWithExecutorAndConfig(t, nil, cfg)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 01:44:32 +00:00
|
|
|
func newTestCronTool(t *testing.T) *CronTool {
|
|
|
|
|
t.Helper()
|
|
|
|
|
return newTestCronToolWithConfig(t, config.DefaultConfig())
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 11:22:20 +00:00
|
|
|
// TestCronTool_CommandBlockedFromRemoteChannel verifies command scheduling is restricted to internal channels
|
|
|
|
|
func TestCronTool_CommandBlockedFromRemoteChannel(t *testing.T) {
|
|
|
|
|
tool := newTestCronTool(t)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "telegram", "chat-1")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "check disk",
|
|
|
|
|
"command": "df -h",
|
|
|
|
|
"command_confirm": true,
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if !result.IsError {
|
|
|
|
|
t.Fatal("expected command scheduling to be blocked from remote channel")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "restricted to internal channels") {
|
|
|
|
|
t.Errorf("expected 'restricted to internal channels', got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 01:44:32 +00:00
|
|
|
func TestCronTool_CommandDoesNotRequireConfirmByDefault(t *testing.T) {
|
2026-03-11 11:22:20 +00:00
|
|
|
tool := newTestCronTool(t)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "cli", "direct")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "check disk",
|
|
|
|
|
"command": "df -h",
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-17 01:44:32 +00:00
|
|
|
if result.IsError {
|
|
|
|
|
t.Fatalf("expected command scheduling without confirm to succeed by default, got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "Cron job added") {
|
|
|
|
|
t.Errorf("expected 'Cron job added', got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCronTool_CommandRequiresConfirmWhenAllowCommandDisabled(t *testing.T) {
|
|
|
|
|
cfg := config.DefaultConfig()
|
|
|
|
|
cfg.Tools.Cron.AllowCommand = false
|
|
|
|
|
|
|
|
|
|
tool := newTestCronToolWithConfig(t, cfg)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "cli", "direct")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "check disk",
|
|
|
|
|
"command": "df -h",
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-11 11:22:20 +00:00
|
|
|
if !result.IsError {
|
2026-03-17 01:44:32 +00:00
|
|
|
t.Fatal("expected command scheduling to require confirm when allow_command is disabled")
|
2026-03-11 11:22:20 +00:00
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "command_confirm=true") {
|
2026-03-17 01:44:32 +00:00
|
|
|
t.Errorf("expected command_confirm requirement message, got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCronTool_CommandAllowedWithConfirmWhenAllowCommandDisabled(t *testing.T) {
|
|
|
|
|
cfg := config.DefaultConfig()
|
|
|
|
|
cfg.Tools.Cron.AllowCommand = false
|
|
|
|
|
|
|
|
|
|
tool := newTestCronToolWithConfig(t, cfg)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "cli", "direct")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "check disk",
|
|
|
|
|
"command": "df -h",
|
|
|
|
|
"command_confirm": true,
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if result.IsError {
|
|
|
|
|
t.Fatalf(
|
|
|
|
|
"expected command scheduling with confirm to succeed when allow_command is disabled, got: %s",
|
|
|
|
|
result.ForLLM,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "Cron job added") {
|
|
|
|
|
t.Errorf("expected 'Cron job added', got: %s", result.ForLLM)
|
2026-03-11 11:22:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 10:56:52 +00:00
|
|
|
func TestCronTool_CommandBlockedWhenExecDisabled(t *testing.T) {
|
|
|
|
|
cfg := config.DefaultConfig()
|
|
|
|
|
cfg.Tools.Exec.Enabled = false
|
|
|
|
|
|
|
|
|
|
tool := newTestCronToolWithConfig(t, cfg)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "cli", "direct")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "check disk",
|
|
|
|
|
"command": "df -h",
|
|
|
|
|
"command_confirm": true,
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if !result.IsError {
|
|
|
|
|
t.Fatal("expected command scheduling to be blocked when exec is disabled")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "command execution is disabled") {
|
|
|
|
|
t.Errorf("expected exec disabled message, got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 11:22:20 +00:00
|
|
|
// TestCronTool_CommandAllowedFromInternalChannel verifies command scheduling works from internal channels
|
|
|
|
|
func TestCronTool_CommandAllowedFromInternalChannel(t *testing.T) {
|
|
|
|
|
tool := newTestCronTool(t)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "cli", "direct")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "check disk",
|
|
|
|
|
"command": "df -h",
|
|
|
|
|
"command_confirm": true,
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if result.IsError {
|
|
|
|
|
t.Fatalf("expected command scheduling to succeed from internal channel, got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "Cron job added") {
|
|
|
|
|
t.Errorf("expected 'Cron job added', got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestCronTool_AddJobRequiresSessionContext verifies fail-closed when channel/chatID missing
|
|
|
|
|
func TestCronTool_AddJobRequiresSessionContext(t *testing.T) {
|
|
|
|
|
tool := newTestCronTool(t)
|
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "reminder",
|
|
|
|
|
"at_seconds": float64(60),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if !result.IsError {
|
|
|
|
|
t.Fatal("expected error when session context is missing")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(result.ForLLM, "no session context") {
|
|
|
|
|
t.Errorf("expected 'no session context' message, got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestCronTool_NonCommandJobAllowedFromRemoteChannel verifies regular reminders work from any channel
|
|
|
|
|
func TestCronTool_NonCommandJobAllowedFromRemoteChannel(t *testing.T) {
|
|
|
|
|
tool := newTestCronTool(t)
|
|
|
|
|
ctx := WithToolContext(context.Background(), "telegram", "chat-1")
|
|
|
|
|
result := tool.Execute(ctx, map[string]any{
|
|
|
|
|
"action": "add",
|
|
|
|
|
"message": "time to stretch",
|
|
|
|
|
"at_seconds": float64(600),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if result.IsError {
|
|
|
|
|
t.Fatalf("expected non-command reminder to succeed from remote channel, got: %s", result.ForLLM)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-14 13:03:23 +00:00
|
|
|
|
2026-03-17 10:56:52 +00:00
|
|
|
func TestCronTool_ExecuteJobPublishesErrorWhenExecDisabled(t *testing.T) {
|
|
|
|
|
cfg := config.DefaultConfig()
|
|
|
|
|
cfg.Tools.Exec.Enabled = false
|
|
|
|
|
|
|
|
|
|
tool := newTestCronToolWithConfig(t, cfg)
|
|
|
|
|
job := &cron.CronJob{}
|
|
|
|
|
job.Payload.Channel = "cli"
|
|
|
|
|
job.Payload.To = "direct"
|
|
|
|
|
job.Payload.Command = "df -h"
|
|
|
|
|
|
|
|
|
|
if got := tool.ExecuteJob(context.Background(), job); got != "ok" {
|
|
|
|
|
t.Fatalf("ExecuteJob() = %q, want ok", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2026-03-17 17:37:07 +00:00
|
|
|
var msg bus.OutboundMessage
|
|
|
|
|
select {
|
|
|
|
|
case msg = <-tool.msgBus.OutboundChan():
|
|
|
|
|
// got message
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
t.Fatal("timeout waiting for outbound message")
|
2026-03-17 10:56:52 +00:00
|
|
|
}
|
|
|
|
|
if !strings.Contains(msg.Content, "command execution is disabled") {
|
|
|
|
|
t.Fatalf("expected exec disabled message, got: %s", msg.Content)
|
|
|
|
|
}
|
|
|
|
|
}
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
|
|
|
|
|
func TestCronTool_ExecuteJobPublishesAgentResponse(t *testing.T) {
|
|
|
|
|
executor := &stubJobExecutor{response: "generated reply"}
|
|
|
|
|
tool := newTestCronToolWithExecutorAndConfig(t, executor, config.DefaultConfig())
|
|
|
|
|
|
|
|
|
|
job := &cron.CronJob{ID: "job-1"}
|
|
|
|
|
job.Payload.Channel = "telegram"
|
|
|
|
|
job.Payload.To = "chat-1"
|
|
|
|
|
job.Payload.Message = "send me a poem"
|
|
|
|
|
|
|
|
|
|
if got := tool.ExecuteJob(context.Background(), job); got != "ok" {
|
|
|
|
|
t.Fatalf("ExecuteJob() = %q, want ok", got)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 15:16:12 +00:00
|
|
|
if !strings.HasPrefix(executor.lastKey, "agent:cron-job-1-") {
|
|
|
|
|
t.Fatalf("sessionKey = %q, want agent:cron-job-1-{uuid}", executor.lastKey)
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
}
|
|
|
|
|
if executor.lastChan != "telegram" || executor.lastChatID != "chat-1" {
|
|
|
|
|
t.Fatalf("executor target = %s/%s, want telegram/chat-1", executor.lastChan, executor.lastChatID)
|
|
|
|
|
}
|
|
|
|
|
if executor.lastPrompt != "send me a poem" {
|
|
|
|
|
t.Fatalf("prompt = %q, want original message", executor.lastPrompt)
|
|
|
|
|
}
|
|
|
|
|
if executor.publishedResp != "generated reply" {
|
|
|
|
|
t.Fatalf("published response = %q, want generated reply", executor.publishedResp)
|
|
|
|
|
}
|
2026-04-27 11:17:25 +00:00
|
|
|
if executor.publishedKey != executor.lastKey {
|
|
|
|
|
t.Fatalf("published sessionKey = %q, want %q", executor.publishedKey, executor.lastKey)
|
|
|
|
|
}
|
fix(cron): publish agent response to outbound bus for cron-triggered jobs (#2100)
* fix(cron): publish agent response to outbound bus for cron-triggered jobs
When a cron job triggers agent execution via ProcessDirectWithChannel,
the agent response was silently discarded — the code assumed AgentLoop
would auto-publish it, but SendResponse is false on this path.
Delegate to PublishResponseIfNeeded (exported from AgentLoop) so the
response reaches the originating channel (e.g. Telegram) only when the
message tool did not already deliver content in the same round.
Also adds a "directive" message type to CronPayload, allowing cron jobs
to instruct the agent to execute a task rather than echo static text.
* fix(cron): add type validation and directive test coverage
Address reviewer blocking feedback:
1. Server-side whitelist for `type` parameter — the `enum` in
Parameters() is only an LLM schema hint; any string was persisted.
Now `addJob` rejects values other than "message" and "directive".
2. Comprehensive test coverage for the directive code path:
- directive adds prompt prefix to ProcessDirectWithChannel
- deliver=true + directive routes through agent (not direct publish)
- directive prompt content, sessionKey, channel, chatID are correct
- invalid type is rejected; valid types ("", "message", "directive") pass
- deliver=true message type goes directly to bus (regression)
- agent error path does not trigger publish (regression)
Also merge the two UpdateJob calls in addJob into one to avoid
redundant disk I/O (non-blocking suggestion from review).
* fix(cron): remove omitempty from CronPayload.Type for consistent JSON
Empty string and "message" are semantically equivalent defaults;
always serializing the field avoids asymmetric JSON output.
* test(cron): remove redundant test, strengthen error path coverage
- Remove ExecuteJobDirectivePassesCorrectContent: its assertions on
sessionKey/channel/chatID duplicate ExecuteJobPublishesAgentResponse;
its prompt check duplicates DirectiveAddsPromptPrefix.
- Strengthen DirectiveAddsPromptPrefix with exact prompt match and
publish response assertion.
- Fix ReturnsErrorWithoutPublish: set non-empty stub response so the
test verifies the error branch early-return, not the response==""
guard.
* fix(ci): satisfy golines and gosmopolitan in cron code
2026-03-29 05:47:28 +00:00
|
|
|
if executor.publishedChan != "telegram" || executor.publishedChatID != "chat-1" {
|
|
|
|
|
t.Fatalf("published target = %s/%s, want telegram/chat-1", executor.publishedChan, executor.publishedChatID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCronTool_ExecuteJobSkipsEmptyAgentResponse(t *testing.T) {
|
|
|
|
|
executor := &stubJobExecutor{}
|
|
|
|
|
tool := newTestCronToolWithExecutorAndConfig(t, executor, config.DefaultConfig())
|
|
|
|
|
|
|
|
|
|
job := &cron.CronJob{ID: "job-empty"}
|
|
|
|
|
job.Payload.Channel = "telegram"
|
|
|
|
|
job.Payload.To = "chat-1"
|
|
|
|
|
job.Payload.Message = "say nothing"
|
|
|
|
|
|
|
|
|
|
if got := tool.ExecuteJob(context.Background(), job); got != "ok" {
|
|
|
|
|
t.Fatalf("ExecuteJob() = %q, want ok", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if executor.publishedResp != "" {
|
|
|
|
|
t.Fatalf("unexpected published response: %q", executor.publishedResp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCronTool_ExecuteJobSkipsWhenMessageToolAlreadySent(t *testing.T) {
|
|
|
|
|
executor := &stubJobExecutor{response: "Sent.", alreadySent: true}
|
|
|
|
|
tool := newTestCronToolWithExecutorAndConfig(t, executor, config.DefaultConfig())
|
|
|
|
|
|
|
|
|
|
job := &cron.CronJob{ID: "job-msg-sent"}
|
|
|
|
|
job.Payload.Channel = "telegram"
|
|
|
|
|
job.Payload.To = "chat-1"
|
|
|
|
|
job.Payload.Message = "send weather"
|
|
|
|
|
|
|
|
|
|
if got := tool.ExecuteJob(context.Background(), job); got != "ok" {
|
|
|
|
|
t.Fatalf("ExecuteJob() = %q, want ok", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if executor.publishedResp != "" {
|
|
|
|
|
t.Fatalf("expected no published response when message tool already sent, got: %q", executor.publishedResp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCronTool_ExecuteJobReturnsErrorWithoutPublish(t *testing.T) {
|
|
|
|
|
executor := &stubJobExecutor{
|
|
|
|
|
response: "this response must not be published",
|
|
|
|
|
err: fmt.Errorf("agent failure"),
|
|
|
|
|
}
|
|
|
|
|
tool := newTestCronToolWithExecutorAndConfig(t, executor, config.DefaultConfig())
|
|
|
|
|
|
|
|
|
|
job := &cron.CronJob{ID: "job-err"}
|
|
|
|
|
job.Payload.Channel = "telegram"
|
|
|
|
|
job.Payload.To = "chat-1"
|
|
|
|
|
job.Payload.Message = "do something"
|
|
|
|
|
|
|
|
|
|
got := tool.ExecuteJob(context.Background(), job)
|
|
|
|
|
if !strings.Contains(got, "agent failure") {
|
|
|
|
|
t.Fatalf("ExecuteJob() = %q, want error message", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if executor.publishedResp != "" {
|
|
|
|
|
t.Fatalf("unexpected publish on error path: %q", executor.publishedResp)
|
|
|
|
|
}
|
|
|
|
|
}
|