Skip to content

feat: add maxInvocationHistory cap and O(1) totalDuration/averageCycleTime#786

Open
kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
kai-agent-free:feat/bounded-invocation-history
Open

feat: add maxInvocationHistory cap and O(1) totalDuration/averageCycleTime#786
kai-agent-free wants to merge 1 commit intostrands-agents:mainfrom
kai-agent-free:feat/bounded-invocation-history

Conversation

@kai-agent-free
Copy link
Copy Markdown

Summary

Implements the feature requested in #785: bounded invocation history to prevent unbounded memory growth in long-lived agents.

Changes

Meter class (src/telemetry/meter.ts)

  • Added MeterConfig with optional maxInvocationHistory (default: Infinity) and onInvocationHistoryFlush callback
  • startNewInvocation() now evicts oldest entries when the cap is exceeded
  • Flush callback is fire-and-forget; errors are caught and never propagate
  • Added _totalCycleDurationMs and _totalCycleCount scalars accumulated in endCycle() — these survive eviction

AgentMetrics class

  • totalDuration now O(1) via lifetime scalar (was O(N) scan of all invocations)
  • averageCycleTime now O(1) via lifetime scalar (was O(N) scan)
  • Both fall back to array scan when constructed without scalars (backward compat)
  • toJSON() includes totalCycleDurationMs and totalCycleCount for roundtrip

AgentConfig (src/agent/agent.ts)

  • Added maxInvocationHistory?: number — passed through to Meter
  • Added onInvocationHistoryFlush? — passed through to Meter

Tests

  • All 48 existing tests pass (2 updated for new toJSON fields)
  • 5 new tests for bounded history: eviction, flush callback, lifetime metrics after eviction, default unbounded, error swallowing
  • Total: 53 tests passing

Usage

const agent = new Agent({
  model,
  maxInvocationHistory: 500,
  onInvocationHistoryFlush: async (evicted) => {
    await metricsDb.bulkInsert(evicted);
  },
});

Fully backward-compatible — both options default to preserving current behavior.

Closes #785

…eTime

- Add MeterConfig with maxInvocationHistory and onInvocationHistoryFlush
- Evict oldest entries when cap exceeded; flush callback is fire-and-forget
- O(1) totalDuration and averageCycleTime via lifetime scalars
- Add maxInvocationHistory and onInvocationHistoryFlush to AgentConfig
- Export MeterConfig and InvocationMetricsData from index
- 5 new tests for bounded history behavior
- All 53 tests pass

Closes strands-agents#785
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add maxInvocationHistory cap and flush callback to prevent unbounded Meter growth on long-lived Agents

1 participant