fix: preserve method-level config overrides across SDK resources#16
Merged
fix: preserve method-level config overrides across SDK resources#16
Conversation
There was a problem hiding this comment.
Pull request overview
This PR ensures per-call (method-level) Config overrides are preserved and consistently applied across SDK resources and nested API calls, and refines sandbox creation timeout behavior while updating documentation to match current SDK usage.
Changes:
- Introduces explicit
readTimeoutoverride detection (hasReadTimeout) and per-API fallback helper (readTimeoutOr) inConfig. - Propagates effective config (instance + method override) through resource instance methods and internal API layers (DataAPI, ToolSet, Model, Credential, Sandbox).
- Adds/updates unit tests for timeout behavior and refreshes README examples + environment variable documentation.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unittests/utils/config.test.ts | Adds unit coverage for hasReadTimeout and readTimeoutOr semantics including merged configs. |
| tests/unittests/sandbox/control-api.test.ts | Verifies createSandbox uses 30s default unless caller explicitly sets readTimeout (instance or per-call). |
| src/utils/data-api.ts | Uses merged config for nested control client calls and request timeouts so overrides aren’t lost. |
| src/utils/config.ts | Makes _readTimeout optional internally and adds hasReadTimeout + readTimeoutOr helpers. |
| src/toolset/toolset.ts | Ensures OpenAPI/MCP tool listing/invocation uses the effective config consistently. |
| src/sandbox/template.ts | Preserves instance config when calling get() without an explicit override. |
| src/sandbox/sandbox.ts | Ensures sandbox instances created from Data API responses retain the effective config. |
| src/sandbox/api/sandbox-data.ts | Fixes merge order for config overrides and passes through per-call config for CRUD calls. |
| src/sandbox/api/control.ts | Applies per-call config merge and uses readTimeoutOr(30000) for fast-fail sandbox creation. |
| src/model/model-service.ts | Allows resource instances created from API responses to retain config and apply it by default in instance methods. |
| src/model/model-proxy.ts | Same as ModelService: preserves config through instance lifecycle methods. |
| src/model/client.ts | Constructs Model resources with both response data and effective config to preserve overrides. |
| src/credential/client.ts | Constructs Credential resources with effective config to preserve overrides. |
| src/agent-runtime/endpoint.ts | Preserves instance config when calling get() without an explicit override. |
| README.md | Updates package name/imports, examples, and environment variable documentation to match current behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
之前调用方传入的 config 在多个资源链路上都未真正生效: data-api 的 timeout/ControlAPI 仍读 this.config,sandbox-data 的 withConfigs 顺序反向导致实例 config 反向覆盖方法级 config,model 和 credential 的 client 把结果 Object.assign 到 new ModelProxy() 但从未设置 _config,endpoint/template 的实例 get 也漏掉 config ?? this._config 的 fallback。结果是用户带 config 创建资源 后再链式 update/get/delete 会回落到默认环境,行为与 AgentRuntime 等已规范模块不一致。 本次统一收敛 method > instance > default 的合并优先级,让所有 请求真正遵循调用方传入的 config: - src/utils/data-api.ts: makeRequest/postFile/getFile 改用合并后 的 cfg.timeout;取 access token 的 ControlAPI 也用 cfg 构造 - src/sandbox/api/sandbox-data.ts: 反转 Config.withConfigs 入参 顺序;createSandbox/deleteSandbox/stopSandbox/getSandbox 在 refreshAccessToken 之后把 params.config 透传给后续 HTTP 调用 - src/model/client.ts: create/delete/update/get/list 10 处把 Object.assign(new X(), result) 替换为 new X(result, cfg) - src/model/model-proxy.ts, src/model/model-service.ts: 构造函数 签名扩展为 (data?, config?),保持无参兼容;实例 update/delete/ get 补 config ?? this._config fallback - src/credential/client.ts: new Credential(result) 全部改为 new Credential(result, cfg) - src/agent-runtime/endpoint.ts, src/sandbox/template.ts: 实例 get 补 config ?? this._config fallback - src/sandbox/sandbox.ts: Sandbox.get 静态方法把合并后的 cfg 传给 fromInnerObject 和各模板子类构造,避免返回的 Sandbox 丢失 config - src/toolset/toolset.ts: toApiSet/callToolAsync/listToolsAsync 统一以 params?.config ?? this._config 作为 baseConfig 修复后线上行为从"偷偷回落到默认环境"变为"真正遵循方法级 config", 原本被资源 client 静默吞掉的 endpoint/token/headers 现在都会按 预期生效。 Change-Id: If9961fabaa0830aa1d536e09c4b04b74ee97bcc1 Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: OhYee <oyohyee@oyohyee.com>
createSandbox 之前在 RuntimeOptions 里写死 readTimeout: 30000,让
用户即使在 Config 上显式配置了 readTimeout 也无法生效。直接删掉硬
编码会让默认从 30s 变成 100000000ms,创建卡死时要等近 28 小时才
报错,体验更差。需要的是"软默认":未显式配置时维持 30s 快速失败,
显式配置时尊重用户。
为此让 Config 区分"用户未设置"与"使用默认值",让具体 API 能定义
自己的默认 fallback:
- src/utils/config.ts: 内部 _readTimeout 由 number 改为
number | undefined,构造时不再强制填默认;update() 合并改用
!== undefined 判断以支持显式 0;公开 getter readTimeout 仍返回
_readTimeout ?? 100000000,向后兼容;新增 hasReadTimeout 和
readTimeoutOr(fallback) 供 API 层定义专属默认
- src/sandbox/api/control.ts: createSandbox 改为
Config.withConfigs(this.config, config) 后用
cfg.readTimeoutOr(30000),并把 cfg 传给 getClient 保持透传一致
测试同步覆盖新行为:
- tests/unittests/utils/config.test.ts: 新增 6 个 readTimeoutOr/
hasReadTimeout 用例,包括 withConfigs 合并下的传递行为
- tests/unittests/sandbox/control-api.test.ts: 新增 4 个用例验证
createSandbox 默认 30s、instance config 覆盖、method-level
config 覆盖、method 优先于 instance
修复后默认行为不变(仍是 30s 快速失败),需要长 readTimeout 的用户
通过 new Config({ readTimeout: ... }) 即可覆盖;其他读取
cfg.readTimeout 的位置默认值保持 100000000,零扩散影响。
Change-Id: I611cbe54218a3537223fdf1032cc87f1d345004f
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: OhYee <oyohyee@oyohyee.com>
之前 README 多处与实际 API/包配置不符,导致按文档照抄会直接报错或
找不到符号:包名写成 @alicloud/agentrun-sdk(实际为 @agentrun/sdk);
AgentRuntime/Endpoint 等创建接口示例缺少 input 包装;waitUntilReady
方法不存在(实际为 waitUntilReadyOrFailed);codeConfiguration 示例
手写 zipFile 字符串不可运行;createCodeInterpreterSandbox 写成位置
参数(实际为对象参数);环境变量列了不存在的 AGENTRUN_TIMEOUT 且漏
列 SECURITY_TOKEN/CONTROL_ENDPOINT/DATA_ENDPOINT/DEVS_ENDPOINT 与
其 fallback;npm run example:quick-start 脚本指向不存在的源文件。
按 src/utils/config.ts、src/agent-runtime、src/sandbox 与 examples/
当前实现重写:
- 包名统一为 @agentrun/sdk
- Agent Runtime 示例改为 AgentRuntime.create({ input }) 并用
codeFromFile 替代手写 zipFile,等待用 waitUntilReadyOrFailed
- Sandbox 示例改用 Template.create({ input })、对象参数的
createCodeInterpreterSandbox、waitUntilRunning + waitUntilReadyOrFailed
组合,并补一段 sandbox.context.create + ctx.execute 真实可运行示例
- 模块表补齐 ToolSet / Server / Integration
- 示例小节移除失效的 example:quick-start 与 example:quick-start-with-tools
脚本,改为列出 examples/ 下真实存在的文件以及 npx tsx 直接运行方式
- 环境变量表按 Config 实际读取顺序补齐 7 项变量及其 ALIBABA_CLOUD_* /
FC_* fallback;额外说明 timeout/readTimeout 仅支持代码配置
修正后照着 README 抄写的代码可以直接通过类型检查并运行。
Change-Id: I1a65101653453bc52f87dd301e1c4d5e5b788ca7
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: OhYee <oyohyee@oyohyee.com>
上一次提交 control-api.test.ts 时未跑 prettier,仓库 format:check 检测到行宽超限。此次不改任何断言/逻辑,仅由 prettier 把过长的行 合并为单行,使 npm run format:check 重新通过。 Change-Id: Ibd76e461ffd5e1b576ce44cf4282637d0e9935f1 Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: OhYee <oyohyee@oyohyee.com>
之前在 fix(sdk): 修复方法级 config 在调用链中被悄悄丢弃中给
AgentRuntimeEndpoint.get 与 Template.get 的实例方法补了
config ?? this._config fallback,但没有相应的测试覆盖两个分支,
导致 agent-runtime 分支覆盖率从 ≥95% 跌到 93.64%、sandbox 跌到
90%,未能通过覆盖率门禁。
补两组等价的 case 分别命中两个分支:
- tests/unittests/agent-runtime/agent-runtime.test.ts: 新增
describe('get (instance)'),分别验证 endpoint.get({ config })
时透传 params.config,以及 endpoint.get() 时回落到构造时传入的
instance _config
- tests/unittests/sandbox/template.test.ts: 同构地为 Template
实例 get 增加 params.config 与 _config fallback 两个 case
补完后覆盖率:agent-runtime 分支 95.45%、sandbox 分支 100%、全量
分支 97.37%,重新跨过 95% 门槛。
Change-Id: I61ddc203253722378e83abe4eef639932bda8b64
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: OhYee <oyohyee@oyohyee.com>
3f3b4f5 to
60b53c9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
configoverrides when resource instances refresh/update/delete or call nested APIscreateSandboxhonor caller-providedreadTimeoutwhile keeping the 30s fast-fail fallbackValidation
npm installnpm run typechecknpm run buildnpm test -- --runInBand(unit tests passed; e2e suites still require real Alibaba Cloud credentials and fail withInvalidCredentialsin this environment)