Skip to content

fix(qwen): pass the complete model id to the qwen CLI - #399

Merged
zpzjzj merged 4 commits into
alibaba:mainfrom
Walternater:fix/qwen-model-id
Nov 23, 2026
Merged

zpzjzj merged 4 commits into
alibaba:mainfrom
Walternater:fix/qwen-model-id

Conversation

@Walternater

@Walternater Walternater commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Update after review: the rejoin moved out of the shared layer into the qwen_code adapter; see "Where the id is rebuilt" below.

Summary

qwen_code resolves its model against the ids registered in the qwen CLI's modelProviders. When --model zcode/GLM-5.3-Flash matches a configured provider, the credential layer splits the reference to select that provider's credentials (ZCODE_API_KEY, base URL), so the adapter used to receive the bare GLM-5.3-Flash, fail to match the registered zcode/GLM-5.3-Flash, and fall back to OPENAI_API_KEY — the failure in #383.

The split must stay: it is what picks provider-scoped credentials, and opencode relies on the same rule. What changes is who rebuilds the id and what the shared layer keeps.

Where the id is rebuilt

The credential/agent-kind layer owns credentials and protocol, not how one CLI receives a model id (#196). So it now only records what happened to the model:

  • ResolvedAgentConfig.ModelSplit is set where the legacy split occurs — applyCLIModelOverride for --model and parseJudgeModel for a judge model — cleared when a provider env var or credential file replaces the model wholesale (setResolvedValue), and inherited by a judge from the runner (applyFallback).
  • Nothing in internal/agent/capabilities.go rejoins anything: ModelPolicyJoinLegacySplit is gone and qwen_code is back to ModelPolicyPassthrough.

The adapter materializes the id it runs:

  • QwenCodeAgent.MaterializedModel() returns provider + "/" + name when ModelSplit is set, and the resolved model unchanged otherwise, so a bare name never gains a prefix it did not have.
  • -m and OPENAI_MODEL use that value.
  • The applied model in SessionResult and the skill_up.model / skill_up.model.applied attributes use the same value, via agent.ModelMaterializer. The report therefore cannot name a model the CLI was not asked for.

One implementation note: annotateSessionResult is a *BaseAgent method, and a method called on an embedded BaseAgent sees only the base — not the *QwenCodeAgent holding it. The adapter therefore gives itself to the base in its constructor (agent.inspector = agent), and the annotation asks that value for the materialized id. Engines that do not implement ModelMaterializer are unaffected.

Related issues

Changes

  • internal/credential/agent_init.go: record ModelSplit at the split sites, clear it on wholesale replacement, inherit it for a judge; drop the Qwen branch from the CLI override path.
  • internal/agent/capabilities.go: remove ModelPolicyJoinLegacySplit and its branch in resolveAppliedModel.
  • internal/agent/agent.go: Config.ModelSplit, ModelMaterializer, BaseAgent.inspector, and reporting that goes through the materialized id.
  • internal/agent/qwen_code.go: MaterializedModel, used by -m, OPENAI_MODEL.
  • internal/agent/factory.go: pass params.ModelSplit to the adapter config.
  • Docs (cli-reference, en + zh) and CHANGELOG.md.

Test plan

  • make test — full suite green; make verify — 0 issues.
  • Resolution tests cover the four shapes: CLI zcode/GLM-5.3-Flash (split recorded, provider zcode), explicit --provider openai --model qwen3-coder-plus (bare, no invented prefix), YAML model.name: zcode/GLM-5.3-Flash (opaque, no split), YAML provider: zcode + name: GLM-5.3-Flash (no split).
  • A judge inherits the runner's split and materializes the same id; a provider env or credential file clears it.
  • TestQwenCodeRun_ForwardsResolvedSlashedModel drives resolution → adapter → run and asserts that -m, OPENAI_MODEL and the reported applied model are the same value.
  • Negative control: making MaterializedModel stop rejoining fails TestResolveAdapterConfig_QwenRecordsLegacySplitForTheAdapter, TestResolveAdapterConfig_QwenJudgeInheritsLegacySplit and TestQwenCodeRun_ForwardsResolvedSlashedModel (adapter materialized model = "GLM-5.3-Flash", want "zcode/GLM-5.3-Flash").
  • Fake qwen shim recording argv, run through the built binary:
    • --model zcode/GLM-5.3-Flash → --yolo -m zcode/GLM-5.3-Flash, OPENAI_MODEL=zcode/GLM-5.3-Flash.
    • --provider openai --model qwen3-coder-plus → -m qwen3-coder-plus, OPENAI_MODEL=qwen3-coder-plus (no prefix invented).

Notes

  • I could not run the real qwen CLI (not installed here, no qwen credential), so the command line is verified against a shim, not against qwen's own modelProviders lookup. The resolution path and the adapter's output are tested directly.
  • opencode keeps slashed ids opaque as before: it never sets or uses ModelSplit.
  • A model dropped wholesale is still forwarded as written; the flag is only about the legacy split.

Disclosure

This change was prepared with AI assistance from DeepSeek Harness (deepseek-v4.1-flash), reviewed and adjusted by me before submission.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 91b238a8ab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/agent/qwen_code.go Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T06:24:36.547747Z 91b238a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Walternater
Walternater force-pushed the fix/qwen-model-id branch 2 times, most recently from 8e20a95 to cffa997 Compare November 13, 2026 07:24
@Walternater

Copy link
Copy Markdown
Contributor Author

One addition on top of the review fix: the engine name is not canonicalised in applyCLIModelOverride, so the branch covers the qwen-code and qwen aliases too, with the new test table running all three. Head is now cffa997.

zpzjzj commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks for addressing the bare-model regression. I think the Qwen-specific behavior should live at the adapter boundary rather than adding another engine-specific branch in applyCLIModelOverride.

Issue #196 separates credential/provider resolution from adapter materialization. Also, #247/#262 removed Qoder’s model allowlist; they retained legacy provider/model parsing, so they are not a precedent for changing that parsing specifically for Qwen.

Simply joining provider + "/" + model inside Qwen is insufficient, as the earlier review correctly identified. Instead, could we preserve the original model input and whether legacy splitting occurred, then let the Qwen adapter determine the value forwarded to both -m and OPENAI_MODEL? Applied-model reporting should reflect that same value.

Please also cover the full resolution-to-execution path for slash-containing IDs, explicit providers with bare IDs, YAML configuration, and judge configuration. The command-builder test alone does not exercise the parsing that caused #383.

@Walternater

Copy link
Copy Markdown
Contributor Author

Moved the Qwen behaviour to the adapter boundary as you asked — the credential layer is generic again.

What changed in b35d4b5:

  • applyCLIModelOverride no longer has a qwen_code branch. It records the reference the user wrote (ResolvedAgentConfig.ModelRef) and whether the legacy provider/name split happened (ModelSplit) around the existing ResolveModelRef call, so provider/credential resolution keeps working exactly as before.
  • qwen_code (and both aliases) declare a requested_ref model policy in CapabilitiesForEngine. resolveAppliedModel returns the requested reference when a split happened and the reference contains /; that value flows through DetectAgentWithResolvedConfig into the adapter config, so -m/OPENAI_MODEL and result.AppliedModel all use the same value.
  • No prefix join in the adapter and no prefix synthesis: --provider openai --model qwen3-coder-plus stays qwen3-coder-plus, and a YAML model: zcode/GLM-5.3-Flash stays opaque.

Tests now cover resolution through execution rather than command construction alone:

  • CLI slashed ref, all three aliases (provider zcode, model GLM-5.3-Flash, reference preserved, split recorded, ZCODE_API_KEY selected);
  • explicit provider + bare model (no invented prefix);
  • YAML slashed model opaque; YAML provider/name split keeps the reference;
  • judge config with a legacy slashed model;
  • adapter test asserts QwenCodeAgent.Cfg.ModelName == AppliedModel; run test asserts the argv/OPENAI_MODEL/result.AppliedModel agreement.

Verified with make test (all packages), make verify (0 issues), and end-to-end through a fake qwen shim that records argv: --model zcode/GLM-5.3-Flash → qwen --yolo -m 'zcode/GLM-5.3-Flash', OPENAI_MODEL=zcode/GLM-5.3-Flash; explicit-provider case → bare model, no prefix. Negative control (policy back to passthrough) fails the new tests.

PR body is updated. Still no real qwen CLI/credentials on this machine, so the reporter's exact auth failure remains unverified end-to-end.

zpzjzj commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

The adapter boundary looks right now, but I think we can simplify the state by dropping ModelRef and keeping only a flag recording an actual legacy provider/model split.

Keeping both ModelRef and Model introduces state that can drift. For example, with YAML provider zcode, model zcode/old-model, and ZCODE_MODEL=zcode/new-model, credential resolution updates Model but leaves ModelRef unchanged. The current inequality check marks this as a split, and Qwen’s policy restores zcode/old-model, silently undoing the environment override.

The legacy split is reversible, so we do not need a second model string. Please set the flag where splitting actually happens, clear it when another source replaces the model, and carry it along when inheriting the model. Qwen’s adapter materialization can reconstruct provider + "/" + model only when that flag is set, using the result consistently as AppliedModel for flags, environment, and reporting.

Explicit provider + bare model and separate YAML provider/name fields should remain unchanged. Please add regression coverage for environment overrides, including judge overrides and inherited runner models.

@Walternater

Copy link
Copy Markdown
Contributor Author

Rewritten per your review — thank you for the drift example; it is exactly the failure mode this simplification closes.

What changed relative to the adapter-boundary revision:

  • ModelRef is gone. ModelSplit is now just a flag, set at the two real split sites: the generic ResolveModelRef call in applyCLIModelOverride, and parseJudgeModel when a legacy judge model is written as one field.
  • A provider env or credential file that replaces the model wholesale clears the flag in setResolvedValue, so the replacement is forwarded as written instead of rejoining a stale reference. The runner-level drift you described (YAML provider + YAML model + ZCODE_MODEL=zcode/new-model) now resolves to zcode + zcode/new-model with the flag cleared; I also added the judge-level regression for the same shape.
  • applyFallback carries the flag with the inherited model, and a judge that inherits a split runner model keeps it (new test).
  • The qwen policy is renamed requested_ref → join_legacy_split; resolveAppliedModel rejoins provider + "/" + model only while the flag holds and the provider is known, and that joined value is what lands in the adapter config, -m, OPENAI_MODEL, and the reported applied model.

Negative controls: disabling the join turns the adapter tests red (applied = "GLM-5.3-Flash", want zcode/GLM-5.3-Flash); removing the flag-clearing turns the env override regressions red (env override must clear the legacy split). make test and make verify pass; the PR body and CHANGELOG wording are updated to the flag language.

zpzjzj commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks, the flag-only implementation in 5909fbf addresses the state-drift concern from my previous comment, and the updated PR description now matches it. I reviewed the source and the new regression tests, but have not independently rerun the tests. GitHub currently reports merge conflicts for this head and no PR-triggered CI runs; please resolve the conflicts with the base branch and rerun verification on the resulting head so we can review the final integrated version.

@Walternater

Copy link
Copy Markdown
Contributor Author

Rebased onto current main () as 44d0001 (CHANGELOG conflict resolved by keeping the four newer entries). Focused credential/agent/cli tests and make verify (0 issues) pass after the rebase; the flag-only design from the last round is unchanged.

zpzjzj commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks, the flag-only implementation addresses the state-drift issue. One part of my earlier feedback needs clarification: I meant for the reconstruction logic to live in qwen_code.go.

ModelPolicyJoinLegacySplit still puts Qwen’s model reconstruction rule in the shared resolveAppliedModel. Could we remove this new policy and move the reconstruction into a Qwen-specific config materialization function? The shared credential layer should only record, clear, and inherit ModelSplit; the shared adapter entry point can dispatch to the Qwen function.

Please use its result consistently for AppliedModel, -m, and OPENAI_MODEL, so execution and reporting remain aligned.

@zpzjzj zpzjzj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on 44d0001 to complete the adapter-boundary change. The drift fix is addressed, but ModelPolicyJoinLegacySplit still reconstructs Qwen IDs inside shared resolveAppliedModel. Please move reconstruction to Qwen-specific materialization in qwen_code.go, leaving the credential layer responsible only for recording/clearing/inheriting ModelSplit, and use the materialized result consistently for reporting, -m, and OPENAI_MODEL.

…licy

`ModelPolicyJoinLegacySplit` rebuilt `provider/name` inside the shared
`resolveAppliedModel`, so a rule about how one CLI receives a model id lived in
the credential/agent-kind layer that alibaba#196 keeps for resolving credentials and
protocol. It also meant the value the policy produced, the value the adapter
passed to `-m`, and what the report called applied could drift.

The shared layer now only records what happened: the credential layer splits a
legacy `provider/name` to select provider-scoped credentials and sets
`ModelSplit`, clears it when a provider env or credential file replaces the
model wholesale, and lets a judge inherit it from the runner. `Config` carries
the flag to the adapter and nothing rejoins it there.

The adapter materializes the id. `QwenCodeAgent.MaterializedModel` rejoins
`provider + "/" + name` when the flag is set and passes everything else through
unchanged; `-m` and `OPENAI_MODEL` use it, and so does the applied model in
`SessionResult` and the observability attributes, through the new
`agent.ModelMaterializer` interface. An embedded `BaseAgent` cannot see the
adapter holding it, so the adapter hands itself to the base in its constructor.

Tests cover the four shapes end to end (CLI slashed, explicit provider with a
bare name, YAML slashed, YAML provider plus name), the judge inheriting the
runner's split, and one run asserting that `-m`, `OPENAI_MODEL` and the reported
model are the same value. Disabling the rejoin fails all three.
@Walternater

Copy link
Copy Markdown
Contributor Author

Moved the rejoin into the adapter in 6a76ff8, and ModelPolicyJoinLegacySplit is gone from the shared layer.

Shared layer: records only. ResolvedAgentConfig.ModelSplit is set exactly where the legacy split happens — applyCLIModelOverride for --model, parseJudgeModel for a judge model — cleared in setResolvedValue when a provider env var or credential file replaces the model wholesale, and carried by applyFallback so a judge inherits the runner's split. Nothing there builds a model id any more; internal/agent/capabilities.go keeps the policy table for protocol/base URL and qwen_code is back to ModelPolicyPassthrough, with no branch for it in resolveAppliedModel.

Adapter: materializes. QwenCodeAgent.MaterializedModel() rejoins provider + "/" + name when ModelSplit is set and returns the resolved model unchanged otherwise — so an explicit --provider openai --model qwen3-coder-plus stays bare rather than gaining an invented prefix. -m and OPENAI_MODEL use that value, and so does what is reported: SessionResult.AppliedModel and the skill_up.model / skill_up.model.applied attributes go through a new agent.ModelMaterializer interface, so the report and the command line cannot disagree. Engines that do not implement it fall back to the resolved model exactly as before, which is why this needed no per-engine change elsewhere.

One thing worth flagging, since it forced the shape: annotateSessionResult is a *BaseAgent method, and on an embedded BaseAgent it sees only the base — a materializeTarget() override on *QwenCodeAgent was never called (base.materializeTarget type=*agent.BaseAgent). The adapter therefore passes itself to the base in its constructor and the annotation asks that value; it is one field and one assignment, and the alternative was copying every adapter's reporting method.

Tests, all end-to-end from resolution rather than command construction alone:

input resolved provider / model split materialized (reported, -m, OPENAI_MODEL)
--model zcode/GLM-5.3-Flash zcode / GLM-5.3-Flash yes zcode/GLM-5.3-Flash
--provider openai --model qwen3-coder-plus openai / qwen3-coder-plus no qwen3-coder-plus
YAML model.name: zcode/GLM-5.3-Flash — / zcode/GLM-5.3-Flash no zcode/GLM-5.3-Flash
YAML provider: zcode + name: GLM-5.3-Flash zcode / GLM-5.3-Flash no GLM-5.3-Flash

A judge inheriting the runner's split materializes the same id, and a provider env or credential file clears the flag and the replacement is forwarded as written. TestQwenCodeRun_ForwardsResolvedSlashedModel runs resolution → adapter → run and asserts -m, OPENAI_MODEL and the reported applied model are one value. Negative control: with the rejoin removed from MaterializedModel, that test and the two adapter-config tests fail (adapter materialized model = "GLM-5.3-Flash", want "zcode/GLM-5.3-Flash").

make test green, make verify 0 issues, fake qwen shim shows --yolo -m zcode/GLM-5.3-Flash with OPENAI_MODEL=zcode/GLM-5.3-Flash, and --provider openai --model qwen3-coder-plus shows -m qwen3-coder-plus with no prefix. The real qwen CLI is still unavailable here, so the final modelProviders lookup against a live qwen remains unverified — the shim proves what we hand over, not what qwen does with it.

zpzjzj commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks, the reconstruction now lives in qwen_code.go, which addresses the placement concern. I found one remaining P2 issue on 6a76ff8: run-level reporting still uses the bare model name.

With a configured zcode provider and --engine qwen_code --model zcode/GLM-5.3-Flash:

  • -m, OPENAI_MODEL, and SessionResult.AppliedModel use zcode/GLM-5.3-Flash.
  • The final report's applied_configuration.model is still GLM-5.3-Flash.

MaterializedModel() only affects the agent instance. ResolvedAgentConfig.AppliedModel remains bare, and buildReportInput calls ResolveAdapterConfig again and reads that value without consulting the adapter. I reproduced this with a temporary regression test comparing the report's applied model with the Qwen adapter's materialized model.

Could we have a Qwen-specific config materialization function produce the complete AppliedModel before execution and reporting consume it, with the shared entry point only dispatching to that function? This would keep the reconstruction rule inside Qwen while giving both paths the same applied value, and should avoid the new ModelMaterializer / BaseAgent.inspector callback mechanism. Please add regression coverage through buildReportInput or final report generation.

The existing make test suite (including race detection) and make verify pass; the additional regression test exposes the reporting gap.

@zpzjzj

zpzjzj commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Following up on the applied-model report finding: there is a second mismatch at 6a76ff8. The Qwen command and session use the full model ID, the machine-readable run report still records the bare ID, and telemetry formatting can prepend the provider to an ID that is already qualified. Please make these consumers use the same canonical effective model ID, and assert the final JSON report and emitted telemetry in addition to the command arguments. The rendered report can add a provider independently, so the report issue here is specifically the machine-readable field.

The applied model id that Qwen Code runs was assembled inside the adapter,
so the run report and the telemetry read a different value than the
command: the report recorded the bare model name and the telemetry could
prefix the provider onto an id that was already qualified.

ResolveAdapterConfig now dispatches to a Qwen-specific materialization
before the adapter is built, so the command, the session result, the
telemetry attributes and the machine-readable applied_configuration.model
all read the same complete id. The ModelMaterializer callback is gone with
it, and the report renderer no longer doubles a provider the id already
carries.

Co-Authored-By: Claude Code <[email protected]>
@Walternater

Copy link
Copy Markdown
Contributor Author

Both findings are addressed in 37ac7c2.

The applied model id is now materialized in the shared entry point. ResolveAdapterConfig dispatches to a Qwen-specific materializeQwenModel before the adapter is built, so one value reaches the command (-m, OPENAI_MODEL), the session result, the telemetry attributes and the machine-readable applied_configuration.model. The ModelMaterializer callback and BaseAgent.inspector are gone with it.

The telemetry no longer doubles the provider. formatAgentModel returns the id unchanged when it already carries the provider, and the report renderer does the same, so zcode/GLM-5.3-Flash is not rendered as zcode/zcode/GLM-5.3-Flash.

Coverage. TestQwenCodeRun_ForwardsResolvedSlashedModel now asserts the telemetry attributes alongside the command arguments, and TestBuildReportInput_MaterializesQwenModelID resolves a slashed CLI model end to end and asserts the final JSON report carries "model":"zcode/GLM-5.3-Flash" in applied_configuration.

make verify and the full go test ./... pass.

goconst flags `"zcode/GLM-5.3-Flash"` in `qwen_code_test.go`: the package
already defines `qwenSlashedModelID` in `capabilities_test.go`, so the new
assertions use it. The two `-m '…'` substring checks build the expected flag
from the same constant instead of repeating the id.
@Walternater

Copy link
Copy Markdown
Contributor Author

CI caught a lint finding in my last commit — goconst flagged "zcode/GLM-5.3-Flash" in qwen_code_test.go because the package already defines qwenSlashedModelID. 171854f uses that constant in the new assertions and builds the expected -m '...' flag from it. Test-only change.

Verified with make lint-tools + make verify: 0 issues; go test ./internal/agent/ ./internal/runner/ ./internal/report/ all ok. The one failure there (TestIndependentJudgeUsesProtocolScopedConnection) comes from an ANTHROPIC_BASE_URL exported in my shell: it fails the same way on the untouched base commit, and the suite runs clean with env -u ANTHROPIC_BASE_URL.

@zpzjzj zpzjzj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zpzjzj
zpzjzj added this pull request to the merge queue Nov 23, 2026
Merged via the queue into alibaba:main with commit 44b1565 Nov 23, 2026
19 checks passed
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.

qwen_code:带 "/" 的模型 ID 被当作 provider/model 拆分,引擎收到的是截断后的 ID

2 participants