refactor(agent): improve code quality and restore summarization

Code review fixes:
- Use map for O(n) job lookup in cron service (was O(n²))
- Set DeleteAfterRun=true for one-time cron tasks
- Restore context compression/summarization to prevent context overflow
- Add pkg/utils/string.go with Unicode-aware Truncate function
- Simplify setupCronTool to return only CronService
- Change Chinese comments to English in context.go

Refactoring:
- Replace toolsSummary callback with SetToolsRegistry setter pattern
- This makes dependency injection clearer and easier to test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yinwm
2026-02-11 19:27:36 +08:00
parent 4bc9e2d768
commit af60ce26fc
5 changed files with 174 additions and 18 deletions

View File

@@ -149,13 +149,15 @@ func (cs *CronService) checkJobs() {
}
// Update next run times for due jobs immediately (before executing)
// Use map for O(n) lookup instead of O(n²) nested loop
dueMap := make(map[string]bool, len(dueJobs))
for _, job := range dueJobs {
dueMap[job.ID] = true
}
for i := range cs.store.Jobs {
for _, dueJob := range dueJobs {
if cs.store.Jobs[i].ID == dueJob.ID {
// Reset NextRunAtMS temporarily so we don't re-execute
cs.store.Jobs[i].State.NextRunAtMS = nil
break
}
if dueMap[cs.store.Jobs[i].ID] {
// Reset NextRunAtMS temporarily so we don't re-execute
cs.store.Jobs[i].State.NextRunAtMS = nil
}
}
@@ -325,6 +327,9 @@ func (cs *CronService) AddJob(name string, schedule CronSchedule, message string
now := time.Now().UnixMilli()
// One-time tasks (at) should be deleted after execution
deleteAfterRun := (schedule.Kind == "at")
job := CronJob{
ID: generateID(),
Name: name,
@@ -342,7 +347,7 @@ func (cs *CronService) AddJob(name string, schedule CronSchedule, message string
},
CreatedAtMS: now,
UpdatedAtMS: now,
DeleteAfterRun: false,
DeleteAfterRun: deleteAfterRun,
}
cs.store.Jobs = append(cs.store.Jobs, job)