Skip to content

Commit ba6f061

Browse files
authored
Merge pull request sipeed#1207 from afjcjsbx/feat/debug-mode-no-truncate
feat: no-truncate param for debug
2 parents c4c16b3 + 013fc35 commit ba6f061

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
package gateway
22

33
import (
4+
"fmt"
5+
46
"github.com/spf13/cobra"
7+
8+
"github.com/sipeed/picoclaw/pkg/logger"
9+
"github.com/sipeed/picoclaw/pkg/utils"
510
)
611

712
func NewGatewayCommand() *cobra.Command {
813
var debug bool
14+
var noTruncate bool
915

1016
cmd := &cobra.Command{
1117
Use: "gateway",
1218
Aliases: []string{"g"},
1319
Short: "Start picoclaw gateway",
1420
Args: cobra.NoArgs,
21+
PreRunE: func(_ *cobra.Command, _ []string) error {
22+
if noTruncate && !debug {
23+
return fmt.Errorf("the --no-truncate option can only be used in conjunction with --debug (-d)")
24+
}
25+
26+
if noTruncate {
27+
utils.SetDisableTruncation(true)
28+
logger.Info("String truncation is globally disabled via 'no-truncate' flag")
29+
}
30+
31+
return nil
32+
},
1533
RunE: func(_ *cobra.Command, _ []string) error {
1634
return gatewayCmd(debug)
1735
},
1836
}
1937

2038
cmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
39+
cmd.Flags().BoolVarP(&noTruncate, "no-truncate", "T", false, "Disable string truncation in debug logs")
2140

2241
return cmd
2342
}

docs/debug.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Debugging PicoClaw
2+
3+
PicoClaw performs multiple complex interactions under the hood for every single request it receives—from routing messages and evaluating complexity, to executing tools and adapting to model failures. Being able to see exactly what is happening is crucial, not just for troubleshooting potential issues, but also for truly understanding how the agent operates.
4+
## Starting PicoClaw in Debug Mode
5+
6+
To get detailed information about what the agent is doing (LLM requests, tool calls, message routing), you can start the PicoClaw gateway with the debug flag:
7+
8+
```bash
9+
picoclaw gateway --debug
10+
# or
11+
picoclaw gateway -d
12+
```
13+
14+
In this mode, the system will format the logs extensively and display previews of system prompts and tool execution results.
15+
16+
## Disabling Log Truncation (Full Logs)
17+
18+
By default, PicoClaw truncates very long strings (such as the *System Prompt* or large JSON output results) in the debug logs to keep the console readable.
19+
20+
If you need to inspect the complete output of a command or the exact payload sent to the LLM model, you can use the `--no-truncate` flag.
21+
22+
**Note:** This flag *only* works when combined with the `--debug` mode.
23+
24+
```bash
25+
picoclaw gateway --debug --no-truncate
26+
27+
```
28+
29+
When this flag is active, the global truncation function is disabled. This is extremely useful for:
30+
31+
* Verifying the exact syntax of the messages sent to the provider.
32+
* Reading the complete output of tools like `exec`, `web_fetch`, or `read_file`.
33+
* Debugging the session history saved in memory.

pkg/agent/context.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sipeed/picoclaw/pkg/logger"
1717
"github.com/sipeed/picoclaw/pkg/providers"
1818
"github.com/sipeed/picoclaw/pkg/skills"
19+
"github.com/sipeed/picoclaw/pkg/utils"
1920
)
2021

2122
type ContextBuilder struct {
@@ -538,10 +539,7 @@ func (cb *ContextBuilder) BuildMessages(
538539
})
539540

540541
// Log preview of system prompt (avoid logging huge content)
541-
preview := fullSystemPrompt
542-
if len(preview) > 500 {
543-
preview = preview[:500] + "... (truncated)"
544-
}
542+
preview := utils.Truncate(fullSystemPrompt, 500)
545543
logger.DebugCF("agent", "System prompt preview",
546544
map[string]any{
547545
"preview": preview,

pkg/utils/string.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ package utils
22

33
import (
44
"strings"
5+
"sync/atomic"
56
"unicode"
67
)
78

9+
// Global variable to disable truncation
10+
var disableTruncation atomic.Bool
11+
12+
// SetDisableTruncation globally enables or disables string truncation
13+
func SetDisableTruncation(enabled bool) {
14+
disableTruncation.Store(enabled)
15+
}
16+
817
// SanitizeMessageContent removes Unicode control characters, format characters (RTL overrides,
918
// zero-width characters), and other non-graphic characters that could confuse an LLM
1019
// or cause display issues in the agent UI.
@@ -30,6 +39,10 @@ func SanitizeMessageContent(input string) string {
3039
// Handles multi-byte Unicode characters properly.
3140
// If the string is truncated, "..." is appended to indicate truncation.
3241
func Truncate(s string, maxLen int) string {
42+
// If the no-truncate flag is active, it returns the full string
43+
if disableTruncation.Load() {
44+
return s
45+
}
3346
if maxLen <= 0 {
3447
return ""
3548
}

0 commit comments

Comments
 (0)