Skip to content

[dotnet-port-fixes] Aggregate loop usage across iterations #847

Description

@github-actions

Summary

Align the Go loop harness with the LoopAgent portion of microsoft/agent-framework#7539 so NonStreamingReturnsLastResponseOnly still reports usage from every loop iteration. The loop middleware now accumulates usage across all iterations, removes per-iteration usage from the buffered final response, and reattaches the aggregated usage to the surfaced last response. I also added a regression test that exercises summed token counts and merged AdditionalCounts.

Ported .NET PRs

Upstream commit referenced: ec32e86646469585bf6d7be83b343c88bf606950 from microsoft/agent-framework. This PR ports only the LoopAgent behavior that maps to agent/harness/loop.

Breaking Changes

No. This corrects internal usage accounting for existing loop runs without changing exported Go API.

Tests and Examples

  • Ran go test ./agent/harness/loop
  • Added regression coverage in agent/harness/loop/loop_test.go for aggregated usage in last-response-only non-streaming runs
  • No example changes were needed for this internal behavior fix

Notes

This intentionally scopes the upstream port to the Go loop harness slice of microsoft/agent-framework#7539. The broader .NET PR also updated other looping components that do not map to this narrow Go change set.

Generated by .NET to Go Fixes and Test Porting Agent · gpt54 · 250.2 AIC · ⌖ 14.4 AIC · ⊞ 24.2K · ◷


Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch dotnet-port-fixes-loop-usage-aggregation-360e094fde178154.

Click here to create the pull request

To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (200 of 200 lines)
From 6812e790368f1f77b68d05b8ddfcc0e54ce36fab Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Mon, 17 Aug 2026 03:00:33 +0000
Subject: [PATCH] [dotnet-port-fixes] Aggregate loop usage across iterations

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 agent/harness/loop/loop.go      | 76 ++++++++++++++++++++++++++++++++-
 agent/harness/loop/loop_test.go | 52 ++++++++++++++++++++++
 2 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/agent/harness/loop/loop.go b/agent/harness/loop/loop.go
index 4b210176a..6624437ab 100644
--- a/agent/harness/loop/loop.go
+++ b/agent/harness/loop/loop.go
@@ -176,6 +176,7 @@ func run(cfg Config, next agent.RunFunc, ctx context.Context, messages []*messag
 		returnLastResponseOnly := cfg.NonStreamingReturnsLastResponseOnly && !stream
 		initialSession, hasSession := agent.GetOption(opts, agent.WithSession)
 		var initialSessionSnapshot []byte
+		var aggregatedUsage message.UsageDetails
 		loopCtx := &Context{
 			InitialMessages:      initialMessages,
 			Options:              slices.Clone(opts),
@@ -197,6 +198,7 @@ func run(cfg Config, next agent.RunFunc, ctx context.Context, messages []*messag
 				if update != nil {
 					resp.Update(update)
 					if returnLastResponseOnly {
+						aggregatedUsage.Add(update.Usage())
 						iterationUpdates = append(iterationUpdates, cloneResponseUpdate(update))
 					}
 				}
@@ -217,7 +219,7 @@ func run(cfg Config, next agent.RunFunc, ctx context.Context, messages []*messag
 
 			if hasPendingApprovalRequests(&resp) || loopCtx.Iteration >= maxIterations {
 				if returnLastResponseOnly {
-					if !yieldResponseUpdates(yield, iterationUpdates) {
+					if !yieldResponseUpdates(yield, responseUpdatesWithAggregatedUsage(iterationUpdates, aggregatedUsage)) {
 						return
 					}
 				}
@@ -231,7 +233,7 @@ func run(cfg Config, next agent.RunFunc, ctx context.Context, messages []*messag
 			}
 			if !ok {
 			
... (truncated)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions