* feat(cli): add workspace, config-dir, tools, and skills override flags Add --workspace, --config-dir, --tools, and --skills flags to `picoclaw agent` for single-shot invocations. Supports workspace override with bootstrap file injection (AGENTS.md, IDENTITY.md, SOUL.md, USER.md), tool allowlisting, and skills filtering. Also extracts FormatSkillsSummary from SkillsLoader for reuse, adds SetSkillsFilter to ContextBuilder, and wires SkillsFilter from agent config into the context builder. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(channels): add MagicForm webhook channel plugin Implement MagicForm channel for webhook-based agentic task delegation. Supports Bearer token auth, async processing with HTTP callback, workspace path validation against configurable workspace_root, bootstrap file injection, tool/skill filtering via metadata, and per-conversation session key isolation. Includes Bus() accessor on BaseChannel for direct message publishing, MagicFormConfig with workspace_root for path traversal prevention, request body size limits, and TTL cleanup for stale request contexts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(agent): support per-request workspace, tool, and skill overrides Add workspace override, tool allowlisting, and skills filtering to the agent loop via processOptions metadata. When a workspace override is active (e.g. from MagicForm channel), creates isolated SessionManager and ContextBuilder instances per request. Threads effSessions/effContextBuilder through all downstream paths including runLLMIteration, forceCompression, maybeSummarize, and summarizeSession to ensure full workspace isolation. Adds defense-in- depth tool execution guard alongside LLM-facing tool definition filter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(auth): add response body size limits to OAuth HTTP calls Wrap all io.ReadAll(resp.Body) calls in the OAuth flow with io.LimitReader capped at 1 MB to prevent memory exhaustion from malicious or unexpectedly large server responses. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(tools): add response body size limits to search providers Add 2 MB LimitReader to BraveSearch, Tavily, DuckDuckGo, and Perplexity search providers to prevent memory exhaustion from unexpectedly large responses. Matches existing pattern used by GLMSearchProvider. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(tools): add 20 MB file write size limit Reject writes exceeding 20 MB in WriteFileTool to prevent disk exhaustion from unexpectedly large LLM-generated file content. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(tools): use crypto/rand for temp file naming Replace predictable PID+nanosecond temp file names with cryptographically random hex strings to prevent symlink pre-placement attacks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): validate GitHub repo format and limit download size Add regex validation for 'owner/repo' format to prevent URL injection in InstallFromGitHub. Add 5 MB LimitReader on skill file downloads to prevent memory exhaustion. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(auth): allow OAuth credentials to be overridden via env vars Add PICOCLAW_OPENAI_CLIENT_ID, PICOCLAW_GOOGLE_CLIENT_ID, and PICOCLAW_GOOGLE_CLIENT_SECRET env var overrides. Hardcoded values remain as fallback defaults for backward compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(auth): sanitize OAuth error messages Replace raw HTTP response bodies in error messages with generic status-code-only messages. Full response details are logged at debug level for troubleshooting. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(launcher): HTML-escape error messages in auth callback Use html.EscapeString on all error text rendered in HTML responses to prevent XSS via crafted OAuth error parameters or error messages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(launcher): remove internal details from HTTP error responses Log detailed errors server-side and return generic messages to clients to prevent information disclosure of file paths and internal state. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(launcher): add HTTP security headers middleware Add SecurityHeaders middleware setting X-Content-Type-Options, X-Frame-Options, and Content-Security-Policy on all responses to mitigate MIME sniffing, clickjacking, and content injection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): validate ClawHub registry base URL scheme Reject non-HTTPS registry URLs (unless localhost) to prevent config-based redirection to malicious skill servers. Falls back to the default https://clawhub.ai with a warning. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): prevent workspace skills from shadowing builtins Builtin skill names are now reserved — workspace and global skills with the same name as a builtin are skipped with a warning log. LoadSkill also checks builtins first. This prevents supply chain attacks where a malicious skill replaces a trusted builtin. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(skills): warn when safety metadata is unavailable Add MetadataAvailable flag to InstallResult. When the registry metadata fetch fails (silent fallback), the user now sees a warning that safety checks could not be completed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * security(shell): add opt-in environment variable filtering When filter_env is enabled in config, shell commands run with a minimal allowlist of environment variables (PATH, HOME, LANG, TERM, etc. plus PICOCLAW_* prefix), preventing accidental leakage of sensitive env vars like API keys to spawned processes. Disabled by default for backward compat. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(config): add workspace-local config.json overlay for per-tenant isolation Support per-workspace config overrides via --config-dir (CLI) or configDir (webhook), enabling different API keys, models, and agent settings per tenant. - Add LoadWorkspaceConfig/MergeWorkspaceConfig with raw JSON overlay to preserve unmentioned bool fields (prevents clobbering tool enabled flags) - Add Config.Clone() for safe per-request config copies in gateway mode - Gateway: per-request provider creation from workspace config, with effProvider/effModel threaded through all LLM call sites and summarization - CLI: workspace config merged before provider creation, CLI flags win - MagicForm: replace inline bootstrap fields with configDir path; agent loop copies bootstrap files and loads config.json from configDir - Fix --session flag: format as agent:main:cli:{key} so router honors it - Fix cron session key: format as agent:main:cron:{id} for isolation - Add shared CopyBootstrapFiles helper in pkg/agent/bootstrap.go - Add config/workspace.config.example.json Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(agent): honor workspace config overrides in model routing selectCandidates uses agent.Model/Candidates baked in at startup, which ignores per-request workspace config overrides (effProvider/effModel). When the workspace overrides the provider, skip routing and fallback candidates entirely to avoid cross-provider credential issues. When only the model is overridden, allow routing but use the effective model. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style: fix linter errors (gci, gofumpt, golines, govet, unused) - gci: alphabetize magicform import in gateway/helpers.go - gofumpt: reformat multi-line function signature in agent/helpers.go - golines: wrap lines exceeding 120 chars across 5 files - govet: rename shadowed variables (err → wcErr, s → trimmed) - unused: remove maybeSummarize, forceCompression, summarizeSession wrappers superseded by their *With variants Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: nuestraai <nuestraai@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: admin-mf <admin@magicform.ai>
750 lines
24 KiB
Go
750 lines
24 KiB
Go
package agent
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/logger"
|
|
"github.com/sipeed/picoclaw/pkg/providers"
|
|
"github.com/sipeed/picoclaw/pkg/skills"
|
|
)
|
|
|
|
type ContextBuilder struct {
|
|
workspace string
|
|
skillsLoader *skills.SkillsLoader
|
|
memory *MemoryStore
|
|
|
|
// Cache for system prompt to avoid rebuilding on every call.
|
|
// This fixes issue #607: repeated reprocessing of the entire context.
|
|
// The cache auto-invalidates when workspace source files change (mtime check).
|
|
systemPromptMutex sync.RWMutex
|
|
cachedSystemPrompt string
|
|
cachedAt time.Time // max observed mtime across tracked paths at cache build time
|
|
|
|
// existedAtCache tracks which source file paths existed the last time the
|
|
// cache was built. This lets sourceFilesChanged detect files that are newly
|
|
// created (didn't exist at cache time, now exist) or deleted (existed at
|
|
// cache time, now gone) — both of which should trigger a cache rebuild.
|
|
existedAtCache map[string]bool
|
|
|
|
// skillFilesAtCache snapshots the skill tree file set and mtimes at cache
|
|
// build time. This catches nested file creations/deletions/mtime changes
|
|
// that may not update the top-level skill root directory mtime.
|
|
skillFilesAtCache map[string]time.Time
|
|
|
|
// skillsFilter limits which skills are included in the system prompt.
|
|
// When non-empty, only skills whose name matches an entry are included.
|
|
// ["*"] means include all skills.
|
|
skillsFilter []string
|
|
}
|
|
|
|
// SetSkillsFilter sets the skills filter on the context builder.
|
|
// When non-empty, only skills matching these names are included in the system prompt.
|
|
// Use ["*"] to include all skills.
|
|
func (cb *ContextBuilder) SetSkillsFilter(filter []string) {
|
|
cb.skillsFilter = filter
|
|
cb.InvalidateCache()
|
|
}
|
|
|
|
// buildFilteredSkillsSummary returns the skills summary, filtered by skillsFilter
|
|
// if one is set. When no filter is set, all skills are included.
|
|
func (cb *ContextBuilder) buildFilteredSkillsSummary() string {
|
|
if len(cb.skillsFilter) == 0 {
|
|
return cb.skillsLoader.BuildSkillsSummary()
|
|
}
|
|
|
|
// Check for wildcard
|
|
for _, f := range cb.skillsFilter {
|
|
if f == "*" {
|
|
return cb.skillsLoader.BuildSkillsSummary()
|
|
}
|
|
}
|
|
|
|
// Build filter set
|
|
filterSet := make(map[string]bool, len(cb.skillsFilter))
|
|
for _, f := range cb.skillsFilter {
|
|
filterSet[f] = true
|
|
}
|
|
|
|
allSkills := cb.skillsLoader.ListSkills()
|
|
var filtered []skills.SkillInfo
|
|
for _, s := range allSkills {
|
|
if filterSet[s.Name] {
|
|
filtered = append(filtered, s)
|
|
}
|
|
}
|
|
|
|
if len(filtered) == 0 {
|
|
return ""
|
|
}
|
|
|
|
return skills.FormatSkillsSummary(filtered)
|
|
}
|
|
|
|
func getGlobalConfigDir() string {
|
|
if home := os.Getenv("PICOCLAW_HOME"); home != "" {
|
|
return home
|
|
}
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return filepath.Join(home, ".picoclaw")
|
|
}
|
|
|
|
func NewContextBuilder(workspace string) *ContextBuilder {
|
|
// builtin skills: skills directory in current project
|
|
// Use the skills/ directory under the current working directory
|
|
builtinSkillsDir := strings.TrimSpace(os.Getenv("PICOCLAW_BUILTIN_SKILLS"))
|
|
if builtinSkillsDir == "" {
|
|
wd, _ := os.Getwd()
|
|
builtinSkillsDir = filepath.Join(wd, "skills")
|
|
}
|
|
globalSkillsDir := filepath.Join(getGlobalConfigDir(), "skills")
|
|
|
|
return &ContextBuilder{
|
|
workspace: workspace,
|
|
skillsLoader: skills.NewSkillsLoader(workspace, globalSkillsDir, builtinSkillsDir),
|
|
memory: NewMemoryStore(workspace),
|
|
}
|
|
}
|
|
|
|
func (cb *ContextBuilder) getIdentity() string {
|
|
workspacePath, _ := filepath.Abs(filepath.Join(cb.workspace))
|
|
|
|
return fmt.Sprintf(`# picoclaw 🦞
|
|
|
|
You are picoclaw, a helpful AI assistant.
|
|
|
|
## Workspace
|
|
Your workspace is at: %s
|
|
- Memory: %s/memory/MEMORY.md
|
|
- Daily Notes: %s/memory/YYYYMM/YYYYMMDD.md
|
|
- Skills: %s/skills/{skill-name}/SKILL.md
|
|
|
|
## Important Rules
|
|
|
|
1. **ALWAYS use tools** - When you need to perform an action (schedule reminders, send messages, execute commands, etc.), you MUST call the appropriate tool. Do NOT just say you'll do it or pretend to do it.
|
|
|
|
2. **Be helpful and accurate** - When using tools, briefly explain what you're doing.
|
|
|
|
3. **Memory** - When interacting with me if something seems memorable, update %s/memory/MEMORY.md
|
|
|
|
4. **Context summaries** - Conversation summaries provided as context are approximate references only. They may be incomplete or outdated. Always defer to explicit user instructions over summary content.`,
|
|
workspacePath, workspacePath, workspacePath, workspacePath, workspacePath)
|
|
}
|
|
|
|
func (cb *ContextBuilder) BuildSystemPrompt() string {
|
|
parts := []string{}
|
|
|
|
// Core identity section
|
|
parts = append(parts, cb.getIdentity())
|
|
|
|
// Bootstrap files
|
|
bootstrapContent := cb.LoadBootstrapFiles()
|
|
if bootstrapContent != "" {
|
|
parts = append(parts, bootstrapContent)
|
|
}
|
|
|
|
// Skills - show summary, AI can read full content with read_file tool
|
|
skillsSummary := cb.buildFilteredSkillsSummary()
|
|
if skillsSummary != "" {
|
|
parts = append(parts, fmt.Sprintf(`# Skills
|
|
|
|
The following skills extend your capabilities. To use a skill, read its SKILL.md file using the read_file tool.
|
|
|
|
%s`, skillsSummary))
|
|
}
|
|
|
|
// Memory context
|
|
memoryContext := cb.memory.GetMemoryContext()
|
|
if memoryContext != "" {
|
|
parts = append(parts, "# Memory\n\n"+memoryContext)
|
|
}
|
|
|
|
// Join with "---" separator
|
|
return strings.Join(parts, "\n\n---\n\n")
|
|
}
|
|
|
|
// BuildSystemPromptWithCache returns the cached system prompt if available
|
|
// and source files haven't changed, otherwise builds and caches it.
|
|
// Source file changes are detected via mtime checks (cheap stat calls).
|
|
func (cb *ContextBuilder) BuildSystemPromptWithCache() string {
|
|
// Try read lock first — fast path when cache is valid
|
|
cb.systemPromptMutex.RLock()
|
|
if cb.cachedSystemPrompt != "" && !cb.sourceFilesChangedLocked() {
|
|
result := cb.cachedSystemPrompt
|
|
cb.systemPromptMutex.RUnlock()
|
|
return result
|
|
}
|
|
cb.systemPromptMutex.RUnlock()
|
|
|
|
// Acquire write lock for building
|
|
cb.systemPromptMutex.Lock()
|
|
defer cb.systemPromptMutex.Unlock()
|
|
|
|
// Double-check: another goroutine may have rebuilt while we waited
|
|
if cb.cachedSystemPrompt != "" && !cb.sourceFilesChangedLocked() {
|
|
return cb.cachedSystemPrompt
|
|
}
|
|
|
|
// Snapshot the baseline (existence + max mtime) BEFORE building the prompt.
|
|
// This way cachedAt reflects the pre-build state: if a file is modified
|
|
// during BuildSystemPrompt, its new mtime will be > baseline.maxMtime,
|
|
// so the next sourceFilesChangedLocked check will correctly trigger a
|
|
// rebuild. The alternative (baseline after build) risks caching stale
|
|
// content with a too-new baseline, making the staleness invisible.
|
|
baseline := cb.buildCacheBaseline()
|
|
prompt := cb.BuildSystemPrompt()
|
|
cb.cachedSystemPrompt = prompt
|
|
cb.cachedAt = baseline.maxMtime
|
|
cb.existedAtCache = baseline.existed
|
|
cb.skillFilesAtCache = baseline.skillFiles
|
|
|
|
logger.DebugCF("agent", "System prompt cached",
|
|
map[string]any{
|
|
"length": len(prompt),
|
|
})
|
|
|
|
return prompt
|
|
}
|
|
|
|
// InvalidateCache clears the cached system prompt.
|
|
// Normally not needed because the cache auto-invalidates via mtime checks,
|
|
// but this is useful for tests or explicit reload commands.
|
|
func (cb *ContextBuilder) InvalidateCache() {
|
|
cb.systemPromptMutex.Lock()
|
|
defer cb.systemPromptMutex.Unlock()
|
|
|
|
cb.cachedSystemPrompt = ""
|
|
cb.cachedAt = time.Time{}
|
|
cb.existedAtCache = nil
|
|
cb.skillFilesAtCache = nil
|
|
|
|
logger.DebugCF("agent", "System prompt cache invalidated", nil)
|
|
}
|
|
|
|
// sourcePaths returns non-skill workspace source files tracked for cache
|
|
// invalidation (bootstrap files + memory). Skill roots are handled separately
|
|
// because they require both directory-level and recursive file-level checks.
|
|
func (cb *ContextBuilder) sourcePaths() []string {
|
|
return []string{
|
|
filepath.Join(cb.workspace, "AGENTS.md"),
|
|
filepath.Join(cb.workspace, "SOUL.md"),
|
|
filepath.Join(cb.workspace, "USER.md"),
|
|
filepath.Join(cb.workspace, "IDENTITY.md"),
|
|
filepath.Join(cb.workspace, "memory", "MEMORY.md"),
|
|
}
|
|
}
|
|
|
|
// skillRoots returns all skill root directories that can affect
|
|
// BuildSkillsSummary output (workspace/global/builtin).
|
|
func (cb *ContextBuilder) skillRoots() []string {
|
|
if cb.skillsLoader == nil {
|
|
return []string{filepath.Join(cb.workspace, "skills")}
|
|
}
|
|
|
|
roots := cb.skillsLoader.SkillRoots()
|
|
if len(roots) == 0 {
|
|
return []string{filepath.Join(cb.workspace, "skills")}
|
|
}
|
|
return roots
|
|
}
|
|
|
|
// cacheBaseline holds the file existence snapshot and the latest observed
|
|
// mtime across all tracked paths. Used as the cache reference point.
|
|
type cacheBaseline struct {
|
|
existed map[string]bool
|
|
skillFiles map[string]time.Time
|
|
maxMtime time.Time
|
|
}
|
|
|
|
// buildCacheBaseline records which tracked paths currently exist and computes
|
|
// the latest mtime across all tracked files + skills directory contents.
|
|
// Called under write lock when the cache is built.
|
|
func (cb *ContextBuilder) buildCacheBaseline() cacheBaseline {
|
|
skillRoots := cb.skillRoots()
|
|
|
|
// All paths whose existence we track: source files + all skill roots.
|
|
allPaths := append(cb.sourcePaths(), skillRoots...)
|
|
|
|
existed := make(map[string]bool, len(allPaths))
|
|
skillFiles := make(map[string]time.Time)
|
|
var maxMtime time.Time
|
|
|
|
for _, p := range allPaths {
|
|
info, err := os.Stat(p)
|
|
existed[p] = err == nil
|
|
if err == nil && info.ModTime().After(maxMtime) {
|
|
maxMtime = info.ModTime()
|
|
}
|
|
}
|
|
|
|
// Walk all skill roots recursively to snapshot skill files and mtimes.
|
|
// Use os.Stat (not d.Info) for consistency with sourceFilesChanged checks.
|
|
for _, root := range skillRoots {
|
|
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, walkErr error) error {
|
|
if walkErr == nil && !d.IsDir() {
|
|
if info, err := os.Stat(path); err == nil {
|
|
skillFiles[path] = info.ModTime()
|
|
if info.ModTime().After(maxMtime) {
|
|
maxMtime = info.ModTime()
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// If no tracked files exist yet (empty workspace), maxMtime is zero.
|
|
// Use a very old non-zero time so that:
|
|
// 1. cachedAt.IsZero() won't trigger perpetual rebuilds.
|
|
// 2. Any real file created afterwards has mtime > cachedAt, so it
|
|
// will be detected by fileChangedSince (unlike time.Now() which
|
|
// could race with a file whose mtime <= Now).
|
|
if maxMtime.IsZero() {
|
|
maxMtime = time.Unix(1, 0)
|
|
}
|
|
|
|
return cacheBaseline{existed: existed, skillFiles: skillFiles, maxMtime: maxMtime}
|
|
}
|
|
|
|
// sourceFilesChangedLocked checks whether any workspace source file has been
|
|
// modified, created, or deleted since the cache was last built.
|
|
//
|
|
// IMPORTANT: The caller MUST hold at least a read lock on systemPromptMutex.
|
|
// Go's sync.RWMutex is not reentrant, so this function must NOT acquire the
|
|
// lock itself (it would deadlock when called from BuildSystemPromptWithCache
|
|
// which already holds RLock or Lock).
|
|
func (cb *ContextBuilder) sourceFilesChangedLocked() bool {
|
|
if cb.cachedAt.IsZero() {
|
|
return true
|
|
}
|
|
|
|
// Check tracked source files (bootstrap + memory).
|
|
if slices.ContainsFunc(cb.sourcePaths(), cb.fileChangedSince) {
|
|
return true
|
|
}
|
|
|
|
// --- Skill roots (workspace/global/builtin) ---
|
|
//
|
|
// For each root:
|
|
// 1. Creation/deletion and root directory mtime changes are tracked by fileChangedSince.
|
|
// 2. Nested file create/delete/mtime changes are tracked by the skill file snapshot.
|
|
for _, root := range cb.skillRoots() {
|
|
if cb.fileChangedSince(root) {
|
|
return true
|
|
}
|
|
}
|
|
if skillFilesChangedSince(cb.skillRoots(), cb.skillFilesAtCache) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// fileChangedSince returns true if a tracked source file has been modified,
|
|
// newly created, or deleted since the cache was built.
|
|
//
|
|
// Four cases:
|
|
// - existed at cache time, exists now -> check mtime
|
|
// - existed at cache time, gone now -> changed (deleted)
|
|
// - absent at cache time, exists now -> changed (created)
|
|
// - absent at cache time, gone now -> no change
|
|
func (cb *ContextBuilder) fileChangedSince(path string) bool {
|
|
// Defensive: if existedAtCache was never initialized, treat as changed
|
|
// so the cache rebuilds rather than silently serving stale data.
|
|
if cb.existedAtCache == nil {
|
|
return true
|
|
}
|
|
|
|
existedBefore := cb.existedAtCache[path]
|
|
info, err := os.Stat(path)
|
|
existsNow := err == nil
|
|
|
|
if existedBefore != existsNow {
|
|
return true // file was created or deleted
|
|
}
|
|
if !existsNow {
|
|
return false // didn't exist before, doesn't exist now
|
|
}
|
|
return info.ModTime().After(cb.cachedAt)
|
|
}
|
|
|
|
// errWalkStop is a sentinel error used to stop filepath.WalkDir early.
|
|
// Using a dedicated error (instead of fs.SkipAll) makes the early-exit
|
|
// intent explicit and avoids the nilerr linter warning that would fire
|
|
// if the callback returned nil when its err parameter is non-nil.
|
|
var errWalkStop = errors.New("walk stop")
|
|
|
|
// skillFilesChangedSince compares the current recursive skill file tree
|
|
// against the cache-time snapshot. Any create/delete/mtime drift invalidates
|
|
// the cache.
|
|
func skillFilesChangedSince(skillRoots []string, filesAtCache map[string]time.Time) bool {
|
|
// Defensive: if the snapshot was never initialized, force rebuild.
|
|
if filesAtCache == nil {
|
|
return true
|
|
}
|
|
|
|
// Check cached files still exist and keep the same mtime.
|
|
for path, cachedMtime := range filesAtCache {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
// A previously tracked file disappeared (or became inaccessible):
|
|
// either way, cached skill summary may now be stale.
|
|
return true
|
|
}
|
|
if !info.ModTime().Equal(cachedMtime) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Check no new files appeared under any skill root.
|
|
changed := false
|
|
for _, root := range skillRoots {
|
|
if strings.TrimSpace(root) == "" {
|
|
continue
|
|
}
|
|
|
|
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, walkErr error) error {
|
|
if walkErr != nil {
|
|
// Treat unexpected walk errors as changed to avoid stale cache.
|
|
if !os.IsNotExist(walkErr) {
|
|
changed = true
|
|
return errWalkStop
|
|
}
|
|
return nil
|
|
}
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
if _, ok := filesAtCache[path]; !ok {
|
|
changed = true
|
|
return errWalkStop
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if changed {
|
|
return true
|
|
}
|
|
if err != nil && !errors.Is(err, errWalkStop) && !os.IsNotExist(err) {
|
|
logger.DebugCF("agent", "skills walk error", map[string]any{"error": err.Error()})
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (cb *ContextBuilder) LoadBootstrapFiles() string {
|
|
bootstrapFiles := []string{
|
|
"AGENTS.md",
|
|
"SOUL.md",
|
|
"USER.md",
|
|
"IDENTITY.md",
|
|
}
|
|
|
|
var sb strings.Builder
|
|
for _, filename := range bootstrapFiles {
|
|
filePath := filepath.Join(cb.workspace, filename)
|
|
if data, err := os.ReadFile(filePath); err == nil {
|
|
fmt.Fprintf(&sb, "## %s\n\n%s\n\n", filename, data)
|
|
}
|
|
}
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
// buildDynamicContext returns a short dynamic context string with per-request info.
|
|
// This changes every request (time, session) so it is NOT part of the cached prompt.
|
|
// LLM-side KV cache reuse is achieved by each provider adapter's native mechanism:
|
|
// - Anthropic: per-block cache_control (ephemeral) on the static SystemParts block
|
|
// - OpenAI / Codex: prompt_cache_key for prefix-based caching
|
|
//
|
|
// See: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
|
|
// See: https://platform.openai.com/docs/guides/prompt-caching
|
|
func (cb *ContextBuilder) buildDynamicContext(channel, chatID string) string {
|
|
now := time.Now().Format("2006-01-02 15:04 (Monday)")
|
|
rt := fmt.Sprintf("%s %s, Go %s", runtime.GOOS, runtime.GOARCH, runtime.Version())
|
|
|
|
var sb strings.Builder
|
|
fmt.Fprintf(&sb, "## Current Time\n%s\n\n## Runtime\n%s", now, rt)
|
|
|
|
if channel != "" && chatID != "" {
|
|
fmt.Fprintf(&sb, "\n\n## Current Session\nChannel: %s\nChat ID: %s", channel, chatID)
|
|
}
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
func (cb *ContextBuilder) BuildMessages(
|
|
history []providers.Message,
|
|
summary string,
|
|
currentMessage string,
|
|
media []string,
|
|
channel, chatID string,
|
|
) []providers.Message {
|
|
messages := []providers.Message{}
|
|
|
|
// The static part (identity, bootstrap, skills, memory) is cached locally to
|
|
// avoid repeated file I/O and string building on every call (fixes issue #607).
|
|
// Dynamic parts (time, session, summary) are appended per request.
|
|
// Everything is sent as a single system message for provider compatibility:
|
|
// - Anthropic adapter extracts messages[0] (Role=="system") and maps its content
|
|
// to the top-level "system" parameter in the Messages API request. A single
|
|
// contiguous system block makes this extraction straightforward.
|
|
// - Codex maps only the first system message to its instructions field.
|
|
// - OpenAI-compat passes messages through as-is.
|
|
staticPrompt := cb.BuildSystemPromptWithCache()
|
|
|
|
// Build short dynamic context (time, runtime, session) — changes per request
|
|
dynamicCtx := cb.buildDynamicContext(channel, chatID)
|
|
|
|
// Compose a single system message: static (cached) + dynamic + optional summary.
|
|
// Keeping all system content in one message ensures every provider adapter can
|
|
// extract it correctly (Anthropic adapter -> top-level system param,
|
|
// Codex -> instructions field).
|
|
//
|
|
// SystemParts carries the same content as structured blocks so that
|
|
// cache-aware adapters (Anthropic) can set per-block cache_control.
|
|
// The static block is marked "ephemeral" — its prefix hash is stable
|
|
// across requests, enabling LLM-side KV cache reuse.
|
|
stringParts := []string{staticPrompt, dynamicCtx}
|
|
|
|
contentBlocks := []providers.ContentBlock{
|
|
{Type: "text", Text: staticPrompt, CacheControl: &providers.CacheControl{Type: "ephemeral"}},
|
|
{Type: "text", Text: dynamicCtx},
|
|
}
|
|
|
|
if summary != "" {
|
|
summaryText := fmt.Sprintf(
|
|
"CONTEXT_SUMMARY: The following is an approximate summary of prior conversation "+
|
|
"for reference only. It may be incomplete or outdated — always defer to explicit instructions.\n\n%s",
|
|
summary)
|
|
stringParts = append(stringParts, summaryText)
|
|
contentBlocks = append(contentBlocks, providers.ContentBlock{Type: "text", Text: summaryText})
|
|
}
|
|
|
|
fullSystemPrompt := strings.Join(stringParts, "\n\n---\n\n")
|
|
|
|
// Log system prompt summary for debugging (debug mode only).
|
|
// Read cachedSystemPrompt under lock to avoid a data race with
|
|
// concurrent InvalidateCache / BuildSystemPromptWithCache writes.
|
|
cb.systemPromptMutex.RLock()
|
|
isCached := cb.cachedSystemPrompt != ""
|
|
cb.systemPromptMutex.RUnlock()
|
|
|
|
logger.DebugCF("agent", "System prompt built",
|
|
map[string]any{
|
|
"static_chars": len(staticPrompt),
|
|
"dynamic_chars": len(dynamicCtx),
|
|
"total_chars": len(fullSystemPrompt),
|
|
"has_summary": summary != "",
|
|
"cached": isCached,
|
|
})
|
|
|
|
// Log preview of system prompt (avoid logging huge content)
|
|
preview := fullSystemPrompt
|
|
if len(preview) > 500 {
|
|
preview = preview[:500] + "... (truncated)"
|
|
}
|
|
logger.DebugCF("agent", "System prompt preview",
|
|
map[string]any{
|
|
"preview": preview,
|
|
})
|
|
|
|
history = sanitizeHistoryForProvider(history)
|
|
|
|
// Single system message containing all context — compatible with all providers.
|
|
// SystemParts enables cache-aware adapters to set per-block cache_control;
|
|
// Content is the concatenated fallback for adapters that don't read SystemParts.
|
|
messages = append(messages, providers.Message{
|
|
Role: "system",
|
|
Content: fullSystemPrompt,
|
|
SystemParts: contentBlocks,
|
|
})
|
|
|
|
// Add conversation history
|
|
messages = append(messages, history...)
|
|
|
|
// Add current user message
|
|
if strings.TrimSpace(currentMessage) != "" {
|
|
msg := providers.Message{
|
|
Role: "user",
|
|
Content: currentMessage,
|
|
}
|
|
if len(media) > 0 {
|
|
msg.Media = media
|
|
}
|
|
messages = append(messages, msg)
|
|
}
|
|
|
|
return messages
|
|
}
|
|
|
|
func sanitizeHistoryForProvider(history []providers.Message) []providers.Message {
|
|
if len(history) == 0 {
|
|
return history
|
|
}
|
|
|
|
sanitized := make([]providers.Message, 0, len(history))
|
|
for _, msg := range history {
|
|
switch msg.Role {
|
|
case "system":
|
|
// Drop system messages from history. BuildMessages always
|
|
// constructs its own single system message (static + dynamic +
|
|
// summary); extra system messages would break providers that
|
|
// only accept one (Anthropic, Codex).
|
|
logger.DebugCF("agent", "Dropping system message from history", map[string]any{})
|
|
continue
|
|
|
|
case "tool":
|
|
if len(sanitized) == 0 {
|
|
logger.DebugCF("agent", "Dropping orphaned leading tool message", map[string]any{})
|
|
continue
|
|
}
|
|
// Walk backwards to find the nearest assistant message,
|
|
// skipping over any preceding tool messages (multi-tool-call case).
|
|
foundAssistant := false
|
|
for i := len(sanitized) - 1; i >= 0; i-- {
|
|
if sanitized[i].Role == "tool" {
|
|
continue
|
|
}
|
|
if sanitized[i].Role == "assistant" && len(sanitized[i].ToolCalls) > 0 {
|
|
foundAssistant = true
|
|
}
|
|
break
|
|
}
|
|
if !foundAssistant {
|
|
logger.DebugCF("agent", "Dropping orphaned tool message", map[string]any{})
|
|
continue
|
|
}
|
|
sanitized = append(sanitized, msg)
|
|
|
|
case "assistant":
|
|
if len(msg.ToolCalls) > 0 {
|
|
if len(sanitized) == 0 {
|
|
logger.DebugCF("agent", "Dropping assistant tool-call turn at history start", map[string]any{})
|
|
continue
|
|
}
|
|
prev := sanitized[len(sanitized)-1]
|
|
if prev.Role != "user" && prev.Role != "tool" {
|
|
logger.DebugCF(
|
|
"agent",
|
|
"Dropping assistant tool-call turn with invalid predecessor",
|
|
map[string]any{"prev_role": prev.Role},
|
|
)
|
|
continue
|
|
}
|
|
}
|
|
sanitized = append(sanitized, msg)
|
|
|
|
default:
|
|
sanitized = append(sanitized, msg)
|
|
}
|
|
}
|
|
|
|
// Second pass: ensure every assistant message with tool_calls has matching
|
|
// tool result messages following it. This is required by strict providers
|
|
// like DeepSeek that enforce: "An assistant message with 'tool_calls' must
|
|
// be followed by tool messages responding to each 'tool_call_id'."
|
|
final := make([]providers.Message, 0, len(sanitized))
|
|
for i := 0; i < len(sanitized); i++ {
|
|
msg := sanitized[i]
|
|
if msg.Role == "assistant" && len(msg.ToolCalls) > 0 {
|
|
// Collect expected tool_call IDs
|
|
expected := make(map[string]bool, len(msg.ToolCalls))
|
|
for _, tc := range msg.ToolCalls {
|
|
expected[tc.ID] = false
|
|
}
|
|
|
|
// Check following messages for matching tool results
|
|
toolMsgCount := 0
|
|
for j := i + 1; j < len(sanitized); j++ {
|
|
if sanitized[j].Role != "tool" {
|
|
break
|
|
}
|
|
toolMsgCount++
|
|
if _, exists := expected[sanitized[j].ToolCallID]; exists {
|
|
expected[sanitized[j].ToolCallID] = true
|
|
}
|
|
}
|
|
|
|
// If any tool_call_id is missing, drop this assistant message and its partial tool messages
|
|
allFound := true
|
|
for toolCallID, found := range expected {
|
|
if !found {
|
|
allFound = false
|
|
logger.DebugCF(
|
|
"agent",
|
|
"Dropping assistant message with incomplete tool results",
|
|
map[string]any{
|
|
"missing_tool_call_id": toolCallID,
|
|
"expected_count": len(expected),
|
|
"found_count": toolMsgCount,
|
|
},
|
|
)
|
|
break
|
|
}
|
|
}
|
|
|
|
if !allFound {
|
|
// Skip this assistant message and its tool messages
|
|
i += toolMsgCount
|
|
continue
|
|
}
|
|
}
|
|
final = append(final, msg)
|
|
}
|
|
|
|
return final
|
|
}
|
|
|
|
func (cb *ContextBuilder) AddToolResult(
|
|
messages []providers.Message,
|
|
toolCallID, toolName, result string,
|
|
) []providers.Message {
|
|
messages = append(messages, providers.Message{
|
|
Role: "tool",
|
|
Content: result,
|
|
ToolCallID: toolCallID,
|
|
})
|
|
return messages
|
|
}
|
|
|
|
func (cb *ContextBuilder) AddAssistantMessage(
|
|
messages []providers.Message,
|
|
content string,
|
|
toolCalls []map[string]any,
|
|
) []providers.Message {
|
|
msg := providers.Message{
|
|
Role: "assistant",
|
|
Content: content,
|
|
}
|
|
// Always add assistant message, whether or not it has tool calls
|
|
messages = append(messages, msg)
|
|
return messages
|
|
}
|
|
|
|
// GetSkillsInfo returns information about loaded skills.
|
|
func (cb *ContextBuilder) GetSkillsInfo() map[string]any {
|
|
allSkills := cb.skillsLoader.ListSkills()
|
|
skillNames := make([]string, 0, len(allSkills))
|
|
for _, s := range allSkills {
|
|
skillNames = append(skillNames, s.Name)
|
|
}
|
|
return map[string]any{
|
|
"total": len(allSkills),
|
|
"available": len(allSkills),
|
|
"names": skillNames,
|
|
}
|
|
}
|