Skip to content

refactor(adk): extract model timeout middleware - #1095

Merged
shentongmartin merged 2 commits into
alpha/10from
fix/session_events
Jun 22, 2026
Merged

refactor(adk): extract model timeout middleware#1095
shentongmartin merged 2 commits into
alpha/10from
fix/session_events

Conversation

@shentongmartin

Copy link
Copy Markdown
Contributor

Model Timeout Middleware and Session Event Cleanup

Problem

TypedChatModelAgent carried model timeout as a first-class config, which made timeout behavior harder to compose with other model wrappers. The stream timeout wrapper also proxied the upstream stream through its own goroutine, so it consumed stream data independently of wrappers such as eventSenderModelWrapper.

The ADK test layout also had several narrow extra test files that duplicated helpers and made related behavior harder to scan.

Solution

Model timeout is now a standalone adk/middlewares/modeltimeout package. Users opt in with modeltimeout.New or modeltimeout.NewTyped, and the timeout package owns Config, Error, and Phase.

Stream timeout enforcement now uses schema.StreamReaderWithConvert, so timeout checks run on the normal downstream Recv path instead of a separate draining goroutine. The ADK package observes timeout retry/span metadata through small error interfaces, so it does not import the middleware package back.

Session event cleanup also renames the user interrupt event to cancel, verifies tool span end emission, and folds scattered ADK tests into their canonical test files.

Key Insight

Timeout middleware should be a normal model wrapper, not a special agent config. For streaming, a timeout wrapper that consumes the stream in its own goroutine changes wrapper ordering semantics; attaching timeout checks to StreamReaderWithConvert keeps stream ownership with the caller and allows wrappers to compose predictably.

Summary

Problem Solution
Model timeout was special-cased on TypedChatModelAgent Move timeout into adk/middlewares/modeltimeout
Stream timeout wrapper consumed streams independently Use StreamReaderWithConvert on the downstream read path
ADK would import timeout middleware if it inspected concrete errors Use narrow error interfaces for retry and span metadata
Scattered ADK tests duplicated local helpers Merge related tests into canonical test files

Model Timeout 中间件与 Session Event 清理

Problem

TypedChatModelAgent 将 model timeout 作为一等配置,导致 timeout 行为难以像普通 middleware 一样与其他 model wrapper 组合。原先的 stream timeout wrapper 还会通过独立 goroutine 代理 upstream stream,因此会独立消费 stream 数据,影响 eventSenderModelWrapper 这类 wrapper 的组合语义。

ADK 测试文件也存在多个细分 extra test 文件,部分 helper 重复,相关行为不够集中。

Solution

Model timeout 现在迁移到独立的 adk/middlewares/modeltimeout package。用户通过 modeltimeout.Newmodeltimeout.NewTyped 显式启用,ConfigErrorPhase 也由该 package 持有。

Stream timeout 现在基于 schema.StreamReaderWithConvert 实现,timeout 检查发生在正常的 downstream Recv 路径上,不再使用额外 goroutine 主动 drain stream。adk package 通过小型 error interface 读取 retry/span 所需的 timeout 信息,避免反向 import middleware package。

同时将 user interrupt event 名称改为 cancel,补充 tool span end emission 验证,并把分散的 ADK 测试合并到对应的 canonical test 文件中。

Key Insight

Timeout middleware 应该是普通 model wrapper,而不是 agent 的特殊配置。对于 streaming,如果 timeout wrapper 自己用 goroutine 消费 stream,会改变 wrapper 的组合语义;把 timeout 检查挂到 StreamReaderWithConvert 的 downstream read path 上,可以保持 stream ownership 清晰,让多个 wrapper 可预测地组合。

Summary

Problem Solution
Model timeout 被特殊内置在 TypedChatModelAgent 迁移到 adk/middlewares/modeltimeout
Stream timeout wrapper 独立消费 stream 改为使用 StreamReaderWithConvert 的 downstream read path
adk 若直接识别 timeout concrete error 会产生 import cycle 通过小型 error interface 读取 retry/span metadata
ADK 测试分散且 helper 重复 合并到 canonical test 文件

Change-Id: I2f796eb73bb811e1c3224e457e5bf0eb77ec9b81
Comment thread adk/prebuilt/deep/deep.go
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.28571% with 11 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (alpha/10@8e22a2d). Learn more about missing BASE report.

Files with missing lines Patch % Lines
adk/middlewares/modeltimeout/timeout.go 71.79% 10 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             alpha/10    #1095   +/-   ##
===========================================
  Coverage            ?   81.56%           
===========================================
  Files               ?      187           
  Lines               ?    30545           
  Branches            ?        0           
===========================================
  Hits                ?    24915           
  Misses              ?     3839           
  Partials            ?     1791           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Change-Id: I0930b9d49ae447cd099000435dcb579eeaaf5a6a
@shentongmartin
shentongmartin merged commit 3afa94e into alpha/10 Jun 22, 2026
16 checks passed
@shentongmartin
shentongmartin deleted the fix/session_events branch June 22, 2026 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant