diff --git a/pkg/agent/core.go b/pkg/agent/core.go index 0431352..fb82793 100644 --- a/pkg/agent/core.go +++ b/pkg/agent/core.go @@ -217,6 +217,7 @@ type agent struct { cwd string startupMessages []string currentAction atomic.Pointer[actionHandle] + toolCallCount atomic.Int64 fifo PromptFIFO pendingInject atomic.Pointer[string] mu sync.RWMutex @@ -231,6 +232,13 @@ type agent struct { auditLog *olog.Logger } +// ToolCallCount returns the total number of tool calls executed in this +// session. The counter is monotonically increasing and never resets. +// Blocked calls (pre-tool hook exit 2) are not counted. +func (s *agent) ToolCallCount() int64 { + return s.toolCallCount.Load() +} + // SetEnv stores a session-scoped variable and propagates it to the execute server. func (s *agent) SetEnv(key, value string) { s.envMu.Lock() @@ -916,6 +924,9 @@ func (s *agent) executeTurn(ctx context.Context, input string, handler EventHand "result": result, }, s.log) } + s.cfg.IncrToolCallCount = func() int64 { + return s.toolCallCount.Add(1) + } s.cfg.SaveSession = func() { s.saveSession() } s.cfg.AutoCompact = func(ctx context.Context) { if ctx.Err() != nil || s.session == nil { diff --git a/pkg/agent/types.go b/pkg/agent/types.go index 8531306..250f744 100644 --- a/pkg/agent/types.go +++ b/pkg/agent/types.go @@ -121,6 +121,14 @@ type Core interface { // Supported fields: WatchState, WatchUsage, WatchCtxSz, WatchCWD. WaitChange(ctx context.Context, field, current string) (string, bool) + // ToolCallCount returns the total number of tool calls executed in this + // session since the agent was created. The counter is monotonically + // increasing and never resets. Blocked calls (pre-tool hook exit 2) are + // not counted. Timed-out or erroring calls are counted because execution + // was attempted. Use modulo arithmetic in hooks to fire every N calls: + // [ $(($(cat tcct) % 10)) -eq 0 ] && ... + ToolCallCount() int64 + // Close releases resources associated with the session, including its // temporary directory under /tmp/ollie/. Close()