Commit graph

105 commits

Author SHA1 Message Date
jinye
93ad0ff131
fix(serve): auth device-flow follow-up for #4255 review threads (#4291) 2026-05-19 07:01:27 +08:00
jinye
3ffe321cfd
feat(serve): MCP guardrail push events + hysteresis (#4175 Wave 3 PR 14b) (#4271) 2026-05-19 01:06:20 +08:00
jinye
6f7a48936f
feat(serve): approval / tools / init / MCP-restart mutation routes (#4175 Wave 4 PR 17) (#4282)
* feat(core): introduce TrustGateError for setApprovalMode (#4175 Wave 4 PR 17)

Adds a named subclass `TrustGateError` thrown by `Config.setApprovalMode`
when the requested mode would grant privileged tool autonomy in a folder
the user has not marked as trusted. Daemon mutation routes can now
recognize this rejection class without depending on message text.

Extends `mapDomainErrorToErrorKind` in `packages/cli/src/serve/status.ts`
to map `TrustGateError → 'auth_env_error'`. Matches by `err.name` rather
than `instanceof` because cross-package bundling can produce duplicate
class instances where `instanceof` returns false. Test covers both the
real class and a name-synthesized instance.

Foundation for the `POST /session/:id/approval-mode` route landing in a
follow-up commit in this PR.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* feat(core): add disabledTools workspace setting (#4175 Wave 4 PR 17)

Introduces a per-workspace skip-registration mechanism for tool names,
distinct from `permissions.deny` (which keeps the tool registered and
blocks invocation). Tools listed in `disabledTools` are not registered
at all and never appear in `/tools`, `getAllTools()`, or function-call
discovery — both built-ins and MCP-discovered tools flow through
`ToolRegistry.registerTool` / `registerFactory`, so gating there covers
every registration path.

- `ConfigParameters.disabledTools?: string[]` (frozen into a `ReadonlySet`
  at Config construction; queried via `Config.getDisabledTools()`)
- `ToolRegistry.registerTool` and `ToolRegistry.registerFactory` skip
  when the tool name is in the disabled set, with a debug log line
- New `settings.tools.disabled: string[]` (UNION merge across scopes),
  wired from `loadCliConfig` into ConfigParameters
- Tests pin the contract: skip at register, lazy factory skip, and the
  "next refresh" semantic (already-registered tools are unaffected by a
  subsequent toggle — the disabled set is consulted at register time,
  not at lookup time)

Foundation for the `POST /workspace/tools/:name/enable` route in a
follow-up commit; the bridge will write the settings file directly,
and the next ACP child spawn will pick up the change.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* feat(serve): add session approval-mode mutation route (#4175 Wave 4 PR 17)

Adds POST /session/:id/approval-mode — the first strict-gated session
mutation surface introduced in Wave 4 alongside PR 16 / PR 21. Remote
clients can switch a live session's approval mode (plan / default /
auto-edit / yolo) without touching the user's host CLI.

Routing:
- Route handler validates `mode` against the closed `APPROVAL_MODES`
  enum and an optional `persist: boolean` flag (400 on either)
- Bridge `setSessionApprovalMode` forwards through the new
  `qwen/control/session/approval_mode` ACP extMethod (introduced in a
  new `SERVE_CONTROL_EXT_METHODS` namespace) so the change lands inside
  the ACP child's per-session `Config`
- `persist: true` writes `tools.approvalMode` to workspace settings via
  a new `BridgeOptions.persistApprovalMode` callback wired in
  `runQwenServe`. Default is ephemeral so a remote caller does not
  pollute the user's host settings unless asked

Trust gate translation:
- ACP child catches `TrustGateError` from `Config.setApprovalMode` and
  re-raises as a JSON-RPC error with `data.errorKind: 'trust_gate'`
- Bridge detects the structured payload and re-instantiates the typed
  `TrustGateError` (since the class name does not survive the wire)
- `sendBridgeError` translates to HTTP 403 with the closed PR-13
  `errorKind: 'auth_env_error'` taxonomy

SDK additions:
- `DaemonClient.setSessionApprovalMode(sessionId, mode, opts?, clientId?)`
  mirrors the route shape and forwards `X-Qwen-Client-Id`
- New `DaemonApprovalMode` literal union and `DAEMON_APPROVAL_MODES`
  const tuple; `DaemonApprovalModeResult` for the route response
- New `approval_mode_changed` typed event on `DaemonControlEvent`,
  reducer integration on `DaemonSessionViewState`
  (`approvalMode` / `approvalModeChangedCount` / `lastApprovalModeChange`)
- Drift detector `approvalMode.test.ts` walks core's `ApprovalMode`
  enum and fails CI if `APPROVAL_MODES` or `DAEMON_APPROVAL_MODES`
  drift in either direction

New capability tag `session_approval_mode_control` (always-on, since v1).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* feat(serve): add workspace tool toggle route (#4175 Wave 4 PR 17)

Adds POST /workspace/tools/:name/enable — strict-gated mutation route
that toggles a tool name in the workspace's `tools.disabled` settings
list. Pure file IO + workspace-scoped event fan-out; no ACP roundtrip.

- Bridge `setWorkspaceToolEnabled(toolName, enabled, originatorClientId)`
  invokes the new `BridgeOptions.persistDisabledTools` callback. The
  default `runQwenServe` wires it to `loadSettings(workspace).setValue(
  'tools.disabled', merged)` with a fresh load on each call so concurrent
  edits from other writers stay safe across the read/modify/write window
- New private `broadcastWorkspaceEvent` helper fan-outs to every live
  session SSE bus, swallowing per-bus errors so a single torn-down
  session can't block its peers. Naming mirrors PR 21 #4255 (the post-
  PR-16 fold-in will collapse the two helpers)
- Unknown tool names are accepted: the daemon has no authoritative tool
  registry to validate against (built-ins live inside the ACP child,
  MCP tools are discovered post-spawn). Pre-disabling a not-yet-installed
  MCP tool is a legitimate use case
- Live ACP children retain already-registered tools — the toggle takes
  effect on the next ACP child spawn (`tools.disabled` is consulted at
  Config construction time, gated in ToolRegistry.registerTool by PR 17
  commit 2)

SDK additions:
- `DaemonClient.setWorkspaceToolEnabled(toolName, enabled, clientId?)`
  with URL-encoded tool name
- `DaemonToolToggleResult` + `DaemonToolToggledEvent` typed event,
  reducer integration on `DaemonSessionViewState` (`toolToggleCount` /
  `lastToolToggle`)
- `asKnownDaemonEvent` runtime guard for `tool_toggled` AND
  `approval_mode_changed` (the latter was missed in commit 3 — without
  this entry the events were silently filed as `unrecognizedKnownEvent`
  by `reduceDaemonSessionEvent`, never reaching the typed reducer cases)

New capability tag `workspace_tool_toggle` (always-on, since v1).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* feat(serve): add workspace init route (#4175 Wave 4 PR 17)

Adds POST /workspace/init — strict-gated mutation route that scaffolds
an empty `QWEN.md` (or whatever `getCurrentGeminiMdFilename()` returns
under `--memory-file-name` overrides) at the daemon's bound workspace
root. Mechanical only — does NOT invoke the LLM. Clients that want
AI-driven content fill should follow up with POST /session/:id/prompt.

Behavior:
- Default refuses to overwrite when the target file exists with non-
  whitespace content; the bridge throws `WorkspaceInitConflictError`
  which the route translates to HTTP 409 `workspace_init_conflict`
  with the resolved path + size in the body
- `body: {force: true}` overwrites unconditionally; response carries
  `action: 'overwrote'` vs `'created'` so SDK consumers can render
  the difference
- Whitespace-only existing content is treated as absent (no 409),
  matching the local `/init` slash command's behavior so a half-
  broken init left with an empty file doesn't trap the user
- Pure file IO + workspace-scoped event fan-out — no ACP roundtrip;
  works regardless of whether an ACP child is alive
- Fan-outs `workspace_initialized` event with `{path, action}` to
  every live session SSE bus via the `broadcastWorkspaceEvent`
  helper introduced in commit 4

SDK additions:
- `DaemonClient.initWorkspace(opts?, clientId?)` with conditional
  body emission (omits `force` unless explicitly true so older
  daemons that reject unknown body fields stay compatible)
- `DaemonInitWorkspaceResult` + `DaemonWorkspaceInitializedEvent`
  typed event with runtime guard (`isWorkspaceInitializedData`),
  reducer integration on `DaemonSessionViewState`
  (`workspaceInitCount` / `lastWorkspaceInit`)

New typed error class `WorkspaceInitConflictError` exported from
`packages/cli/src/serve/index.ts` so direct embeds can match it via
`instanceof`.

New capability tag `workspace_init` (always-on, since v1).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* feat(serve): add MCP server restart route with budget guard (#4175 Wave 4 PR 17)

Adds POST /workspace/mcp/:server/restart — strict-gated mutation
route that performs a single-server MCP restart through the ACP
child's `McpClientManager.discoverMcpToolsForServer`. Pre-checks the
live budget snapshot from PR 14 v1 (#4247) so a restart on a
budget-saturated workspace returns a soft refusal rather than
triggering a `BudgetExhaustedError` cascade through the discovery
loop.

Decision logic (ACP-side, in `qwen/control/workspace/mcp/restart`
extMethod):
- Server not in `getMcpServers()` → JSON-RPC `resourceNotFound` →
  HTTP 404
- Server in `excludedMcpServers` → 200 with `{skipped:true,
  reason:'disabled'}`
- `manager.isServerDiscovering(name)` → 200 with `{reason:'in_flight'}`
- Mode is `enforce`, server not in `reservedSlots`, total ≥ budget →
  200 with `{reason:'budget_would_exceed'}`
- Otherwise: `discoverMcpToolsForServer(name, config)`, return
  `{restarted:true, durationMs}`

Soft refusals still return 200 because the route understood the
request and reached a deterministic answer about why no restart
happened. Only hard "we cannot answer" cases (unknown server, no
live ACP child) escalate to non-2xx. This mirrors PR 14 v1's
discovery-time refusal contract: refusals don't throw, they get
recorded.

Bridge:
- New `restartMcpServer(serverName, originatorClientId)` forwards
  through the new `SERVE_CONTROL_EXT_METHODS.workspaceMcpRestart`
  extMethod against the live `liveChannelInfo()` channel
- Throws `SessionNotFoundError` (mapped to HTTP 404) when no ACP
  child is alive — restart inherently requires a live
  `McpClientManager` instance
- Fan-outs `mcp_server_restarted` (success) or
  `mcp_server_restart_refused` (skip) to every live session SSE bus

Core:
- New public `McpClientManager.isServerDiscovering(serverName):
  boolean` — reads `serverDiscoveryPromises.has(name)` so the
  daemon can short-circuit a redundant restart with
  `skipped:in_flight` instead of awaiting the original discovery
  promise (HTTP latency stays bounded)

SDK additions:
- `DaemonClient.restartMcpServer(serverName, clientId?)` with
  URL-encoded server name
- `DaemonMcpRestartResult` discriminated union, two new typed
  events (`DaemonMcpServerRestartedEvent`,
  `DaemonMcpServerRestartRefusedEvent`) with runtime guards,
  reducer integration on `DaemonSessionViewState`
  (`mcpRestartCount` / `lastMcpRestart` /
  `mcpRestartRefusedCount` / `lastMcpRestartRefused`)

New capability tag `workspace_mcp_restart` (always-on, since v1).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* docs(serve): mutation control routes protocol section (#4175 Wave 4 PR 17)

Adds a "Mutation: approval, tools, init, MCP restart" section to the
developer protocol doc covering all four PR 17 routes:

- POST /session/:id/approval-mode — `{mode, persist?}` request, four
  closed-enum modes, trust-gate 403 with `errorKind: 'auth_env_error'`,
  `approval_mode_changed` SSE event (session-scoped)
- POST /workspace/tools/:name/enable — `{enabled}` request, unknown
  names accepted, "next-spawn semantics" call-out, `tool_toggled`
  SSE event (workspace-scoped fan-out)
- POST /workspace/init — `{force?}` request, scaffold-only contract
  (no LLM call), 409 with `path` + `existingSize` body when the
  target exists with non-whitespace content, `workspace_initialized`
  SSE event (workspace-scoped)
- POST /workspace/mcp/:server/restart — empty body, soft-skip
  decision table (in_flight / disabled / budget_would_exceed),
  `mcp_server_restarted` and `mcp_server_restart_refused` SSE events

Capability list at the top of the file updated with the four new
tags (and a missing-from-PR-13 fix for `workspace_env` /
`workspace_preflight`).

User-facing `qwen-serve.md` gains a one-line "Remote runtime control"
bullet under "What it gives you" pointing to the four routes and
clarifying that `/workspace/init` is mechanical only.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): fold-in 1 — wenshao + gpt-5.5 review (#4175 Wave 4 PR 17)

Addresses 5 critical / 4 high / 2 medium items from #4282 review.

CI blocker (wenshao H1)
- Move `approvalMode.test.ts` from `packages/cli/src/acp-integration/`
  to `packages/sdk-typescript/test/unit/approval-mode-drift.test.ts`.
  The CLI package has no `@qwen-code/sdk` dep and the tsconfig has no
  path mapping for it, so `tsc --build` failed `Cannot find module
  '@qwen-code/sdk'` on Lint + Test (mac/linux/windows). The SDK
  package is the right host: it already depends on
  `@qwen-code/qwen-code-core`, and the test pins the SDK ↔ core
  contract directly. Also drop the tautological
  `APPROVAL_MODES contains every ApprovalMode enum value` check —
  `APPROVAL_MODES` is defined as `Object.values(ApprovalMode)` in
  core, so that assertion can never fire.

Critical (gpt-5.5 via wenshao /review)
- C1 (`initWorkspace` path traversal): `getCurrentGeminiMdFilename()`
  is settings-controlled. A daemon configured with
  `context.fileName: "../outside.md"` could resolve outside
  `boundWorkspace` and let this strict-gated mutation create or
  truncate a file outside the workspace boundary. Resolve and verify
  the joined path stays within `boundWorkspace`; reject otherwise.
- C2 (`X-Qwen-Client-Id` forgery): the 3 workspace mutation routes
  (`/workspace/init`, `/workspace/tools/:name/enable`,
  `/workspace/mcp/:server/restart`) accepted any syntactically valid
  client id and stamped it onto fan-out events without checking
  `bridge.knownClientIds()`. Mirrors the inline validation pattern
  PR 16 already uses for `/workspace/memory` and
  `/workspace/agents`. Add `parseAndValidateWorkspaceClientId` shared
  helper in `server.ts` (collapses with PR 16's pattern when the
  Wave-4-wide DRY refactor lands).
- C3 (MCP restart budget under-count): the pre-check used
  `accounting.total >= budget`, but enforce-mode capacity is
  reserved by `tryReserveSlot` via `reservedSlots` (which counts
  configured + in-flight + disconnected slot holders). `total` only
  counts CONNECTED, so a restart on a budget-saturated workspace
  passed the pre-check while the manager refused internally and
  the route reported `restarted: true`. Mirror the manager's policy
  by checking `reservedSlots.length`.
- C4 (false `restarted: true` on broken MCP):
  `discoverMcpToolsForServer` catches reconnect/discovery errors
  internally (logs and resolves void), so the route reported
  `restarted: true` while the server stayed disconnected. After the
  call, verify the live `getMCPServerStatus(name)` is
  `MCPServerStatus.CONNECTED`; throw a structured JSON-RPC error
  otherwise. New typed bridge error `McpServerRestartFailedError`
  → HTTP 502 with `errorKind: 'protocol_error'`.
- C5 (unknown MCP server falls through as 500): the agent-side
  `RequestError.resourceNotFound` was not specially handled by
  `sendBridgeError`, so a typo in the server name returned 500
  indistinguishable from an internal daemon failure. Re-raise with
  structured `data.errorKind: 'mcp_server_not_found'`; bridge
  re-instantiates as `McpServerNotFoundError`; route maps to a
  stable 404 with `code: 'mcp_server_not_found'` and `serverName`
  in the body.

High (wenshao)
- H2 (`persistDisabledTools` scope leak): the callback read
  `fresh.merged.tools?.disabled` (UNION across System / SystemDefaults
  / User / Workspace) and wrote the result back into
  `SettingScope.Workspace`, copying entries from higher scopes into
  the workspace file on the first toggle. Subsequent removals at the
  originating scope (e.g. User) would no longer take effect. Read
  from the WORKSPACE-scope `LoadedSettings` only via
  `fresh.forScope(SettingScope.Workspace).settings.tools?.disabled`.
- H3 (silent persist no-op): `setSessionApprovalMode` with
  `persist: true` returned HTTP 200 + `persisted: false` when no
  `persistApprovalMode` callback was wired, indistinguishable from
  "hook ran but failed" or genuine `persisted: true`. Throw
  asymmetrically with the sibling `setWorkspaceToolEnabled` (which
  already throws in the same situation).
- H4 (whitespace-only init clobber): `/workspace/init` overwrote a
  whitespace-only `QWEN.md` with `action: 'created'` despite `force`
  not being passed, destroying the user's whitespace content
  (template, half-written init, intentional newline) without a
  signal. Treat existing-and-whitespace-only as a no-op; return
  `action: 'noop'` and skip the write. Adds `'noop'` to the
  discriminator union on `DaemonInitWorkspaceResult` and the
  `workspace_initialized` event payload.

Medium
- M1 (SDK `clientId` position consistency): the four new mutation
  helpers placed `clientId` inconsistently (4th vs 3rd vs 2nd). Fold
  `clientId` into the trailing options bag for all four. Matches
  the existing `context: { clientId }` argument the bridge layer
  already uses internally; reduces caller boilerplate for callers
  that always stamp clientId for audit.
- M2 (dead `instanceof String` branch): drop the no-op
  `instanceof String` clause in `setSessionApprovalMode`'s wire-error
  reconstruction — `Error.message` is always a primitive string.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* chore(vscode): regenerate settings.schema.json for tools.disabled (#4175 PR 17 fold-in)

Picked up by `Check settings schema is up-to-date` lint step (the only
red CI step on `3f63ad435`). PR 17 commit 2 added `tools.disabled` to
`packages/cli/src/config/settingsSchema.ts` but didn't run
`npm run generate:settings-schema`, so the JSON-schema mirror used by
the VSCode IDE companion drifted. Regenerating now picks up the new
entry verbatim — no behavior change.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): fold-in 2 — gpt-5.5 + deepseek review (#4175 Wave 4 PR 17)

Addresses 3 critical / 3 suggestion items from #4282 round-2 review.

Critical (gpt-5.5)
- CV1 (`initWorkspace` symlink escape): the textual `withinWorkspace`
  check on the joined path doesn't see through symlinks. A `QWEN.md`
  symlink inside the workspace pointing outside it would still get
  followed by `fs.readFile` / `writeFile`; under `force: true` the
  route would truncate the external target, and a dangling symlink
  could create outside the workspace. Add an `lstat(target)` check
  before the read/write and reject when `isSymbolicLink()`. The
  proper long-term fix routes through PR 18's `WorkspaceFileSystem`
  boundary (chain-aware resolution + audit hooks); tracked under
  the SV2 TODO comment below.
- CV2 (MCP restart timeout vs MCP discovery deadline): bridge raced
  against `initTimeoutMs` (10s) but `McpClientManager`'s per-server
  discovery deadline can be up to 5 minutes
  (`MAX_DISCOVERY_TIMEOUT_MS = 300_000`). A valid restart returned
  HTTP timeout to the client while the ACP child kept reconnecting
  in the background, leaving daemon and client state divergent. Add
  a dedicated `MCP_RESTART_TIMEOUT_MS = 300_000` constant and use it
  for the bridge race. The bridge race remains a safety net against
  a wedged ACP channel; per-server discovery deadlines stay owned
  by the manager.
- CV3 (`disabledTools` rename ordering bug): the gate ran on
  `tool.name` BEFORE the MCP collision-rename branch. An MCP tool
  that collided with a lazy factory and got renamed via
  `asFullyQualifiedTool()` (e.g. `structured_output` →
  `mcp__rogue-server__structured_output`) bypassed the disabled set
  if the operator disabled the renamed-and-exposed name. Re-check
  `isToolDisabled` after the rename, before inserting into
  `this.tools`. New regression test pins the contract.

Suggestion
- SV1 (deepseek): cap `:name` path parameter at 256 chars so an
  extremely long tool name can't bloat the workspace settings file.
  Mirrors `MAX_CLIENT_ID_LENGTH = 128` and `MAX_WORKSPACE_PATH_LENGTH
  = 4096` siblings.
- SV2 (deepseek): `initWorkspace` uses `node:fs/promises` directly
  instead of routing through `WorkspaceFileSystem`. Bridge layer
  doesn't have `fsFactory` plumbed today (PR 18 boundary is
  per-request inside `createServeApp`); a separate plumbing PR will
  hoist it into `BridgeOptions`. Added a FIXME pointing to that
  follow-up. CV1's symlink reject covers the immediate
  boundary-escape concern.
- SV3 (gpt-5.5): the daemon stamps `originatorClientId` on the SSE
  envelope, but reducer snapshots stored only `event.data`. Consumers
  of `lastApprovalModeChange` / `lastToolToggle` / `lastWorkspaceInit`
  / `lastMcpRestart{,Refused}` couldn't tell whether the mutation
  originated from themselves. New `mergeOriginator` helper copies
  the envelope's `originatorClientId` onto the stored snapshot when
  `data.originatorClientId` is unset (the daemon does not currently
  populate `data.originatorClientId`, but the field exists on the
  Data interfaces — preserve it if a future daemon version does).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): fold-in 3 — gpt-5.5 round-3 review (#4175 Wave 4 PR 17)

Addresses 2 suggestion items from #4282 round-3 review (post-rebase
onto PR 21).

- C7 (`docs/developers/qwen-serve-protocol.md`): protocol doc showed
  built-in display labels (`Bash`, `Read`, `Write`) as disable-able,
  but `ToolRegistry.isToolDisabled` checks the actual registered tool
  name. The shell tool registers as `run_shell_command`, so a
  `POST /workspace/tools/Bash/enable {enabled:false}` would persist
  + emit `tool_toggled` while the next session still registers
  `run_shell_command`. Updated the doc to use the canonical registry
  name in the example body and added a ⚠️ block explaining that
  names must match the registry's exposed identifier exactly. The
  daemon route deliberately does not alias-resolve (it accepts
  unknown names for forward-looking MCP pre-disable, so any
  alias map would be incomplete).
- C8 (`packages/sdk-typescript/test/unit/daemonEvents.test.ts`): the
  5 PR 17 reducer cases (`approval_mode_changed`, `tool_toggled`,
  `workspace_initialized`, `mcp_server_restarted`,
  `mcp_server_restart_refused`) had no SDK-side coverage. Added 7
  tests covering happy-path counter + last-snapshot accumulation,
  malformed-payload rejection (rounds through
  `asKnownDaemonEvent → undefined` and increments
  `unrecognizedKnownEventCount` rather than the event-specific
  counter), all 3 refused-reason literals, the `noop` action
  literal added in fold-in 1, and the `mergeOriginator` precedence
  rule (data-level wins over envelope-level when both present).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): fold-in 4 — qwen-latest review (#4175 Wave 4 PR 17)

Round-4 reviewer adoption (qwen-latest-series-invite-beta-v28):

- C1: hoist `persistApprovalMode` guard before the ACP roundtrip so a
  missing callback no longer leaves the daemon's mode shifted while the
  caller observes a 500 (httpAcpBridge.ts).
- C2: serialize `persistApprovalMode` and `persistDisabledTools` through
  a per-workspace promise chain (`withSettingsLock`) so concurrent
  toggles can't lose updates in the read-modify-write window
  (runQwenServe.ts).
- C3: trim `toolName` before persisting in `/workspace/tools/:name/enable`
  so the write path matches `loadCliConfig`'s `.trim()` on read.
  Re-validates empty-after-trim with 400 `invalid_tool_name`.
- S1: cap `serverName` at `MAX_SERVER_NAME_LENGTH=256` on
  `/workspace/mcp/:server/restart` for parity with the tool-toggle cap.
- S2: when `persist:true` succeeds, mirror `approval_mode_changed` via
  `broadcastWorkspaceEvent` so peer sessions in the same workspace
  observe the new default before their next ACP child spawn.
- S3: `'noop'` added to `FakeBridge.initWorkspaceImpl` return type.
- S5: `qwen-serve-protocol.md` action enumeration now includes
  `'noop'` and notes how the SSE event mirrors the response action.

S4 (sync IO inside async persist callbacks) is acknowledged but
deferred — `loadSettings` is the project-wide read path and the H2
fold-in already restricted us to workspace-scope-only consumption,
keeping the sync window bounded. Fully eliminating it requires
swapping `loadSettings` to async across the CLI, which is out of scope.

7 new tests:
- server.test.ts × 3: tool-name trim, whitespace-only 400, server-name
  256 cap.
- httpAcpBridge.test.ts × 4: pre-call guard ordering for persist:true
  (no callback), persist:false bypasses guard, persist:true broadcasts
  to peer sessions, persist:false stays session-scoped.

Typecheck clean across cli / sdk-typescript / core.
1599/1599 unit tests pass.
2026-05-19 00:27:39 +08:00
jinye
688d64416e
feat(serve): add workspace file write/edit routes (#4175 PR20) (#4280)
Some checks are pending
Qwen Code CI / Classify PR (push) Waiting to run
Qwen Code CI / Lint (push) Blocked by required conditions
Qwen Code CI / Test (macos-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (windows-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Blocked by required conditions
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run
* feat(serve): add workspace file write/edit routes

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(serve): bind file hashes to text snapshots

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(serve): tighten read-bytes snapshot and create-mode publish

- readBytesWindow: re-stat the open fd after read and require
  unchanged ino+size+mtime before emitting the response. Mirrors
  the hardened text-snapshot path so the full-window hash can no
  longer pair with bytes that drifted under in-place rewrite or
  append. Surface drift as retryable hash_mismatch.
- atomicWriteTextResolvedFile: reject a symlinked parent up-front
  as defense-in-depth ahead of the parent-fd publish follow-up
  referenced by assertInodeStableAfterRead.
- atomicWriteTextResolvedFile: publish create-mode writes via
  link()+unlink() instead of rename(). POSIX rename() overwrites
  an existing regular file, so a racing external process could
  break the public create contract; link() returns EEXIST
  atomically and is portable across POSIX/NTFS. The early
  assertCreateTargetAbsent check stays for friendlier errors on
  the non-racing path.

---------

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-18 22:37:08 +08:00
jinye
36760ca63c
feat(serve): auth device-flow route (#4175 Wave 4 PR 21) (#4255)
* feat(serve): auth device-flow route

Implements issue #4175 Wave 4 PR 21. Brokers OAuth 2.0 Device
Authorization Grant (RFC 8628) through the `qwen serve` daemon so a
remote SDK client can trigger a Qwen-account login whose tokens land
on the **daemon** filesystem, not on the client. The daemon polls the
IdP itself; the client's only job is to display the verification URL +
user code.

Runtime locality (#4175 §11): the daemon NEVER spawns a browser or
calls `open(url)` — even when running locally. Static-source grep
test fails the build on `node:child_process` / `open` / `xdg-open` /
`shell.openExternal` / `execa` / `shelljs` / `process.spawn` and
their dynamic-import / require variants.

- `POST /workspace/auth/device-flow` — strict mutation gate; returns
  201 fresh / 200 idempotent take-over with `attached: true`. Per
  per-`providerId` singleton: a second POST while pending takes over
  rather than allocating a new `device_code`.
- `GET /workspace/auth/device-flow/:id` — public state read. Pending
  entries echo `userCode/verificationUri/expiresAt/intervalMs`;
  terminal entries (5-min grace) drop them and surface
  `status/errorKind/hint`.
- `DELETE /workspace/auth/device-flow/:id` — strict; idempotent
  (terminal → 204 no-op; unknown → 404).
- `GET /workspace/auth/status` — pending flows + supported providers
  snapshot. v1 stub for `providers: []` (populated in fold-in 1).

`DeviceFlowRegistry` (`packages/cli/src/serve/auth/deviceFlow.ts`)
is the in-memory state holder:
- per-`providerId` singleton with idempotent take-over
- workspace-wide cap of 4 active flows (abuse defense)
- 5-min terminal grace so SDK reconnects can still observe results
- TTL sweeper evicts grace-expired entries every 30s
- in-flight `Promise` map coalesces concurrent `start()` calls so two
  parallel POSTs don't double-allocate IdP `device_code`
- `transitionTerminal` returns `boolean` so caller-side emit/audit
  guard prevents sweeper × poll-tick double-fire
- `dispose()` wired into `runQwenServe.close()`'s shutdown drain;
  cancels `provider.poll()` mid-flight via `cancelController`,
  records `lost_success` audit when an IdP-minted token is dropped
  by transition

`DeviceFlowProvider` interface accepts `start({signal})` +
`poll(state, {signal})`. `QwenOAuthDeviceFlowProvider` wraps the
existing `QwenOAuth2Client.requestDeviceAuthorization` /
`pollDeviceToken` primitives directly (NOT
`authWithQwenDeviceFlow`, which calls `open(url)`). PKCE is
provider-required by Qwen but optional in the interface for future
non-PKCE providers. `success.persist()` writes to disk FIRST, then
updates the in-process client — a failed disk write no longer
leaves the daemon with a zombie in-memory token. Maps RFC 8628
errors via an anchored regex (`^Device token poll failed:
(expired_token|access_denied|invalid_grant)`) so an
`error_description` containing one of those literals can't
mis-classify an unrelated upstream error.

`BrandedSecret<T extends string>` holds the `device_code` and PKCE
verifier. Earlier draft used `new String()` wrapper which leaked
through `+` / template literals (`Symbol.toPrimitive` →
`valueOf` returned the primitive). Final shape: frozen plain object
+ `WeakMap` indirection + 4-way redaction
(`toString` / `toJSON` / `Symbol.toPrimitive` / numeric coercion →
`'[redacted]'` or `NaN`) + `unique symbol` brand. 6 leak-path
tests: `JSON.stringify` / `String()` / concat / template / `+x` /
reveal-roundtrip.

5 new daemon events (workspace-scoped, fanned out to every active
session bus via `bridge.broadcastWorkspaceEvent`):

- `auth_device_flow_started` — `{deviceFlowId, providerId, expiresAt}`
  (no userCode/verificationUri — see PR 21 design §3)
- `auth_device_flow_throttled` — `{deviceFlowId, intervalMs}`,
  emitted only on upstream `slow_down` interval bumps
- `auth_device_flow_authorized` — `{deviceFlowId, providerId,
  expiresAt?, accountAlias?}`; `accountAlias` is best-effort
  non-PII (never email/phone)
- `auth_device_flow_failed` — `{deviceFlowId, errorKind, hint?}`
  with `errorKind ∈ {expired_token, access_denied, invalid_grant,
  upstream_error, persist_failed}`
- `auth_device_flow_cancelled` — `{deviceFlowId}` (DELETE on pending)

Workspace-scoped reducer `reduceDaemonAuthEvent` produces
`DaemonAuthState { flows: Partial<Record<ProviderId, ...>> }` —
parallel to `reduceDaemonSessionEvent`. Session reducer no-ops on
auth events (workspace-scoped state belongs in its own reducer).

`bridge.broadcastWorkspaceEvent` is intentionally distinct from PR
16's `publishWorkspaceEvent` to avoid merge conflict; collapses to
the shared helper as a fold-in once #4249 lands (~25 LoC).

`@qwen-code/sdk` (`packages/sdk-typescript/`):

- 4 new `DaemonClient` methods: `startDeviceFlow`, `getDeviceFlow`,
  `cancelDeviceFlow`, `getAuthStatus` — typed against the wire
  shapes, errors mapped through the existing `DaemonHttpError`.
- High-level `client.auth` getter (lazy `DaemonAuthFlow` singleton)
  exposes a `start(...).awaitCompletion()` shape mirroring `gh auth
  login`'s UX: print code first, let the SDK consumer decide where
  to open the browser. `awaitCompletion` polls GET on the
  daemon-supplied `intervalMs`, honors `slow_down` bumps, and
  fall-back-recovers from 404 (entry evicted post-grace).

POST + DELETE flow through PR 15's `mutate({strict: true})` —
401 `token_required` on token-less loopback defaults. GET routes
use only the global `bearerAuth`. Every state transition
(`started/authorized/failed/cancelled/expired/lost_success`)
records a structured stderr breadcrumb (`[serve] auth.device-flow:
provider=... deviceFlowId=abc12... clientId=... status=...`)
since `mutate()` doesn't carry an audit hook — events alone aren't
enough since SDK can silently drop them; stderr → journald/docker
logs is the unfalsifiable record.

`auth_device_flow` advertised unconditionally on
`/capabilities.features`. Supported providers list lives on
`/workspace/auth/status` to keep the registry descriptor uniform.

- `packages/core/src/qwen/qwenOAuth2.ts`:
  - exports `cacheQwenCredentials` (was a private function; needed
    by the daemon's device-flow registry)
  - `cacheQwenCredentials` now calls `SharedTokenManager.clearCache()`
    after writing, folding what was previously a paired call site at
    L820+L829. Idempotent change.
  - file mode `0o600` on `oauth_creds.json` (was default 0o666 +
    umask). Mirrors opencode's `auth/index.ts`.
- `packages/cli/src/serve/runQwenServe.ts`: device-flow registry
  `dispose()` wired into the shutdown drain (BEFORE
  `bridge.shutdown()`).

- `auth/deviceFlow.test.ts` — 21 tests: BrandedSecret leak paths,
  state machine (slow_down / success / error), terminal grace,
  concurrent-start coalescing, dispose, cancel idempotency, static-
  source grep against browser-spawn primitives.
- `server.test.ts` — 10 device-flow integration tests:
  POST 201/200 take-over, strict 401, 400 `unsupported_provider`,
  GET / DELETE / `/workspace/auth/status`, 502 `upstream_error`
  mapping, sweeper-driven auto-expiry with controlled clock,
  capability advertisement.
- `daemonEvents.test.ts` — 5 SDK reducer tests: type guards, per-
  provider state projection, `failed` always → `status: 'error'`
  (errorKind carries the kind, including new `persist_failed`),
  session reducer no-ops on auth events.

369/369 serve + SDK tests pass; typecheck + `eslint
--max-warnings 0` clean across 14 PR 21 files.

- [x] Independently mergeable (depends only on merged PR 4 / PR 7 /
      PR 12 / PR 15)
- [x] Backward compatible (4 new routes + 1 capability tag + 5 typed
      events + 4 SDK helpers; existing routes/events untouched)
- [x] Default off (capability advertised but no client is forced to
      use it; CLI `qwen` OAuth flow unchanged)
- [x] `qwen serve` Stage 1 routes / SDK behavior preserved
- [x] Gradual migration (v1 only `qwen-oauth`; future providers
      register through the `DeviceFlowProvider` interface)
- [x] Reversible (revert removes 4 routes + 1 tag + 5 events with no
      schema migration)
- [x] Tests-first (28 new tests across 3 layers)

- Inline `bridge.broadcastWorkspaceEvent` → fold-in to PR 16 (#4249)
  `publishWorkspaceEvent` once that lands
- `/workspace/auth/status` vs PR 12 `/workspace/providers` boundary
  — separate route in v1; merge alternative discussed
- Wave 4 PRs 17/19/20 should adopt the same mutate-strict +
  workspace event-fan-out pattern

5 items from pre-PR specialist passes parked for a focused
follow-up: `DeviceFlowEntry` discriminated union, single-source SDK
status / ProviderId unions, `awaitCompletion` memoization,
broadcast-100%-fail stderr elevation, SDK 404 →
`not_found_or_evicted` errorKind.

Refs: #4175

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 round-1 review feedback

Eleven items from copilot-pull-request-reviewer's round-1 pass on
#4255 — 4 inline threads + 7 from the PR-level review summary.

## Adopted (11 items, code/doc changes)

- **`lastSeenAt` → `lastSeenEventId`** (`events.ts`,
  `DaemonDeviceFlowReducerState`). The field was set from
  `rawEvent.id` (SSE event id) but documented as "epoch ms" — a real
  semantic mismatch that would mislead consumers into time-based
  logic against a monotonic counter. Rename + tighten the JSDoc to
  describe it as an event-id counter; reducer cases updated.
- **`DEVICE_FLOW_EXPIRY_GRACE_MS = 30_000` extracted** in
  `DaemonAuthFlow.ts` (was a magic number on `start.expiresAt +
  30_000`). `AwaitCompletionOptions.timeoutMs` doc now describes the
  actual grace-past-expiry behavior + the rationale (clock skew +
  daemon sweeper interval + network latency) instead of the wrong
  "defaults to expiresAt - Date.now()" claim.
- **Explicit `chmod 0o600`** in `cacheQwenCredentials` after every
  write. `fs.writeFile`'s `mode` only applies on file creation; a
  pre-existing `oauth_creds.json` written under a broader umask kept
  its old permissions across upgrades. The chmod now tightens it on
  every write; chmod failure (Windows / hardened FS) surfaces via
  `debugLogger.warn` instead of silently dropping the invariant.
- **`SharedTokenManager.clearCache()` failure now logs**
  `debugLogger.warn` (was a silent `try { } catch { }`). In
  production a swallowed clearCache means in-process callers serve
  stale credentials until the SharedTokenManager mtime watcher
  catches up — a recoverable degradation worth a log line.
- **Protocol doc** lists `persist_failed` in the
  `auth_device_flow_failed.errorKind` union (was added to the type
  but missed in the doc).
- **`pollDeviceToken({signal})`** plumbed through
  `IQwenOAuth2Client` interface + `QwenOAuth2Client` impl + the Qwen
  device-flow provider. Cancel / dispose during a slow IdP response
  now aborts the in-flight HTTP socket immediately instead of
  waiting for the upstream timeout. Two new registry tests assert
  `cancel()` / `dispose()` propagate abort to the signal observed by
  `provider.poll`.
- **`revealSecret` error message** clarified: was "secret has been
  GC-evicted" (impossible — WeakMap doesn't evict reachable keys).
  Now points at the actual reachable failure modes (forged shape /
  serialize+reparse losing the WeakMap binding).
- **`transitionTerminal` JSDoc** clarifies that the PRIMARY guard
  against late timer secret leaks is the `entry.status !== 'pending'`
  check at the top of `runPollTick`; secret-clearing here is
  defense-in-depth.
- **`DeviceFlowErrorKind` JSDoc'd per variant** so consumers can tell
  when each fires (RFC 8628 distinctions + `persist_failed` vs
  `upstream_error` boundary).
- **Stale "PR 16 / PR 21 §3" temporal references** in
  `DaemonAuthFlow.ts:124` rephrased to be timeless ("workspace-scoped
  events fan out through whatever session buses happen to be live"
  — no PR number references that rot when those PRs merge).

## Not adopted (4 items, replied to in-thread)

- **`authWithQwenDeviceFlow` browser-launch separation** — correct
  architectural advice but out of #4255 scope (would refactor a CLI
  auth UX module that PR 21 only touched additively). Tracked as a
  Wave 5 follow-up.
- **Copyright header year range** — repo-wide convention "2025"; not
  introduced by this PR.
- **Spread `...(x ? {x} : {})` → `x: x ?? undefined`** — the two are
  not semantically equivalent. The current form omits the key
  entirely on falsy `x`; the suggested form always includes the key.
  Tests assert object shape and would break under the change.
- **Eager `client.auth` getter** — public API boundary. Lazy
  construction matches `DaemonSessionClient` precedent + saves the
  module load for SDK consumers that never touch auth.

Refs: #4175 #4255

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-1 review feedback

15 items from @wenshao's review batches on #4255. Catches a handful
of real bugs that the earlier round (commit 3d9f082f5) didn't
surface.

## Critical fixes

- **C1 — `pollUntilTerminal` providerId pass-through**
  (`DaemonAuthFlow.ts:185`). The synthetic 404 fallback hardcoded
  `providerId: 'qwen-oauth'`; the parent `awaitCompletion` already
  receives the real providerId via `start.providerId` but
  `pollUntilTerminal`'s parameter type stripped it. Add the field to
  the param type, propagate.
- **C2 — open `errorKind` allowlist** (`events.ts`). The closed
  5-value union in the type guard silently dropped any `failed`
  event whose errorKind the daemon added without mirroring SDK-side
  (e.g. a future `rate_limited`). The flow's reducer state would
  never transition to terminal, leaving SDK consumers stuck on
  `pending` forever. Open the union with `(string & {})` and accept
  any non-empty string in the runtime guard. Updated test asserts
  forward-compat behavior + still rejects the truly-malformed
  empty-string case.
- **C3 — `persist()` timeout + signal**
  (`deviceFlow.ts`). A wedged disk I/O (NFS stall, encrypted-volume
  contention) without bounds would pin the entry in `pending` until
  the upstream `expires_in` elapsed (potentially minutes). The
  registry now passes its `cancelController.signal` AND arms a hard
  `DEVICE_FLOW_PERSIST_TIMEOUT_MS = 30_000` timer; persist failure
  surfaces as `persist_failed` immediately. The
  `DeviceFlowPollResult` `success` variant signature changed to
  `persist({signal})`.
- **C4 — cancel × success race rollback**
  (`deviceFlow.ts` + Qwen provider). Today, if `cancel()`
  transitions while `persist()` is in flight, the credentials get
  written but the flow's status is `cancelled`. User sees cancelled,
  daemon disk has a valid token. `DeviceFlowPollResult.success`
  gains an optional `unpersist()` callback the registry calls when
  `transitionTerminal(authorized)` fails — the Qwen provider wires
  it to `clearQwenCredentials()`. Rollback failure is audited but
  not propagated (re-running auth would overwrite anyway).
- **C5 — don't `unref()` the `awaitCompletion` sleep timer**
  (`DaemonAuthFlow.ts`). On a standalone Node CLI/script doing just
  `client.auth.start().awaitCompletion()`, the unref'd between-poll
  timer was the only event-loop handle, so Node could exit before
  the user finished authorization. The poll wait is foreground work
  the caller explicitly awaits — keep it ref'd.

## Information-leak fixes

- **S1 — sanitize `persist_failed` hint**. `err.message` from
  `cacheQwenCredentials` embeds the full `~/.qwen/oauth_creds.json`
  path. Broadcast via SSE, that path leaks the daemon's home layout
  to every connected session subscriber. Replace user-facing hint
  with `"credentials could not be written to the daemon filesystem
  — check disk space and permissions"`; full err goes to stderr
  audit only.
- **S2 — sanitize upstream `pollDeviceToken` hint**. The class
  embedded the entire raw IdP response body (which can be an HTML
  error page from a reverse proxy) into the thrown message. Same
  broadcast leak path. Replace upstream-error hint with
  `"unexpected response from identity provider"`; RFC 8628 errors
  use `"Qwen IdP returned ${kind}"`.

## Cleanup / forward-compat

- **D1 — drop duplicate `clearCache()`** at `qwenOAuth2.ts:840`. The
  paired call became redundant once `cacheQwenCredentials` folded
  the clearCache in (PR #4255 fold-in 1). The fold-in 1 message
  said this would be done; the duplicate slipped through.
- **S3 — drop unused `DeviceFlowNotFoundError`** (`deviceFlow.ts`).
  Exported but never imported; route handlers do inline 404 JSON.
- **S4 — single-source SDK status / errorKind unions**
  (`types.ts`). `DaemonAuthDeviceFlowSdkStatus` /
  `DaemonAuthDeviceFlowSdkErrorKind` were parallel literal copies
  of the canonical events.ts definitions — drift waiting to happen.
  Now imported + aliased as type-only re-exports.
- **S5 — broadcast 100% fail elevates to stderr**
  (`httpAcpBridge.ts`). Per-session bus failures stay debug-only,
  but a broadcast where EVERY session bus refused is operationally
  interesting (clients won't see the event). Track success / fail
  counts; `writeStderrLine` when `successCount === 0`.
- **S6 — `this.disposed` check after `await provider.start()`**
  (`deviceFlow.ts`). `dispose()` mid-start would orphan the freshly-
  inserted entry (`schedulePoll` guards on `disposed` so no poll
  fires; the entry never transitions). Throw post-await if disposed.
- **W1 — thread `signal` into `requestDeviceAuthorization`**
  (`qwenOAuth2.ts` + Qwen provider). `start()` had the same
  cancellation gap that `pollDeviceToken` had — a slow
  device-authorization request couldn't be aborted during shutdown.
  Now plumbed end-to-end.
- **W2 — split `invalid_request` from `unsupported_provider`**
  (`server.ts`). Conflating them surfaced misleading remediation
  hints to SDK consumers branching on `code` ("this provider isn't
  supported here" when the real cause was a serializer dropping the
  field). Bad-shape now returns `code: 'invalid_request'`;
  unknown-but-well-formed stays `unsupported_provider`.
- **W3 — drop never-populated `accountAlias`**
  (Qwen provider). The field was wired through types / events /
  reducer / audit but the Qwen IdP's token response doesn't carry
  one (no `name` / `email` / `sub`). Returning only `{expiresAt}`
  makes the field type-honestly absent rather than always-undefined.
  Future provider with an alias-bearing response can populate it.
- **W4 — `DaemonAuthFlow` JSDoc accuracy**. Doc claimed "first
  attempts to consume an SSE event stream … falls back to GET-based
  polling"; actual is GET-only with SSE as a real-time hint for
  clients already subscribed to a session stream.
- **W5 — clearer unit arithmetic** in interval normalization. The
  `(_INTERVAL_MS / 1000) * 1000` cancelation hid the s↔ms boundary;
  expanded form makes both branches unit-explicit.

## Test changes

- `daemonEvents.test.ts` updated to match the now-OPEN errorKind
  union (forward-compat assertion + empty-string still rejected).
- `deviceFlow.test.ts` `FakeProvider.poll` aligned with the new
  `persist({signal})` signature + optional `unpersist`.

## Validation

- `npm run typecheck --workspace packages/cli --workspace
  packages/sdk-typescript --workspace packages/core` — clean
- `npx vitest run packages/cli/src/serve/
  packages/sdk-typescript/test/unit/daemonEvents.test.ts` — 368/368
- `npx eslint --max-warnings 0` over the 11 PR 21 surface files —
  clean

Refs: #4175 #4255

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-2 review feedback

10 new threads from @wenshao's second deep-review pass on #4255.
Verified status: 5 real issues, 1 improvement, 3 stale (already
fixed; comments lagged), 1 false alarm (typecheck demonstrably
clean).

## Critical fixes

- **fold-in 2 C4 REVERSED**: when `provider.poll()` returns success
  AND `cancel()` / `dispose()` transitioned the entry mid-`persist()`,
  the registry now FORCES the entry to `authorized` and keeps the
  on-disk credentials. The earlier rollback (`unpersist()`) wasted
  the user's IdP approval because the RFC 8628 `device_code` is
  single-use — re-running the flow would force them through the
  whole browser-prompt + paste-code dance again for a click whose
  intent was likely "stop the wait" rather than "undo my already-
  completed approval". Aligns with gh CLI / Auth0 SDK / git-
  credential-manager. Audit captures the race via `hint:
  'lost_success_kept ...'`. `DeviceFlowPollResult.success.unpersist`
  field + Qwen provider's `clearQwenCredentials` rollback removed.
- **#1 GET /workspace/auth/device-flow/:id strict gate**: this GET
  surfaces `userCode` / `verificationUri` for pending entries, which
  on the loopback no-token default were readable by any local
  process. POST + DELETE were already strict; aligning GET closes
  the information-disclosure asymmetry. `/workspace/auth/status`
  stays bearer-only (its `pendingDeviceFlows` entries intentionally
  omit `userCode`).
- **#2 `inFlightStarts` hard timeout**: a hung `provider.start()`
  (network partition, unresponsive IdP) used to leave the per-
  `providerId` slot in `inFlightStarts` occupied forever, blocking
  every subsequent POST until daemon restart. New
  `DEVICE_FLOW_START_TIMEOUT_MS = 30_000` arms a timer that
  `cancelController.abort()`s the start; the rejected promise
  unwinds through the `try/finally` clearing the slot.
- **#10 chain-completing the C3 persist-timeout**: the earlier C3
  fix armed a 30s timer that fired `cancelController.abort()` then
  `await result.persist({signal})`, but the chain ended at the
  registry boundary — `cacheQwenCredentials` didn't take a signal,
  so `fs.writeFile` couldn't be aborted. Now `cacheQwenCredentials`
  accepts an optional `{signal}` and threads it into
  `fs.writeFile(..., {signal})` (Node native). The Qwen provider's
  `persist({signal})` forwards the entry's
  `cancelController.signal` end-to-end.

## Improvement (#4): 404 fallback errorKind

`pollUntilTerminal`'s 404 catch used to synthesize
`{status: 'expired'}` for ALL evicted entries — conflating "your
flow expired during your disconnect", "the daemon was restarted",
and "your deviceFlowId was wrong". Now returns
`status: 'error'` + `errorKind: 'not_found_or_evicted'` + a `hint`
so SDK consumers branching on errorKind can distinguish.

## Information leak (#9): start() path raw IdP message

S2 (fold-in 2) sanitized `poll()`'s upstream-error hint, but
`start()` still embedded the raw `err.message` (full IdP response,
potentially HTML from a reverse proxy / WAF) into the
`UpstreamDeviceFlowError` that flowed to SDK clients via the 502.
Now uses static messages for the SDK-visible errors; raw detail
goes through `writeStderrLine` for operator audit only. Mirrors
S2's approach.

## Stale comments cleaned (#5, #7)

`qwenDeviceFlowProvider.ts:177` claimed
`cacheQwenCredentials` "doesn't currently take a signal — that's
a follow-up". After #10 above, that's no longer true; the comment
is replaced with the actual end-to-end signal-threading note.

## Not adopted (1 false alarm)

- Thread on `types.ts:330` claimed type-only-import-after-
  declarations breaks `tsc` and fails `daemonEvents.test.ts:670`
  with TS2345. Demonstrably false: `npx tsc -p
  packages/sdk-typescript/tsconfig.json --noEmit` exits 0;
  `daemonEvents.test.ts` is the post-fold-in-2 file with the
  open-allowlist assertion (test 28/28 passes). The reviewer may
  have been looking at a transient state during their analysis.

## Validation

- `npm run typecheck --workspace packages/cli --workspace
  packages/sdk-typescript --workspace packages/core` — clean
- `npx vitest run packages/cli/src/serve/
  packages/sdk-typescript/test/unit/daemonEvents.test.ts` — 398/398
  pass
- `npx eslint --max-warnings 0` over the PR 21 surface — clean

Refs: #4175 #4255

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-3 review feedback

5 new threads from the third deep-review pass on #4255. 3 real
issues fixed; 1 stale (already done in fold-in 3); 1 deferred as
non-blocking design suggestion.

- **A — `expiresIn` / `interval` non-finite guard**
  (`deviceFlow.ts`). The provider contract types both as `number`,
  but a misbehaving / future provider could hand `undefined` /
  `NaN` / `Infinity`. `Math.max(0, NaN) * 1000` is `NaN`, then
  `now() + NaN` is `NaN`, then `now >= NaN` is always `false` —
  the sweeper would NEVER evict the entry, pinning an upstream
  `device_code` slot until daemon restart. Same hazard on
  `interval * 1000` (NaN → `setTimeout(NaN)` fires immediately,
  Infinity → scheduler clamps to TIMEOUT_MAX). Now both fields go
  through `Number.isFinite(x) && x > 0`; missing/bad values fall
  back to RFC 8628's recommended ceilings (10 min for expiry, 5s
  for interval).

- **D — typed `app.locals` accessor**
  (`deviceFlow.ts` + writer/reader call sites). The
  `app.locals['deviceFlowRegistry']` string key was shared between
  `createServeApp` (writer) and `runQwenServe` (reader); a typo on
  either side would compile cleanly and the shutdown dispose call
  would silently no-op, leaving polling timers running until the
  `unref()` rescue. New `setDeviceFlowRegistry(app, registry)` /
  `getDeviceFlowRegistry(app)` pair gives both call sites
  type-checked access; the string literal is encapsulated in one
  module.

- **E — `UnsupportedDeviceFlowProviderError` docstring**
  (`deviceFlow.ts`). After fold-in 2's W2 fix split
  `invalid_request` from `unsupported_provider`, the route layer
  screens unknown ids against `DEVICE_FLOW_SUPPORTED_PROVIDERS`
  before reaching the registry — so this error is now reachable
  ONLY on a daemon-internal invariant violation (id is declared
  supported but not registered in the runtime provider map).
  Docstring + thrown message updated to reflect that this branch
  signals a programmer error, not user input.

- **B** claimed `cacheQwenCredentials(credentials)` doesn't forward
  signal to `fs.writeFile`. Verified: fold-in 3 (#10) at
  `qwenDeviceFlowProvider.ts:204` calls
  `cacheQwenCredentials(credentials, { signal: persistOpts.signal })`
  and the core helper threads it into `fs.writeFile(..., {mode,
  signal})`. The reviewer was looking at the comment block above
  (lines 174-181) without scrolling to the actual call site.

- **C — SDK `cancelDeviceFlow` lossy 204/404 collapse**.
  Suggested returning `{existed: boolean; alreadyTerminal: boolean}`
  instead of resolving void on both 204 and 404. Real signal-loss
  but tagged "[非阻塞]" by the reviewer; changing requires a
  daemon route shape change (200 + body instead of 204) which is
  better as a focused follow-up PR. Acknowledged in-thread;
  deferred to a fold-in PR after #4255 lands.

- `npm run typecheck` — clean across `packages/{cli,sdk-typescript,core}`
- `npx vitest run packages/cli/src/serve/
  packages/sdk-typescript/test/unit/daemonEvents.test.ts` — 398/398
- `npx eslint --max-warnings 0` over the PR 21 surface — clean

Refs: #4175 #4255

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-4 review feedback

4 threads from the fourth review pass on #4255. 3 adopted + 1
deferred (out-of-scope rename of PR 15's `mutate` helper).

## Adopted

### #1 — `persistInFlight` flag suppresses cancel × persist event-stream UX trap

When `provider.poll()` returns success and we await `persist()`, a
concurrent `cancel()` would synchronously transition the entry to
`cancelled` and emit `auth_device_flow_cancelled` — then `persist()`
resolves and (per fold-in 3 C4) force-overrides to `authorized` +
emits `auth_device_flow_authorized`. The reducer state correctly
last-write-wins on `authorized`, but DIRECT event-stream consumers
(close-dialog handlers, telemetry, UI cleanup) race onto an unmounted
UI when the second event lands.

Now: while persist is in-flight, `cancel()` and the sweeper SKIP the
state transition + event emit. They register intent (set
`cancelRequestedDuringPersist=true` for cancel; sweeper just no-ops)
and let the persist resolution decide:

- persist succeeds → `authorized` (IdP wins per fold-in 3 C4)
- persist fails AND cancel was requested → `cancelled`
- persist fails AND `now >= expiresAt` → `expired` / `expired_token`
- persist fails otherwise → `error` / `persist_failed`

Result: at most one terminal event per flow. Imperative SSE
consumers no longer see oscillating terminal states. Audit captures
the race (`hint: 'lost_success_kept ...'`) for incident-response
correlation.

### #2 — `revealSecret` → `unsafeRevealSecret` rename

The earlier JSDoc claimed "the `unsafeReveal_` naming is intentional:
greppable in code review, easy to allowlist in lint rules, hard to
invoke by accident" — but the actual function was named
`revealSecret`. The promised safety properties didn't exist; a code
reviewer wouldn't single out `revealSecret` as suspicious, and a
`no-restricted-syntax` ESLint rule wouldn't flag it.

Renamed to `unsafeRevealSecret` so the JSDoc-promised "greppable" /
"lintable" property is now actually true. Two call sites in the
Qwen provider + 4 test references updated. Internal symbol; not
exposed through the SDK package.

### #4 — `QwenOAuthPollError` typed class replaces substring regex

The earlier RFC 8628 error mapper used an anchored regex against the
thrown error message text — an implicit cross-file string contract
between `qwenOAuth2.ts` (throws) and `qwenDeviceFlowProvider.ts`
(matches). If `qwenOAuth2.ts` ever changed its message format, ALL
RFC 8628 errors (`expired_token` / `access_denied` / `invalid_grant`)
would silently fall through to `upstream_error` — wrong errorKind
flowing through telemetry with no test or type-system check to catch
the drift.

Now `QwenOAuth2Client.pollDeviceToken` throws a structured
`QwenOAuthPollError extends Error` with `oauthError` / `description`
/ `status` fields. The provider branches on `instanceof
QwenOAuthPollError` and reads `.oauthError` directly via a
dedicated `mapRfc8628OAuthCode(code)` switch. The drift hazard is
gone: a future code change that touches the typed class will
fail tsc until both sides are updated. Message format preserved
for any pre-existing log-parsing / substring matchers.

## Not adopted

### #3 — `mutate({strict:true})` semantic awkwardness on GET

Reviewer correctly noted that `mutate` is named for state-changing
routes, but `GET /workspace/auth/device-flow/:id` uses it for an
information-disclosure defense (only reachable code path is reading
state). Suggested rename: `mutate` → `strictHttpGate`.

Deferred: the rename touches PR 15's helper which has many call
sites in `server.ts` and is shared infrastructure for Wave 4 PRs
17/19/20. PR 21 is the first / only consumer of the strict-on-GET
form so far; widening the rename to a Wave 4 follow-up keeps the
fold-in scope tight. Replied in-thread.

## Validation

- `npm run typecheck` — clean across `packages/{cli,sdk-typescript,core}`
- `npx vitest run packages/cli/src/serve/
  packages/sdk-typescript/test/unit/daemonEvents.test.ts` — 544/544
- `npx eslint --max-warnings 0` over the PR 21 surface — clean

Refs: #4175 #4255

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-5 review feedback

Five small adopt items from the round-5 review pass; one stale thread
already addressed in b5b77ee90 (fold-in 5).

#2 — `as const` + derived type for DEVICE_FLOW_SUPPORTED_PROVIDERS so
adding/removing a provider id requires touching exactly ONE site.
Mirrors `SERVE_ERROR_KINDS` / `ServeErrorKind` in `status.ts`.

#3 — Clarify `DEVICE_FLOW_EXPIRY_GRACE_MS` JSDoc to distinguish the
daemon's 30s SWEEP cadence (what the grace tracks) from the 5-min
TERMINAL_GRACE_MS reconnect window (which awaitCompletion does NOT
need to wait through).

#4 — Add `@remarks` block on `DeviceFlowProvider.poll()` warning
future provider authors that thrown `err.message` flows verbatim
into the SSE-broadcast `auth_device_flow_failed` hint, and must be
sanitized. Two equally-correct paths documented (typed `error`
result vs sanitized thrown message).

#5 — Truncate raw IdP detail in `qwenDeviceFlowProvider.ts` stderr
audit lines to 2 KiB. WAFs / reverse proxies can return MB-sized
HTML error pages, and container log aggregators (Loki, Fluent Bit,
Stackdriver) typically truncate or drop lines past 4-32 KiB —
losing the useful prefix downstream. 2 KiB retains structured JSON
envelopes while staying well below every aggregator's per-line cap.

#6 — Track latest `originatorClientId` on per-provider singleton
take-over via new `entry.lastOriginatorClientId` field +
`recordTakeover()` helper. When a second SDK client posts
`POST /workspace/auth/device-flow` for an already-pending provider
(or one being created in `inFlightStarts`) with a different
`initiatorClientId`, an audit breadcrumb records the take-over so
incident response can correlate "client A started, client B took
over at 12:34". Event-routing intentionally still uses the original
`initiatorClientId` (events are workspace-broadcast and changing
the originator field mid-flow would break SDK reducers that key on
it). Two new tests cover the differing-id audit + same-id no-op.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-6 review feedback

Six "Critical" findings from a gpt-5.5 /review pass — all real
liveness/correctness defects in the daemon's auth device-flow path
and the SDK's awaitCompletion polling loop.

#1 — Make `provider.start()` timeout authoritative via `Promise.race`
in `DeviceFlowRegistry.doStart`. The earlier shape only ABORTED the
signal on timeout; a provider that ignores `signal` (non-abortable
I/O, future implementer who forgets to thread it to `fetch`) would
leave the `await` hanging until daemon restart, pinning the
`inFlightStarts` slot for that providerId. Race against a rejecting
timer makes the timeout authoritative regardless of provider
cooperation; abort still fires for cooperative cleanup.

#2 — Same shape for `result.persist()` in the success branch of
`runPollTick`. A future provider whose persist performs
non-abortable steps (mkdir/chmod/mv outside the abortable
fs.writeFile path) would otherwise hang the poll tick until process
restart. Race against rejecting timer; rejection maps to
`persist_failed`.

#3 — Clamp `expiresIn` and `interval` upper bounds. Previous
`Number.isFinite + > 0` guards stopped NaN/Infinity but a finite
extreme like `1e12` was still accepted — pinning the per-provider
singleton for ~30,000 years (`expires_in`) or scheduling a
TIMEOUT_MAX-clamped poll that never fires within `expiresAt`
(`interval`). Two new constants (`DEVICE_FLOW_MAX_EXPIRES_IN_SEC =
3600`, `DEVICE_FLOW_MAX_INTERVAL_MS = 60_000`) cap the worst case.

#4 — Extract `getDeviceFlowOrSynthetic404(...)` helper in
`DaemonAuthFlow.ts` and route BOTH the loop body and the
timeout-ceiling final read through it. Previously the ceiling read
went directly through `client.getDeviceFlow` and a 404 at the
boundary (entry evicted just as the timeout fired) would reject with
`DaemonHttpError(404)` instead of returning the structured `{ status:
'error', errorKind: 'not_found_or_evicted' }` that the rest of
`awaitCompletion` promises.

#5 — Validate `AwaitCompletionOptions.timeoutMs` and `pollOverrideMs`
with `Number.isFinite + > 0`. NaN slipped past the previous `??
default` form (NaN is truthy-ish in that position) and produced a
`ceiling` of `NaN` (loop runs forever — `now >= NaN` always false)
or a `setTimeout(NaN)` (Node clamps to 1ms — tight polling loop).
Sanitize to `undefined` so the documented defaults take effect.

#6 — Thread `signal` into `DaemonClient.getDeviceFlow` and forward
to `fetchWithTimeout` (which already composes caller + timeout
signals). awaitCompletion now passes `opts.signal` from both GET
sites. Without this, an `awaitCompletion` caller that aborts mid-
poll could not cancel an in-flight stalled GET; it would have to
wait for the daemon-side `fetchTimeoutMs` (30s default) to fire.

Four new tests in `deviceFlow.test.ts` pin the new behaviors:
hanging-start timeout (#1), hanging-persist → persist_failed (#2),
extreme-expiresIn clamp (#3), extreme-interval clamp (#3).
FakeProvider gained a `startHangs` flag for the non-cooperative
provider scenario.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-7 review feedback

Two findings from a DeepSeek /review pass; both small but legitimate
defense-in-depth gaps.

#1 — `runPollTick`'s catch block forwarded `err.message` verbatim
into the SSE-broadcast `hint`. The provider's `@remarks` contract
(fold-in 6 #4) requires throwers to sanitize, but if violated the
unbounded raw payload would reach every SSE subscriber. Added
`DEVICE_FLOW_POLL_HINT_MAX_LEN = 256` + `truncatePollHint()`,
applied to the catch's `result.hint`. Full raw `err.message` is
still routed to the audit trail (`audit?.record({hint: 'provider.poll()
threw (raw): ...'})`) so operator visibility for incident response
is preserved. Belt-and-suspenders: the contract is now structurally
enforced rather than relying on every future provider author to
read the JSDoc.

#2 — `updateMatchingFlow` (and the `started`/`authorized` handlers
in `reduceDaemonAuthEvent`) unconditionally overwrote state without
comparing `rawEvent.id` against the existing flow's
`lastSeenEventId`. The field's JSDoc documented it as a monotonic
counter to prevent stale frames from overwriting newer state, but
the code didn't enforce that contract. SSE reconnect with
`Last-Event-ID < terminal-frame-id` would replay older frames; if
any of them were for the same `deviceFlowId` (e.g. a delayed
`failed` arriving after `authorized`) the stale frame would
overwrite the terminal. Daemon-side `transitionTerminal` makes the
exact reachable scenario thin, but the documented contract should
match the code.

Threaded `rawEventId` into `updateMatchingFlow` and added the gate
there + in the `started` and `authorized` handlers (the two cases
that don't go through `updateMatchingFlow`). Synthetic frames
without an envelope `id` (`rawEventId === undefined`) bypass the
gate — they originate inside SDK reducer machinery and aren't
subject to replay ordering.

Three new tests pin the contracts:
- `runPollTick catch truncates the SSE hint and preserves raw on
  the audit (fold-in 8 #1)` — `pollThrowsWith` flag on FakeProvider
  models a non-conforming provider; SSE hint < 400 chars + contains
  'truncated'; audit hint contains the full 4_000-char raw.
- `reduceDaemonAuthEvent rejects out-of-order frames (fold-in 8 #2
  monotonicity)` — stale `failed`(id=7) does NOT overwrite
  `authorized`(id=10); stale `started`(id=4) for a different flow
  also rejected.
- `reduceDaemonAuthEvent passes synthetic frames (no envelope id)
  through the gate` — SDK-internal frames without `id` are honored.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-8 review feedback

Twelve correctness + structural fixes from a wenshao + DeepSeek + gpt-5.5
review pass. Tests deferred to fold-in 10 (separate, larger commit).

CRITICAL CORRECTNESS

#7 — `provider.persist()` Promise.race could publish `persist_failed`
to SSE while a non-cooperative provider was still committing
credentials to disk. Added an independent tracker on the original
persist promise: if the race timed out (`persistTimedOut === true`)
AND the underlying persist later resolved successfully, audit a
`lost_success_after_timeout` breadcrumb so operators see the
inconsistency. Tightened the persist `@remarks` contract to require
signal honoring end-to-end. Qwen provider already complies (fold-in
3 #10); this is forward-defense for future providers.

#11 — auth surface (`DaemonAuthFlow`, `reduceDaemonAuthEvent`,
`createDaemonAuthState`, `DEVICE_FLOW_EXPIRY_GRACE_MS`, all event /
data / state types) was re-exported from `src/daemon/index.ts` but
NEVER from the published SDK entry `src/index.ts`. SDK consumers got
`undefined` for everything except `client.auth.start()` (which
traveled through the already-exported `DaemonClient`). Added the
missing exports and pinned via `daemon-public-surface.test.ts`.

#12 — `core/src/qwen/qwenOAuth2.ts:373`'s
`debugLogger.debug('Device authorization result:', result)` writes
the raw `device_code` (RFC 8628 bearer-equivalent credential) to
stderr / journald, bypassing the `BrandedSecret` redaction layer.
Pre-existing on main but PR 21 expanded the exposure surface.
Sanitized to log only `{ ok, expires_in }` on success / `{ ok,
error }` on error.

#13 — `runPollTick` success-branch persist-failure × past-`expiresAt`
classified as `expired_token` instead of `persist_failed`, routing
operators toward "tell user to retry" (RFC 8628 expiry) when the
actual root cause was disk I/O. Reclassified to `persist_failed`
with a `persist_also_failed_past_expiry` audit hint to preserve the
timing detail for incident response.

SMALL CORRECTNESS

#1 — `runPollTick` catch hint replaced with a STATIC bounded message
("provider.poll() failed; see daemon audit log for details"). The
fold-in 8 truncated-prefix approach could still leak the first 256
chars of provider-templated raw text including secret material. Full
raw still routed to audit channel for operator visibility.

#5 — `cancellerClientId` field added to `DeviceFlowEntry`; deferred-
cancel branch in `cancel()` now stamps it on the entry, and the
persist-resolution `cancelled` event publish uses
`entry.cancellerClientId ?? entry.initiatorClientId`. SSE consumers
that suppress self-emitted events can now attribute the cancel
correctly.

#6 — `AwaitCompletionOptions.timeoutMs === 0` (the documented
"settle immediately, return current daemon view" contract) was
treated as falsy by the `?` ternary, falling back to the default.
`sanitizePositiveMs` now takes an `allowZero` opt-in; the ceiling
computation uses `!== undefined` instead of truthy check.

#8 — `EventBus.publish()` returns `undefined` for closed buses (it
does NOT throw). `broadcastWorkspaceEvent` previously counted that
path as success, hiding the all-buses-dropped operator alarm.
Folded the closed-bus-as-failure check into the canonical
`publishWorkspaceEvent` (see #X below).

#9 — start-timeout Promise.race rejected with a plain `Error`,
falling through `sendBridgeError` to a generic 500. Switched to
`UpstreamDeviceFlowError` so a hung IdP correctly surfaces as 502
(matching the envelope every other IdP start failure uses).

STRUCTURAL

#3 — Three identical `transitionTerminal + publish + audit`
expired_token blocks in `runPollTick`/`sweep`/(removed by #13)
deduplicated into a private `expireEntry()` helper. Future event-
shape changes are now a one-edit operation.

#X — PR 16 (#4249) merged on 2026-05-18 06:27Z. Per the inline
comment at httpAcpBridge.ts:501, PR 21's `broadcastWorkspaceEvent`
was kept distinct only to avoid the merge conflict; once PR 16
landed, it became a fold-in candidate. Folded the closed-bus +
all-failed-stderr-escalation operator-visibility features (PR 21's
S5 + fold-in 9 #8) INTO `publishWorkspaceEvent`; dropped
`broadcastWorkspaceEvent` from the bridge interface + impl + test
mocks. PR 21's deviceFlowEventSink now calls
`bridge.publishWorkspaceEvent` — single canonical workspace fan-out.

DOC

#16 — Added a "Cross-client take-over" paragraph to
`docs/users/qwen-serve.md` explaining that two clients on the same
daemon for the same provider get the per-provider singleton with
`attached: true`/`false` distinguishing them; no separate event
fires (both eventually observe the same `auth_device_flow_authorized`).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao round-9 review feedback

Two small non-blocking items from the round-9 pass; defensive shape +
docs only. The 4 deferred test-coverage threads (#1-4 of round-8) are
still tracked for fold-in 10.

#6 — `lastSeenEventId` typed `number` with `?? 0` defaults in the
`auth_device_flow_started` reducer case. The daemon-side `EventBus`
assigns ids ≥ 1 so the `0` sentinel has no real-traffic meaning, but
the monotonic gate (`rawEventId <= flow.lastSeenEventId`) would
reject any future SDK-internal synthetic frame using `id: 0`.
Changed the field type to `number | undefined` and dropped the
`?? 0` from the started case. The `updateMatchingFlow` /
`auth_device_flow_authorized` guards already short-circuit on
`existing.lastSeenEventId !== undefined`, so undefined is safe
end-to-end. Existing 34 reducer tests still pass unchanged.

#7 — Added `@remarks` block to `DeviceFlowErrorKind.persist_failed`'s
JSDoc explaining the lost-success retry UX. When fold-in 9 #7's
`lost_success_after_timeout` audit fires (non-conforming provider
violates signal contract; disk write succeeds after registry
published `persist_failed`), a naive SDK retry hits the IdP a
second time with a fresh `device_code` and prompts the user
twice — but the first credential set is already valid. JSDoc now
documents the mitigation: SDK consumers writing retry logic on
`persist_failed` should call `client.auth.getStatus()` BEFORE
re-prompting; operators can grep stderr/audit for
`lost_success_after_timeout` to detect occurrences.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* test(serve): fold-in 10 — auth device-flow test bundle (#4255)

Lands the four deferred test-coverage items the round-8 review
flagged (and round-9 re-surfaced) as a hard merge prerequisite.
Net +41 tests across registry / SDK helper / client HTTP /
HTTP route layers.

#1 — `deviceFlow.test.ts` `persist failure paths` describe (3
tests, +3). The success arm's three terminal mappings — pure
`persist_failed`, `cancelled` (cancel during persist), and
`persist_failed` past `expiresAt` (the fold-in 9 #13
reclassification with `persist_also_failed_past_expiry` audit
hint) — were 0-covered. Now pinned. Test #2 also asserts the
fold-in 9 #5 cancellerClientId routing on the deferred
`cancelled` event.

#2 — new `DaemonAuthFlow.test.ts` (+14 tests). Mock DaemonClient
with sequenced `getDeviceFlow` replies. Covers happy-path
polling → `authorized`; `slow_down`-driven `intervalMs` bump
firing `onThrottled`; `signal.abort()` rejection; `signal`
propagation through `client.getDeviceFlow` (fold-in 7 #6);
`timeoutMs` ceiling final-read; `timeoutMs:0` immediate-return
(round-9 #6); NaN/Infinity → `sanitizePositiveMs` fallback to
default ceiling (fold-in 7 #5); 404 → synthetic
`error`/`not_found_or_evicted` (fold-in 3 #4) at BOTH the loop
body AND the timeoutMs ceiling read (fold-in 7 #4); non-404
DaemonHttpError rethrown; `cancel()` and top-level
`status()`/`cancel()` wrappers forward correctly.

#3 — `DaemonClient.test.ts` `device-flow methods` describe
(+11 tests). POSTs `/workspace/auth/device-flow` happy path +
clientId header + body shape; 200/201 acceptance; non-2xx →
`DaemonHttpError`. GETs URL-encode the deviceFlowId; forward
`opts.signal` to `fetchWithTimeout`'s composed signal (fold-in
7 #6 — verified by aborting caller signal and observing the
fetch's signal flip to `aborted`); 404 throws. DELETEs
swallow 204 + 404 (idempotent, mirrors `closeSession`); non-
204/404 throws. `getAuthStatus` plain GET. `client.auth`
lazy-instantiated singleton.

#4 — `server.test.ts` 5 supplementary contract tests (+5).
The existing 8 `it()`s cover happy paths + take-over + 401
POST + DELETE pending/terminal/unknown + 502 upstream + sweeper.
This commit plugs gaps: 400 `invalid_request` for missing /
non-string providerId (fold-in W2 split this from
`unsupported_provider`); 409 `too_many_active_flows` (via
injected fake registry); 401 `token_required` on DELETE
without bearer; the asymmetric GET posture
(`/workspace/auth/device-flow/:id` IS strict-gated to prevent
peer-process userCode shoulder-surf; `/workspace/auth/status`
stays read-only because its `pendingDeviceFlows` entries
intentionally redact `userCode`).

Validation: cli serve 631/631 (+8 from #1, #4); sdk 384/384
(+25 from #2, #3, +/- some pre-existing churn). Typecheck +
lint clean.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(qwen): atomic temp+chmod+rename in cacheQwenCredentials (PR #4255 round-11 #2)

gpt-5.5 /review flagged a real correctness/security gap: the
post-write `chmod` ordering left a window where freshly-written
credentials could land in a broadly-readable existing
`oauth_creds.json` before the chmod tightened it. On POSIX, a
chmod failure additionally degraded to a debug warning while the
broadly-readable tokens stayed on disk.

New shape mirrors the standard atomic-write idiom:

  1. Write `${filePath}.tmp.${pid}.${randomUUID()}` with `mode: 0o600`.
     The temp path doesn't exist beforehand, so the `mode` flag
     actually applies on creation (it doesn't on existing files,
     which was the root of the original race).
  2. Defensive `chmod` on the temp file. POSIX failure is now a
     HARD ERROR (refuses to publish broad-perm credentials to the
     canonical filename). Windows logs a debug breadcrumb and
     proceeds, since chmod is a no-op on most NTFS volumes (perms
     go through ACLs).
  3. Atomic `fs.rename` over `filePath`. The canonical path is
     ALWAYS at `0o600` from the moment it contains the new tokens;
     readers see either the old creds or the new creds, never a
     partially-written or broadly-readable state.
  4. Best-effort `fs.unlink` of the temp file on any failure path
     so failed writes don't leave `.tmp.<pid>.<uuid>` litter on
     disk.

Test mock in `qwenOAuth2.test.ts` extended with `chmod` + `rename`
no-op stubs so the existing 158 core/qwen tests still pass; no test
behavior change beyond the mock surface.

Validation: typecheck clean (cli + core + sdk-typescript); core
qwen 158/158; cli serve 643/643; sdk 384/384.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): address PR #4255 wenshao + gpt-5.5 round-12 review feedback

Eight findings from a wenshao + gpt-5.5 /review pass: 1 critical
correctness, 2 real defensive defects, 4 edge cases / minor
hardening, 1 test gap. All adopted.

CRITICAL CORRECTNESS

#1 CzSpN — `dispose()` race: after `await provider.poll(...)` the
post-await guard checked only `entry.status !== 'pending'`, NOT
`this.disposed`. `dispose()` clears the registry maps and aborts
the entry's signal but doesn't mutate `entry.status`, so a
provider whose poll already resolved (or doesn't honor abort) could
enter the success branch and call `result.persist({...})` —
committing credentials on a shutting-down daemon. Added the
`if (this.disposed) return;` guard symmetric with the top-of-method
check.

REAL DEFENSIVE DEFECTS

#2 Cy_ZG — sync-throw escape: the `result.persist({signal})` call
happens BEFORE the `new Promise` constructor that captures it
(`persistTracker` is closed-over inside the constructor). A
non-conforming provider whose persist throws synchronously (e.g.
top-of-function validation) would escape past the outer
`try/catch (await new Promise(...))` and become an
`unhandledRejection` since `runPollTick` is fire-and-forget via
`void`. Wrapped the persist invocation in a try/catch that routes
the sync-throw into the same `persistError` branch.

#3 CzSpe — runtime provider map: provider validation hardcoded
`DEVICE_FLOW_SUPPORTED_PROVIDERS` even though `deps.deviceFlowProviders`
is the documented extension hook for tests/future providers.
Switched both POST validation and `/workspace/auth/status`
`supportedDeviceFlowProviders` to derive from
`deviceFlowProviderMap.keys()` — single source of truth matches
the registry's `resolveProvider`.

EDGE CASES / MINOR HARDENING

#4 Cy_Y9 — `slow_down` re-clamp: `intervalMs += SLOW_DOWN_BUMP_MS`
can push past `DEVICE_FLOW_MAX_INTERVAL_MS` (the bound that keeps
`setTimeout` from clamping to TIMEOUT_MAX). Wrapped in
`Math.min(MAX_INTERVAL_MS, ...)` symmetric with the doStart clamp.

#5 Cy_ZF — `expiresInSec` lower bound: `0.5` was finite-positive
and produced `expiresAt = now() + 500 ms` — first poll (clamped at
≥1 s) fires AFTER expiresAt → flow expires before any user could
authorize. Added `DEVICE_FLOW_MIN_EXPIRES_IN_SEC = 30` (RFC 8628
§3.2 calls 5–30 minutes "reasonable"; sub-30s is non-compliant).

#6 CzHOK — take-over response privacy: `initiatorClientId` was
echoed to ANY take-over POST caller, including those with no
`X-Qwen-Client-Id` header. Bearer-gated already, but the
asymmetry "anonymous caller learns who started it" violated the
no-header-as-privacy-signal contract. Now only echoed when the
caller's id matches the entry's initiator.

#7 CzSpd — production audit visibility: production audit sink
dropped `line.hint`, but the registry uses hints for operator-only
breadcrumbs (`provider.poll() threw (raw)...`,
`lost_success_after_timeout`, `persist_also_failed_past_expiry`,
take-over correlation, `deferred (persist in flight; ...)`). The
documented troubleshooting trail was invisible in production
stderr. Now included with a 1 KiB bound + JSON-quoted so multi-
word hints stay parseable.

TEST GAP

#8 Cy_ZH — `lost_success_after_timeout` audit: the
fold-in 9 #7 split-brain detector for non-cooperative providers
had no test pinning it. Added a controllable `latePersist` Promise
+ test that drives poll → success → enters persist race → fires
PERSIST_TIMEOUT (registry publishes persist_failed) → resolves
persist late → asserts the lost_success audit fires.

Validation: typecheck + lint clean; cli serve 644/644 (+1 from
the new test); sdk-typescript 384/384.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fixup(serve): close concurrent multi-provider cap bypass (PR #4255 round-13 #1)

gpt-5.5 /review caught a real workspace-wide cap bypass:
`countActive()` only counted entries already installed in
`byProvider`, but the cap check at the top of `start()` runs
before any provider's `inFlightStarts` slot completes
`provider.start()`. A burst of fresh starts for
`DEVICE_FLOW_MAX_CONCURRENT + 1` distinct providers all run
synchronously to the cap check (each `start()` is async but
runs to its first await — the await happens AFTER the cap
check), all observe `count === 0` (no `byProvider` entries
installed yet), and all pass — eventually installing more
than the documented four pending flows.

Fix: include `inFlightStarts.size` in `countActive()`. The
two maps are disjoint by construction (the existing-pending
fast-path catches any provider with both), so simple
addition cannot double-count. The second concurrent caller
sees count=1, the third count=2, …, and the (MAX+1)th caller
is rejected with `TooManyActiveDeviceFlowsError`.

Test: `caps at DEVICE_FLOW_MAX_CONCURRENT under CONCURRENT
distinct-provider starts`. Fires `MAX+1` concurrent starts
via `Promise.allSettled`, asserts exactly `MAX` fulfilled +
exactly 1 rejected with the typed error. Pre-fix this test
fails (all `MAX+1` succeed); post-fix it passes.

Validation: typecheck clean across all 4 workspaces;
deviceFlow.test.ts 35/35 (was 34); cli serve 645/645.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)
2026-05-18 22:05:53 +08:00
jinye
103090669e
feat(serve): workspace memory and agents CRUD (#4175 Wave 4 PR 16) (#4249)
* feat(serve): workspace memory and agents CRUD (#4175 Wave 4 PR 16)

Adds the first Wave 4 mutation route surface: workspace-scoped memory
and subagent CRUD over HTTP. Remote clients (TUI / channels / web /
IDE adapters) can now list, read, create, update, and delete subagent
definitions and read / append / replace QWEN.md without disturbing
session state.

Routes:
- GET    /workspace/memory             (read-only snapshot)
- POST   /workspace/memory             (append/replace, strict-gated)
- GET    /workspace/agents             (list project + user + builtin)
- POST   /workspace/agents             (create-only; 409 on collision)
- GET    /workspace/agents/:agentType  (full detail incl. systemPrompt)
- POST   /workspace/agents/:agentType  (update; 403 read-only on builtin)
- DELETE /workspace/agents/:agentType  (idempotent for SDK callers)

Mutation paths use mutate({ strict: true }) from PR 15 so they refuse
unauthenticated requests even on no-token loopback defaults. Workspace
mutations validate X-Qwen-Client-Id against bridge.knownClientIds() and
stamp originatorClientId on emitted events.

Capability tags added: workspace_memory, workspace_agents.

New typed events fanned out via bridge.publishWorkspaceEvent (best-
effort to every active session bus; read-after-write is the contract):
- memory_changed { scope, filePath, mode, bytesWritten }
- agent_changed  { change, name, level }

writeContextFile.ts is the new core helper that resolves
QWEN.md placement (workspace vs ~/.qwen) and append-vs-replace
semantics. Whitespace-only appends short-circuit before fs.writeFile,
so a no-op POST does not bump mtime or fan out a misleading event.

SubagentManager is wrapped with a CRUD-scoped Config stub via Proxy:
only getSdkMode / getProjectRoot / getActiveExtensions are stubbed
(verified against subagent-manager.ts; getToolRegistry is execution-
path only). Any future Config method touched on a CRUD path throws
immediately so dependency creep is visible.

Auto-memory CRUD, persistent audit log, and the EACCES → NOT_FOUND
unlink mapping in core SubagentManager.deleteSubagent are explicit
follow-ups (PR 16.5 / PR 24 / separate fix).

Validation:
- typecheck: cli + sdk-typescript clean
- vitest:    serve 348/348, writeContextFile 10/10, SDK 335/335
- eslint:    clean

* fix(serve): address Codex P2 review on PR 16 (#4175 Wave 4 PR 16 follow-up)

Three correctness issues Codex flagged on the just-shipped workspace
memory + agents CRUD surface:

1. Concurrent POST /workspace/memory append no longer loses writes.
   Two simultaneous appends would each read the same existing file,
   compose new content in JS memory, then race the fs.writeFile —
   the later write silently overwrote the earlier appended entry.
   Add a per-resolved-path Mutex map (mirroring jsonl-utils.ts's
   fileLocks pattern) and wrap the entire read-compose-write
   sequence in runExclusive.

2. GET /workspace/agents now reflects out-of-band file changes.
   SubagentManager.listSubagents() default served the in-memory cache;
   developer / IDE adapter edits to .qwen/agents/*.md never appeared
   even though GET /workspace/agents/:agentType always reads disk.
   Pass { force: true } so the LIST route walks disk every call,
   matching the detail route's "filesystem is the source of truth"
   contract.

3. Reject builtin agent names on POST /workspace/agents to prevent
   undeleteable shadow files. A client could write a project-level
   agent named "general-purpose" — list/load resolved the shadow
   first, but SubagentManager.deleteSubagent's name-based builtin
   guard (subagent-manager.ts:302) rejected DELETE forever. Add a
   BuiltinAgentRegistry.isBuiltinAgent check in parseAgentConfig
   so the conflict surfaces at create time instead of trapping the
   file beyond the API. The check is case-insensitive, matching the
   resolver's case-insensitive cascade.

New tests:
- writeContextFile.test.ts: 10 parallel appends, all 10 entries
  must survive in the final file (would fail without the mutex).
- workspaceAgents.test.ts: GET /workspace/agents observes a
  freshly-written agent file on the second call (force-refresh
  proof); POST with name="general-purpose" returns 422 + the
  case-insensitive variant "explore" too.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: serve 351/351 (was 348, +3 new), writeContextFile 11/11
- eslint: clean

* fix(serve): apply round-1 review fold-in 2a (HIGH + CodeQL) on PR 16

Round-1 inline review (#4249) flagged ~28 items across Copilot,
wenshao, and CodeQL. This commit lands the HIGH-severity correctness
fixes plus the two CodeQL polynomial-regex warnings.

Validation tighten — `parseAgentConfig` + `parseAgentUpdates`:
- Trim leading/trailing whitespace on `name` before passing to
  SubagentManager. `" tester "` would otherwise create a frontmatter
  name with spaces that case-insensitive lookups can never find.
- Fail-closed (422 invalid_config) on present-but-wrong-type optional
  scalars: `model`, `color`, `approvalMode`, `background`. Previously
  malformed values silently dropped through validation, masking
  client-serialization bugs.
- Validate `approvalMode` against the `APPROVAL_MODES` enum on both
  create and update; an unknown value used to 201 with the field
  silently omitted from the saved file.
- `runConfig` is now whitelist-sanitized to `{ max_time_minutes,
  max_turns }` only; unknown keys are dropped, malformed values
  return 422. Previously the whole input object was persisted
  verbatim into YAML frontmatter.
- `?scope=` query is fail-closed for repeated values
  (`?scope=workspace&scope=global`) — Express parses these as arrays
  which the previous `typeof === 'string'` check silently treated as
  absent, broadening DELETE/UPDATE semantics from one level to both.
- Empty update body returns 400 invalid_config (previously rewrote
  the file + emitted a misleading `agent_changed` event).
- No-op updates (every supplied field already matches `existing`)
  return 200 + `changed: false` and SKIP the file rewrite + event
  fan-out.

Memory write helper — `writeContextFile.ts`:
- Move whitespace-only no-op detection BEFORE `fs.mkdir`. Without
  this, an empty POST still created the parent directory and bumped
  its mtime even though `changed: false` was reported.
- Replace two polynomial regex patterns flagged by CodeQL
  (`/^\s+|\s+$/g` and `/^\n+|\n+$/g`) with hand-rolled `while` loops.
  Same pattern auth.ts:120-125 already uses for the same CodeQL rule.

SDK — `DaemonClient.ts` + `types.ts`:
- `DaemonWriteMemoryResult` gains optional `changed?: boolean` so
  typed callers can suppress redundant cache invalidation on no-op
  appends. Optional for forward-compat with daemons that predate the
  field — undefined treats as "changed: true" (legacy contract).
- `deleteWorkspaceAgent` only swallows 404 when the body's `code`
  is `agent_not_found`. A bare 404 (older daemon, misrouted proxy,
  generic gateway page) now throws — previously the SDK silently
  reported success even when the request never reached a route that
  understands workspace agents.
- `updateWorkspaceAgent` adds an optional `scope` parameter
  mirroring `deleteWorkspaceAgent`, so callers can target the user-
  level definition when a project-level agent shadows it.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: serve 357/357 + writeContextFile 12/12 = 369/369 passing
  (was 362; +7 new)
- eslint: clean

Explicitly NOT applying (out of scope per issue #4175 PR 16
review-resolution policy):
- Copilot's "strict gate after body parser" finding — already
  documented as PR 15 review-resolved tradeoff at auth.ts:256-269.

* fix(serve): apply round-1 review fold-in 2b (MEDIUM + tests) on PR 16

MEDIUM hardening:
- Fix the JSDoc on `collectWorkspaceMemoryStatus` to match the
  workspace-root-only discovery the implementation actually does
  today. The 32-iteration upward walk is reserved for a future
  hierarchical mode but breaks after iteration 1 in v1.
- Lower the depth limit on `walkWorkspaceForMemory` from 32 → 12.
  Realistic project depth sits well below 8; 12 leaves headroom
  without amplifying blast radius from symlink cycles.
- Daemon `Config` Proxy now defines a `has` trap symmetric to the
  existing `get` trap. Without it, a future SubagentManager path
  doing `'someMethod' in this.config` would silently get `false` and
  bypass the safety net the throw-on-unknown-property design
  installed.
- Preflight `manager.loadSubagent(name, level)` before
  `manager.createSubagent`. The default-path collision check inside
  SubagentManager would otherwise miss same-frontmatter-name +
  different-filename collisions; the preflight makes 409
  agent_already_exists deterministic.
- Multi-level DELETE now emits one `agent_changed` event per level
  that actually had a file removed. Previously an unscoped DELETE
  removing both project and user shadows would publish only one
  event with one level — misleading subscribers using event metadata
  for toasts / audit / echo-suppression.

Test additions (covers the new event types + bridge fan-out + SDK
helpers):
- `daemonEvents.test.ts`: predicate narrowing for `memory_changed` /
  `agent_changed` (rejects malformed scope/mode/level), reducer
  records `lastWorkspaceMutation` + `lastWorkspaceMutationType` with
  latest-event-wins semantics and stays non-terminal.
- `httpAcpBridge.test.ts`: `publishWorkspaceEvent` fans out to every
  active session bus; `knownClientIds()` aggregates clientIds across
  sessions and the returned set is a snapshot (mutating it does not
  affect future calls).
- `workspaceAgents.test.ts`: success-path test stamping
  `originatorClientId` on the create / update / delete events for a
  known client.
- `DaemonClient.test.ts`: 7 round-trip tests for the new SDK helpers
  (workspaceMemory, writeWorkspaceMemory, listWorkspaceAgents,
  getWorkspaceAgent, createWorkspaceAgent, updateWorkspaceAgent with
  scope query, deleteWorkspaceAgent: 204 / structured 404 / bare 404
  triage).
- `writeContextFile.test.ts`: replace the 30ms-mtime test with a
  `vi.spyOn(fs, 'writeFile')` assertion that the no-op path never
  invokes writeFile. Deterministic on every filesystem.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: serve 363/363 + writeContextFile 12/12 + SDK 347/347
- eslint: clean

Reviewer guide: combined with fold-in 2a (commit 134c43c82),
PR 16's round-1 review feedback is closed except for the explicitly-
deferred Copilot finding on "strict gate after body parser" (already
documented as PR 15 review-resolved tradeoff at auth.ts:256-269).
The DRY refactor wenshao suggested for `resolveOriginatorClientId`
is left as a future sweep — it touches multiple Wave 4 routes and
should land alongside PR 17/19/20/21 to keep the helper's shape
informed by all consumers.

* docs(serve): apply round-1 review fold-in 2c (doc/type tightening) on PR 16

Two doc-only fixes that close the last open Copilot threads on PR
#4249 — both are JSDoc/tsdoc corrections where the wording promised
broader behavior than the implementation actually delivers, so a
maintainer or SDK consumer reading the type would form a wrong
mental model.

1. `DaemonAgentLevel` (sdk-typescript) and `ServeAgentLevel` (cli
   serve) keep `'extension'` + `'session'` on the union for forward-
   compat but the JSDoc now explicitly says the daemon does NOT
   return either today. The `'extension'` case is gated by the
   daemon's stub `Config.getActiveExtensions()` returning `[]`;
   `'session'` is a runtime-only `SubagentManager` cache the CRUD
   routes don't read. Both arms stay so a future PR exposing either
   source is not a breaking SDK change.
2. `DaemonClient.workspaceMemory()` tsdoc no longer says
   "hierarchical" — v1 only discovers files at the bound workspace
   root + the global `~/.qwen` directory, no parent-directory walk.
   The 12-iteration upward-walk loop body inside
   `walkWorkspaceForMemory` is reserved for PR 16.5 hierarchical
   mode and breaks after iteration 1 today; the SDK doc now states
   that explicitly so callers don't expect more than they receive.

No runtime change. Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 363/363 serve + 12/12 writeContextFile + SDK unchanged
- eslint: clean

* fix(serve): apply round-2 review fold-in 2d on PR 16

wenshao round-2 (4 inline comments at 16:51-16:53Z): three real bugs
+ one performance-tradeoff doc note.

1. `composeAppendedContent` now inserts inside the MEMORY section,
   not at EOF. Previously a QWEN.md whose `## Qwen Added Memories`
   block was followed by another `## ...` heading would silently
   land each new entry past the next heading — moving entries into
   the wrong section. Walk the memory header forward, find the next
   `\n## ` heading, and insert just before it. Fall back to the EOF
   append when the memory section is the last block.

2. `parseAgentUpdates` now matches the create-side trim/empty rule
   for `description` (rejects whitespace-only) and ensures
   `systemPrompt` rejects the empty string. Update path used to
   silently accept `"   "` and overwrite the field with blank
   content — divergent from create which 422s the same payload.

3. `isNoOpUpdate`'s runConfig comparison no longer false-positives
   on partial updates. Comparing every known runConfig field against
   `existing` treated absent keys as `undefined` while existing had
   real values — so `{max_time_minutes: 30}` against `{max_time_minutes:
   30, max_turns: 10}` claimed non-no-op and re-emitted
   `agent_changed`. Fixed to only compare keys actually present in
   `updates.runConfig`, matching `mergeConfigurations` semantics
   (existing values preserved when not in updates).

4. JSDoc on the LIST-route `force: true` call now explains the
   tradeoff (no TTL cache / no fs.watch invalidation): re-introducing
   caching would re-introduce the stale-list bug Codex P2 #2 fixed,
   `fs.watch` is platform-fragile, and PR 24's audit/policy layer is
   the proper home for request rate limiting. Sub-millisecond cost
   per request on local SSD; revisit if profiling flags it.

Tests:
- writeContextFile.test.ts: section-boundary insertion + EOF fallback
- workspaceAgents.test.ts: whitespace-only description rejected; partial
  runConfig no-op detection; partial runConfig real change preserves
  omitted keys via mergeConfigurations

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 368/368 (was 363, +5 new)
- eslint: clean

* fix(serve): apply round-3 review fold-in 2e on PR 16

wenshao round-3 (5 inline [Suggestion]s, all real correctness or
forward-compat issues; one item carried over from round-2):

1. `parseAgentConfig` rejects whitespace-only `systemPrompt` on
   create, matching the description field's `trim().length === 0`
   rule. Pure-whitespace prompts collapse to nothing on YAML
   serialization and the agent can't operate without instructions —
   422 at the boundary is friendlier than the downstream "agent does
   nothing" failure.
2. `parseAgentUpdates` mirrors the same `trim()` check on the update
   path so `{systemPrompt: "   "}` returns 422 rather than silently
   blanking the field.
3. `POST /workspace/memory` `file_error` 500 response now carries
   `scope`, `mode`, optional `osCode` (`EACCES`/`EROFS`/`ENOSPC`/...)
   and a redacted `errorMessage`. Previous shape was just
   `{error, code: 'file_error'}` — callers had nothing to branch on.
4. `composeAppendedContent` runs `fs.stat` before `fs.readFile` and
   refuses with a typed `WorkspaceMemoryFileTooLargeError` when the
   existing file exceeds 16 MB. Without this cap a pathological QWEN.md
   would be loaded into the daemon heap on every append. The route
   maps the typed error to a 413 with `code: 'memory_file_too_large'`
   plus `bytes` / `limit` so callers can decide whether to trim or
   switch to mode=replace.
5. `toDetail` no longer spreads `config.runConfig` with a cast.
   Explicit field-by-field pick of `max_time_minutes` / `max_turns`
   ensures any future `SubagentConfig.runConfig` field requires a
   deliberate route-schema update rather than silently leaking
   through the HTTP API.

Tests:
- workspaceAgents.test.ts: whitespace-only systemPrompt rejected on
  create AND update; toDetail.runConfig only emits whitelisted keys
- existing tests still cover the description-side trim and the
  partial runConfig no-op detection from fold-in 2d

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 371/371 (was 368, +3 new)
- eslint: clean

Reviewer note: response shape on 500 file_error is additive
(`scope`/`mode`/`osCode`/`errorMessage` are new fields), so SDK
callers that only consumed `{error, code}` keep working. The new
413 `memory_file_too_large` is a new error code SDK consumers can
branch on but that pre-PR-16 daemons never emitted, so adding it is
also additive.

* fix(sdk): expose `changed` on DaemonAgentMutationResult (PR 16 round-4)

wenshao round-4 review (single inline at types.ts:434): the agent
update route emits `changed: true` for real updates and
`changed: false` for no-op short-circuits (introduced in fold-in
2a alongside the no-op detection), but `DaemonAgentMutationResult`
in the SDK type still only exposed `{ ok, agent }`. Typed callers
of `updateWorkspaceAgent()` couldn't observe the no-op signal even
though `DaemonClient` already returns the raw JSON at runtime.

Add optional `changed?: boolean` matching the shape introduced for
`DaemonWriteMemoryResult.changed` in fold-in 2a. Optional for
forward-compat with daemons that predate the field; SDK consumers
should treat `undefined` as `true` (the legacy contract — every
successful create / update was a write before fold-in 2a's no-op
short-circuit landed).

Test:
- `DaemonClient.test.ts`: round-trip asserts the typed result
  surfaces `changed: false` from the wire payload.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 82/82 in DaemonClient.test.ts (was 81; +1 new)
- eslint: clean

* fix(serve): apply round-6 review fold-in 2g on PR 16

Round-6 review (gpt-5.5 [Critical] + 5 wenshao [Suggestion]s).

[Critical] Per-level delete verification (workspaceAgents.ts):
- gpt-5.5 flagged that `SubagentManager.deleteSubagent` swallows
  per-level `fs.unlink()` failures (subagent-manager.ts:332-336)
  and returns success as long as ANY level was removed. Trusting
  that signal would let the route publish `agent_changed`/`deleted`
  for a file still on disk under EACCES/EBUSY/EPERM — the client UI
  would drop a still-active definition from cache.
- Route now runs `fs.access` on each pre-checked level's file path
  AFTER `manager.deleteSubagent` returns and partitions into
  `removed` / `remaining`. Events are emitted ONLY for confirmed
  removals; if any level still has its file, the route returns 500
  `agent_delete_partial` with `removedLevels` + `remainingLevels`
  so callers can act precisely.
- New test installs a 0o555 chmod on the user-level agents directory
  so `fs.unlink` raises EACCES while the project-level unlink
  succeeds, asserting both the 500 response and that exactly one
  `agent_changed` event fired for the level that actually went away.

Concurrency consistency (writeContextFile.ts):
- Whitespace-only no-op detection now happens INSIDE the per-file
  mutex's `runExclusive` block. The pre-fix layout did the
  short-circuit `fs.stat` outside the lock; under concurrent
  POSTs (one whitespace-only, one with real content) the no-op's
  `bytesWritten` could lag the post-write reality. Functional
  behavior was already correct; this aligns the snapshot with the
  post-write state.

Defense-in-depth + DRY (workspaceAgents.ts):
- `validateAgentType(req, res)` regex-validates `:agentType` URL
  parameter at the route boundary against the same
  `^[\\p{L}\\p{N}_-]+$/u` pattern as `SubagentValidator.validateName`,
  with a 64-char cap. `findSubagentByNameAtLevel`'s readdir scan
  already prevented path traversal, but failing fast at the boundary
  keeps surprising inputs out of downstream code paths. Two new
  tests cover `..%2Fetc%2Fpasswd` and over-long names.
- `parseScopeQuery(req, res)` extracts the duplicated `?scope=` query
  parser from the POST update + DELETE handlers. Same fail-closed
  semantics on repeated/non-string values.
- `assertMutableLevel(found, agentType, res)` extracts the
  duplicated `isBuiltin || level === 'builtin' || 'extension' ||
  'session'` 403 guard. Future Wave 4 mutation routes (PR
  17 / 19 / 20) call this helper instead of re-implementing the
  predicate.

Client-id helper consistency (workspaceMemory.ts):
- `resolveWorkspaceClientId` removed; the inline branch in the POST
  handler now mirrors `workspaceAgents.ts:resolveOriginatorClientId`
  (validate against `bridge.knownClientIds()`, send 400 directly,
  return so the caller short-circuits). Previously this file threw
  `InvalidClientIdError` and caught it locally — wenshao round-6
  flagged the throw-vs-direct-400 inconsistency between the two
  files. The deeper full-extraction DRY refactor remains deferred
  to the cross-Wave-4 sweep with PR 17/19/20/21.

Won't-fix doc note (workspaceMemory.ts):
- Mount-point JSDoc now explicitly explains why the route returns
  absolute on-disk paths (success / 413 / GET list): clients
  pre-flight `caps.workspaceCwd` to learn the bound workspace and
  can compute relative paths if they want; the global scope's
  `~/.qwen/QWEN.md` is NOT under the workspace root, so a
  workspace-relative form would lose information. Path redaction
  for multi-tenant deployments belongs to PR 24's `--redact-errors`
  policy work, not a per-route default flip in PR 16.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 374/374 (was 371, +3 new)
- eslint: clean

* fix(serve): apply round-7 review fold-in 2h on PR 16

glm-5.1 round-7: 2 [Critical] + 5 [Suggestion] inline comments. Five
applied as code changes; one is a stale-snapshot false positive
(workspaceMemory.ts no longer has the InvalidClientIdError call site
glm-5.1 referenced — fold-in 2g already replaced it with inline
400); one is rationale-replied (INVALID_CONFIG → 422 mapping
suggestion is based on incorrect premise about manager semantics).

[Critical] Code-fence-aware section-boundary detection (writeContextFile.ts):
- The naive `\n## ` indexOf scan would split user-authored memory
  entries that quote markdown documentation containing `##` headings
  inside fenced code blocks. New `findNextTopLevelHeading` helper
  tracks fence state line-by-line and only accepts matches outside
  fences. Two new tests: (a) entry containing a fenced `## Request
  Body` keeps its body intact; (b) real `## post` heading outside
  fences still acts as the section boundary.

[Suggestion] errorMessage + filePath gating (workspaceMemory.ts):
- 500 `file_error` and 413 `memory_file_too_large` responses now
  omit `errorMessage` and `filePath` unless `QWEN_SERVE_DEBUG` is
  set. Default response carries `error / code / scope / mode /
  osCode` — enough for SDK callers to branch without leaking
  absolute filesystem paths. New test asserts both modes round-trip
  the right shape.

[Suggestion] publishWorkspaceEvent visibility (httpAcpBridge.ts):
- Catch block now writes to stderr unconditionally during normal
  operation; only downgrades to the debug channel when
  `shuttingDown` is true. `EventBus.publish` is documented never to
  throw, so a hit in normal ops is by definition a regression that
  must be visible in production logs — silencing via debug-gate
  could let a true bug succeed at the route layer (200 OK) while
  SSE subscribers stop receiving events.

[Suggestion] Log-injection defense for `agentType` (workspaceAgents.ts):
- New `safeLogValue` helper wraps `agentType` interpolations in
  `JSON.stringify(...).slice(0, 82)` before stderr writes (mirrors
  `server.ts:1340`). The route's `validateAgentType` regex already
  rejects names with control chars, but defense-in-depth covers
  legacy on-disk shadows and future fields. Five `writeStderrLine`
  call sites updated (GET / POST / DELETE failure, reload-failure,
  partial-delete, create-reload-failure).

[Suggestion] Simplify walkWorkspaceForMemory (workspaceMemory.ts):
- Replaced the 12-iteration loop with a straightforward single-pass
  stat-each-filename. The `seen` Set, `cursor = parent` walk, and
  filesystem-root guard were dead code (the loop unconditionally
  broke on first iteration). PR 16.5's hierarchical mode lands as a
  fresh upward walk rather than re-enabling commented-out code.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 377/377 (was 374, +3 new)
- eslint: clean

Reviewer notes (NOT adopting):
- glm-5.1's "InvalidClientIdError('workspace', ...)" message-confusion
  Critical: stale-snapshot false positive — fold-in 2g already
  removed `resolveWorkspaceClientId` and inlined a 400 with the
  correct "registered for this workspace" wording. Only a comment
  reference remains.
- glm-5.1's "INVALID_CONFIG → 422" suggestion: SubagentManager only
  ever throws INVALID_CONFIG for read-only conditions (built-in /
  extension / session) — not for malformed config (which uses
  VALIDATION_ERROR). The current 403 mapping in update + delete is
  correct for the manager's actual semantics.

* fix(serve): apply round-8 review fold-in 2i on PR 16

wenshao round-8: 2 [Critical] path-disclosure + 5 [Suggestion]
(name regex, per-field caps, mutex timeout, test gaps, tilde fence).
All adopted.

[Critical] C1 — 413 `err.message` path disclosure (workspaceMemory.ts):
- The 413 `memory_file_too_large` response sent `err.message`
  unconditionally as the `error` field. The
  `WorkspaceMemoryFileTooLargeError` constructor embeds the
  absolute file path in its message ("Existing memory file at
  /Users/<x>/.qwen/QWEN.md is ..."), bypassing the `debugMode()`
  gating that already hid the `filePath` field. Same gating now
  applies to both `error` and `filePath`; default response carries
  a generic string + structured `code` / `bytes` / `limit` so SDK
  callers can branch without the path leak.

[Critical] C2 — workspaceAgents FILE_ERROR `err.message` (workspaceAgents.ts):
- Two catch blocks (create + update) sent `SubagentError(FILE_ERROR)`
  messages directly in the response. Node fs errors embed paths
  like "ENOENT: ... '/Users/<x>/.qwen/agents/foo.md'". Both now
  gate behind `isServeDebugMode()`; default response is the generic
  "Failed to write workspace agent file" envelope.

Shared `isServeDebugMode` helper (debugMode.ts new):
- Moved from inlined copies in workspaceMemory.ts to a small
  shared module so both route files (and future Wave 4 mutation
  routes) share one canonical predicate.

[Suggestion] S1 — POST body `name` validation (workspaceAgents.ts):
- `parseAgentConfig` now applies the same regex + length contract
  as `validateAgentType` (`^[\p{L}\p{N}_-]+$/u`, 2-64 chars). A
  client posting `name: "my/agent"` or 100-char name now fails at
  the body-validation boundary with a 422 `invalid_config` instead
  of bubbling a less-specific `SubagentValidator` error.

[Suggestion] S2 — Per-field size caps (workspaceAgents.ts):
- `description` / `systemPrompt`: 256 KB each
- `tools` / `disallowedTools`: 256 entries, each at most 256 chars
  Applied on both create + update; matches workspaceMemory's
  `MAX_MEMORY_CONTENT_BYTES = 1 MB` posture and keeps `GET
  /workspace/agents` list-response cost bounded.

[Suggestion] S3 — Mutex timeout (writeContextFile.ts):
- `getFileLock` now wraps each Mutex with `withTimeout(..., 30_000)`
  so a wedged filesystem (NFS hiccup, OneDrive lock, kernel I/O
  hang) cannot indefinitely hold the per-file lock. The
  `E_TIMEOUT` sentinel is caught and re-thrown as a typed
  `WorkspaceMemoryWriteTimeoutError`; the route maps it to 500
  `memory_write_timeout` with `timeoutMs` so SDK callers can
  branch on stalled-fs without parsing a generic 500.

[Suggestion] S4 — Test gaps:
- `DELETE /workspace/agents/:id?scope=workspace` happy path:
  removes only the project shadow, leaves user file on disk,
  emits exactly one `agent_changed` event with `level: project`.
- `POST /workspace/agents/:id?scope=global` happy path: updates
  user shadow, leaves project file untouched.
- 413 `memory_file_too_large`: write a 17 MB QWEN.md externally,
  POST append fails with the structured 413 payload (`bytes` /
  `limit`, no `filePath` / no path-embedding error message in
  default response).

[Nice] N1 — Tilde fence support (writeContextFile.ts):
- `findNextTopLevelHeading` now toggles fence state on both ``` `
  and `~~~` openers (CommonMark allows both). A `## heading`
  inside a `~~~` fenced block no longer counts as the section
  boundary.

Validation:
- typecheck: cli + sdk-typescript clean
- vitest: 380/380 (was 377, +3 new)
- eslint: clean

* fix(serve): apply round-9 review fold-in 2j on PR 16

Two real correctness fixes from wenshao's 2026-05-18 review:

1. resolveContextFilePath now uses getCurrentGeminiMdFilename() so
   POST /workspace/memory writes to the same file GET surfaces.
   Without this, a deployment that ran setGeminiMdFilename('AGENTS.md')
   saw GET list AGENTS.md while POST kept appending to a stale QWEN.md
   — clients then observed "I just wrote content but it's missing
   from /workspace/memory".

2. runWrite no-op branch now returns bytesWritten: 0 instead of the
   existing file's stat.size. The prior value conflated "bytes I
   wrote" with "current file size"; clients accumulating writes via
   sum(bytesWritten) added the file size for every whitespace POST.
   changed: false already signals the no-op; the byte count should
   match its field name.

JSDoc updated on both WriteContextFileResult.bytesWritten and
DaemonWriteMemoryResult.bytesWritten so the contract is explicit.
New test covers setGeminiMdFilename(AGENTS.md) round-trip; existing
no-op test updated for the new bytesWritten semantics.

Round-8 thread PRRT_kwDOPB-92c6Cpyap (DRY resolveOriginatorClientId)
stays open as the cross-Wave-4 tracking marker. CodeQL "missing rate
limiting" alert deferred to PR 24's audit/policy layer (bearer +
max-connections + mutation gate provide v1 mitigations).

* fix(serve): skip two Windows-incompatible test fixtures on win32

Both tests rely on `fs.chmod(dir, 0o555)` to trigger EACCES on a
subsequent write/unlink. Windows ignores Unix-style permission bits
passed to `fs.chmod`, so the directory stays writable, the operation
succeeds, and the error path the test exercises is unreachable —
the test then sees the success status (200 / 204) instead of the
expected 500. CI failed on Windows runner only; Ubuntu + macOS pass.

Route logic is platform-agnostic — these tests validate that:

- `workspaceMemory.test.ts` POST returns the structured 500 envelope
  (no `errorMessage` / `filePath` leakage outside QWEN_SERVE_DEBUG).
- `workspaceAgents.test.ts` DELETE returns 500 `agent_delete_partial`
  when one level's `fs.unlink` silently fails inside SubagentManager.

Both invariants are still covered by the Ubuntu + macOS runs. We can't
swap in a `vi.spyOn(fs, 'unlink')` mock for the agents case either —
`SubagentManager` does `import * as fs from 'fs/promises'`, creating
a sealed ESM namespace object vitest can't redefine.

Skip pattern mirrors `customBanner.test.ts:232`
(`if (process.platform === 'win32') return;`).
2026-05-18 14:26:59 +08:00
jinye
96219924a0
feat(serve): MCP client guardrails (#4175 Wave 3 PR 14) (#4247)
* feat(serve): MCP client guardrails (#4175 Wave 3 PR 14)

Adds an in-process MCP client counter, slot-reservation enforcement at all 3 spawn sites (discoverAllMcpTools / discoverAllMcpToolsIncremental / readResource), new `--mcp-client-budget=N` + `--mcp-budget-mode={enforce,warn,off}` CLI flags forwarded to the ACP child via env, and additive `clientCount` / `clientBudget` / `budgetMode` / `budgets[]` fields plus `disabledReason: 'budget'` tagging on `GET /workspace/mcp`.

Always-on capability tag `mcp_guardrails` with `modes: ['warn', 'enforce']` so SDK clients can pre-flight refusal semantics. Typed SSE push events (`mcp_budget_warning` / `mcp_child_refused_batch`) intentionally deferred to a small follow-up PR — the snapshot already exposes `budgets[0].status: 'warning'|'error'` + `refusedCount` so operator visibility isn't blocked.

* fixup(serve): address PR 14 review (#4247) findings 1-7

Addresses Codex + Copilot review feedback on #4247. Seven functional and forward-compat fixes; (8) `tcp` transport mapper vs createTransport deferred pending @wenshao direction (separate core/protocol decision).

1. **Single-server rediscovery bypass** — add `tryReserveSlot` at the top of `discoverMcpToolsForServerInternal`. Pre-fix a server refused at startup could be brought online later via `/mcp reconnect <name>` and exceed the cap in enforce mode.
2. **Empty `budgets[]` when mode=off** — early `return []` in `buildBudgetCells` when mode is `off`. Protocol docs / SDK types promise empty array; pre-fix emitted a synthetic noisy cell.
3. **runQwenServe validation + env leakage** — mirror CLI budget validation in `runQwenServe` (the embedded entry point); explicitly delete `QWEN_SERVE_MCP_*` env vars when options are undefined so multiple daemons in one process don't leak prior budget config to subsequent ACP children.
4. **Disabled-vs-refused precedence + stale refusal log** — config-disable wins over budget refusal in the per-server cell; `removeServer` + `disconnectServer` drop the entry from `lastRefusedServerNames` so operator action immediately clears the budget tag.
5. **Incremental remove-before-reserve ordering** — process config-removed servers FIRST in `discoverAllMcpToolsIncremental` so freed slots are visible to subsequent `tryReserveSlot` calls. Pre-fix scenario {a,b}→{a,c} with budget=2 wasted a slot.
6. **`scope` forward-compat type widening** — `'workspace' | (string & {})` on both `ServeMcpBudgetStatusCell` and `DaemonMcpBudgetStatusCell` so SDK consumers don't break when PR 23 adds `scope: 'pool'` per the documented no-schema-bump contract.
7. **Test comment alignment** — fix "With budget=1" comment to match `clientBudget: 2` code.

Plus 4 new core regression tests covering #1/#2/#4/#5, and 4 new serve tests covering #3 (boot rejection + env cleanup). 237/237 pass across the affected files (36 core mcp-client-manager + 50 acpAgent + 151 serve).

* docs(serve): clarify v1 snapshot-based budget warning detection (#4247)

Address github-actions review-summary finding (I) on PR #4247: v1 operators have no SSE push event for budget pressure yet (deferred to PR 14b), so the protocol doc should explicitly say how to detect warning / error states from the snapshot. Adds the three-way mapping `budgets[0].status` ↔ live/refused counts.

* fixup(serve): address PR 14 review round 2 (#4247 wenshao)

Addresses @wenshao review on PR #4247. Three critical safety fixes + four suggestion-level improvements.

Critical (zombie slot leaks — would break `enforce` mode for the rest of the daemon's lifetime):
- C2: `discoverAllMcpTools` connect() catch now releases reservedSlots + clients entry. Pre-fix one failed connect permanently consumed a budget slot.
- C3: `readResource` wraps client.connect() in try/catch; on throw the slot + client entry are cleaned up before re-raising. Tracked `weReservedSlot` so the cleanup only fires for newly-created lazy spawns (reused already-CONNECTED clients are untouched).
- (wenshao C1 was the rediscovery-bypass also caught by Codex + Copilot — already addressed in fixup 597f011e6.)

Suggestion:
- S4: `readBudgetFromEnv` downgrades `mode='enforce'` → `'off'` when no budget is set, mirroring the CLI + `runQwenServe` invariant. Fail-closed on operator misconfiguration rather than silently bypassing enforcement.
- S5: extract duplicated `mcp_budget_decision` telemetry into private `emitBudgetTelemetry(configuredCount)`.
- S6: rename `BudgetExhaustedError` constructor param `liveCount` → `reservedCount`. `reservedSlots.size` is what's blocking the new server, not the live CONNECTED count (those differ when a reserved server is disconnected).
- S7a: bump accounting-failure log level — `debugLogger.debug` (gated on debug=true) replaced by `process.stderr.write` so production daemons surface slot-leak / type-mismatch failures in journald/docker logs.

(S7b — expose `reservedSlots[]` on the wire for slot-leak debugging — deferred as additive; will be in PR 14b alongside the typed events.)

+ 3 new core regression tests (C2 leak release, C3 lazy-spawn leak release, S4 env enforce-downgrade). 626/626 tests pass across the focused suite; typecheck + lint clean.

* fixup(serve): address PR 14 review round 3 (#4247 wenshao second pass)

Addresses @wenshao's second review pass on PR #4247 (submitted 15:56Z after round 2 fixup landed). Four code fixes + three doc clarifications.

Code:
- R3 #5: `readResource` lazy-spawn path now checks `isMcpServerDisabled` BEFORE the budget gate. Pre-existing gap: a server disabled via `mcpServers.<name>.disabled: true` or `/mcp disable <name>` could be resurrected by any resource read. Disabled precedence over budget mirrors the per-server cell logic.
- R3 #6: `buildBudgetCells` now receives the post-disabled-filter `refusedCount` so the workspace cell matches the per-server cell precedence. Pre-fix a server disabled after being refused rendered `disabled` on its per-server row but `error: budget_exhausted` on the workspace row.
- R3 #7: extract `MCP_BUDGET_WARN_FRACTION = 0.75` constant. Was hardcoded in `acpAgent.buildBudgetCells` AND `commands/serve.ts` stderr breadcrumb (the latter with `Math.ceil` divergence on non-integer multiples). Pre-extract so PR 14b's dual-threshold (0.75 warn + 0.375 rearm) lands in one file.
- R3 #1: env-var enforce-without-budget downgrade (already fixed in round 2 ba3e3febd S4 — reply-only on the new thread).

Docs:
- R3 #2: docstring on `mcpTransportOf` now spells out the `tcp` vs `createTransport` divergence + records the deferred decision (PR 14b / future core). Closes the "comment claims X but code does Y" gap.
- R3 #3: comments in both `discoverAllMcpTools` catch (release slot — stop() owns lifecycle) AND `discoverMcpToolsForServerInternal` catch (KEEP slot — operator intent + health-monitor retry). Different paths, different contracts, both explicit.
- R3 #4: invariant note in `readResource` lookup→reserve sequence documenting the synchronous no-await guarantee that closes the TOCTOU window.

+ 3 new core regression tests (readResource disabled gate, disabled-wins-over-budget precedence, MCP_BUDGET_WARN_FRACTION pin). 629/629 tests pass; typecheck + lint clean.

* fixup(serve): address PR 14 review round 4 (#4247 wenshao second + third pass)

Addresses @wenshao's second + third review passes on PR #4247. One critical scope-correction (per-session vs per-workspace) + one zombie leak fix shared across three threads.

Critical correction — per-session vs per-workspace (wenshao R3 line 117 docs):
- Reality check: `acpAgent.newSessionConfig()` constructs a fresh `Config` + `ToolRegistry` + `McpClientManager` for EVERY ACP session. Each manager independently reads `QWEN_SERVE_MCP_CLIENT_BUDGET` env. So `--mcp-client-budget=10` with 5 sessions caps at 5 × 10 = 50 live MCP clients across the daemon, NOT 10. The "per-workspace" framing in v1 docs was incorrect.
- Pragmatic v1 path (not the big refactor): rewrite docs + change `scope: 'workspace'` → `scope: 'session'` so the wire contract reflects reality. Wave 5 PR 23 (shared MCP pool) will introduce a workspace-scoped manager and add `scope: 'workspace'` cells alongside.
- Files touched: `status.ts` + `sdk types.ts` (cell `scope` field widened to `'session' | 'workspace' | (string & {})` with v1 emitting `'session'`), `acpAgent.buildBudgetCells` (emits `'session'` + new code comment explaining the per-session truth), `docs/users/qwen-serve.md` (CLI flag + budget section relabel + ⚠️ v1 limitation callout), `docs/developers/qwen-serve-protocol.md` (capabilities section + JSON example + paragraph rewrite + per-session detection hint).

Zombie leak fix — single weReserved-pattern fix in discoverMcpToolsForServerInternal closes wenshao R3 line 546 + R4 line 639 + R4 line 929:
- Same pattern as R2 C3 (`readResource`): track `weReservedSlot = reservation === 'reserved' && this.reservedSlots.has(serverName)` (the set-membership guard distinguishes a real fresh reservation from `off`-mode's no-op return). On connect-failure, release slot + drop client only when `weReservedSlot`; an `'already_held'` reconnect keeps its slot so health-monitor retry doesn't compete for capacity.
- Pre-fix a brand-new server connecting via /mcp reconnect / health monitor / incremental's serversToUpdate that failed on connect() would permanently consume a budget slot under enforce mode.
- Updated R3's "always keep" doc comment to reflect the new two-mode cleanup (release on fresh + keep on reconnect).
- Caught and added a tripwire test for the `off`-mode no-op edge case (`tryReserveSlot` returns `'reserved'` without adding to the set in off mode — without the has-guard, my fix would have broken the pre-existing "should restore health checks after failed server rediscovery" test by deleting the failed client even in unbudgeted operation).

+ 2 new core regression tests (fresh-reserve connect-failure releases slot, reconnect connect-failure keeps slot). 631/631 focused tests pass; typecheck + lint clean.

* fixup(serve): address PR 14 review round 5 (#4247 wenshao fourth pass)

Addresses @wenshao's fourth review pass on PR #4247. Two critical zombie-leak / staleness fixes; three reviewer findings deferred or already-addressed (replied + resolved on the threads).

Critical fixes:
- R5 line 956: `runWithDiscoveryTimeout` timeout handler now releases `reservedSlots.delete(serverName)` and drops the stale `lastRefusedServerNames` entry alongside the existing `clients.delete`. Pre-fix a timed-out server in `enforce` mode permanently held its budget slot; N consecutive timeouts permanently degraded daemon capacity. + regression test.
- R5 line 1268-1: `readResource` lazy-spawn path drops the server from `lastRefusedServerNames` when `tryReserveSlot` returns `'reserved'` (a successful late re-reservation). Pre-fix a server refused at discovery but later re-reserved via `readResource` (e.g., after another server freed a slot) kept its stale `disabledReason: 'budget'` tag in the snapshot. + regression test.

Reviewer findings deferred / already done (replied + resolved):
- R5 line 1268-2 (`no try/catch around connect()` in readResource): stale view — R2 C3 fixup ba3e3febd added the try/catch with the weReservedSlot cleanup pattern.
- R5 line 1274 (`BudgetExhaustedError.liveCount` semantic mismatch): R2 S6 fixup ba3e3febd renamed the param + readonly field to `reservedCount`, exactly matching the proposed semantic.
- R5 acpAgent.ts null line (`Math.ceil(0.75 * budget)` for small budgets): proposed fix is semantically a no-op for integer liveCount — `liveCount >= 0.75` and `liveCount >= Math.ceil(0.75) === 1` give identical results when liveCount is an integer. The underlying "small budgets jump ok→error" observation is a real but inherent limitation of percentage-based thresholds at small N; design tradeoff, not implementation bug.

46/46 core tests pass (44 prior + 2 new R5 regression). Typecheck + lint clean.

* fixup(serve): address PR 14 review round 6 (#4247 wenshao fifth pass)

Addresses @wenshao's fifth review pass on PR #4247. Two critical fixes (one TOCTOU race, one cross-daemon env leak).

Critical fixes:
- R6 Thread 2 (line 956): remove the duplicate pre-reservation block in `discoverAllMcpToolsIncremental`. The reservation already happens inside `discoverMcpToolsForServerInternal` (R1 fix #1). With both sites reserving, the timeout cleanup raced against the inner connect path — `runWithDiscoveryTimeout`'s timeout handler could release the slot mid-flight while the inner `connect()` later resolved successfully, leaving a CONNECTED client with NO reservation and breaking `enforce`-mode budget enforcement. With pre-reservation removed, the inner call owns the entire reservation lifecycle (reserve → connect → release-on-failure-via-weReservedSlot → cleared-by-timeout-if-fires) at a single site. Refusal behavior is observably identical from outside.

- R6 Thread 1 (runQwenServe.ts:216): per-handle env passthrough via new `BridgeOptions.childEnvOverrides` instead of mutating global `process.env`. Pre-fix concurrent embedded `runQwenServe()` handles with different MCP budgets would race on the global env — `defaultSpawnChannelFactory` snapshots `process.env` AT SPAWN TIME, so the last `runQwenServe()` call to set the var would silently win for ALL daemon handles' subsequent ACP child spawns. Wire surface:
  - `ChannelFactory` signature: `(workspaceCwd, childEnvOverrides?) => Promise<AcpChannel>`.
  - `BridgeOptions.childEnvOverrides?: Readonly<Record<string, string | undefined>>` — `undefined` value means "scrub this var from the child env" so an embedded caller can wipe a stale inherited var without touching global state.
  - `defaultSpawnChannelFactory` merges overrides AFTER `SCRUBBED_CHILD_ENV_KEYS` so the daemon-only secret list still wins (operators can't override the scrub).
  - `runQwenServe` closes over per-handle overrides; never touches `process.env`.

+ 3 new regression tests (incremental refusal post-pre-reservation-removal, runQwenServe-doesn't-mutate-process.env, bridge forwards childEnvOverrides to channelFactory with two concurrent bridges asserting isolation). 327/327 focused tests pass; typecheck + lint clean.

* fixup(serve): address PR 14 review round 7 (#4247 wenshao sixth pass)

Addresses @wenshao's sixth review pass on PR #4247 (glm-5.1 via Qwen Code /review). One critical staleness fix + four real bug fixes + one operator-visibility breadcrumb + one refactor.

Critical:
- R7 #1 line 612: `discoverMcpToolsForServerInternal` now drops the entry from `lastRefusedServerNames` on successful connect+discover. Pre-fix a previously-refused server that reconnects via `/mcp reconnect` (or health-monitor retry after another server frees capacity) left the snapshot reporting `error / disabledReason: 'budget'` for a CONNECTED, working server until the next discovery pass cleared the per-pass log.

Real bugs:
- R7 #2 line 528: disabled gate added to `discoverMcpToolsForServerInternal`. Reachable from `/mcp reconnect`, OAuth re-discovery, and health-monitor `reconnectServer` — none of which previously checked `isMcpServerDisabled`. Pre-fix a disabled server could be resurrected through any of these paths, wasting a budget slot and registering tools the operator told us to ignore. Mirrors the bulk-discovery + readResource patterns. Optional-chain on the call to stay defensive against test fixtures missing the method.
- R7 #3 line 634: transport leak in the `discoverMcpToolsForServerInternal` connect-failure catch. Pre-fix when `connect()` succeeded (transport established) and `discover()` later threw, the catch deleted the client reference without calling `client.disconnect()`, leaking the stdio child / socket until Node exit. Best-effort `await client.disconnect()` added before the map cleanup.
- R7 #4 line 1302: `readResource`'s `weReservedSlot` now uses the same `reservation === 'reserved' && this.reservedSlots.has(serverName)` guard as `discoverMcpToolsForServerInternal`. Distinguishes a real fresh reservation from `off`-mode's no-op return. Maintenance-trap fix; in `off` mode the cleanup branch never fires now.
- R7 #5 line 1342: `readResource` re-checks `isMcpServerDisabled` on EVERY call, regardless of whether the client was just lazy-spawned or pre-existing. Pre-fix a server connected pre-disable and then operator-disabled mid-session via settings reload still served resource reads via its existing CONNECTED client until the next incremental discovery pass called `removeServer`.

Polish:
- R7 #6 line 191: `readBudgetFromEnv` now emits a stderr breadcrumb when env values are invalid (`QWEN_SERVE_MCP_CLIENT_BUDGET=abc`, `QWEN_SERVE_MCP_BUDGET_MODE=foo`). Pre-fix operator typos silently fell through to "no enforcement". Same pattern as the `--require-auth` boot log.
- R7 #7 line 464: extracted `dropRefusalEntry` (4 sites) + `refuseAndLog` (3 sites) helpers. Pure refactor, zero behavior change. The `readResource` refusal path now calls `refuseAndLog` before throwing `BudgetExhaustedError` so operators get the same stderr trail as bulk-discovery refusals.

+ 5 new core regression tests (refusal-cleared-on-success, internal-disabled-gate, discover-throw-disconnects, env-typo-breadcrumb, existing-client-disabled-rejected). 52/52 core tests pass; typecheck + lint clean.

* fixup(serve): address PR 14 review round 8 (#4247 wenshao seventh pass)

Addresses @wenshao's seventh review pass on PR #4247 (gpt-5.5 + DeepSeek/deepseek-v4-pro via Qwen Code /review). One critical transport leak + three soundness/consistency fixes; one optional clarity refactor explicitly deferred.

Critical:
- R8 #1 line 532 (4 duplicate threads): bulk-path transport leak. Mirrors the R7 #3 fix but in `discoverAllMcpTools` instead of the per-server path. Pre-fix: when `connect()` succeeded (transport established) and `discover()` later threw, the bulk catch deleted the client reference without calling `client.disconnect()`, leaking the stdio child / WebSocket / HTTP socket for the rest of the daemon's lifetime (`stop()` can't see what we just removed from `this.clients`). Best-effort `await client.disconnect()` added before `clients.delete` + `reservedSlots.delete`. Updated the doc comment that misleadingly claimed `stop()` was the lifecycle owner — true only for slot bookkeeping, not transports.

Soundness:
- R8 #2 line 431: tighten `readBudgetFromEnv` mode-without-budget downgrade. Originally only `enforce` got downgraded to `off` when no budget was set; `warn` mode without a budget threshold reached `emitBudgetTelemetry` with `clientBudget: undefined`, contradicting the JSDoc invariant `mode !== 'off' ⇒ clientBudget defined`. Now both `enforce` AND `warn` downgrade to `off` when no budget is configured. The invariant comment was also weakened to match the actual `?? 0` defense-in-depth (the new R8 #5 constructor downgrade closes the remaining edge case).

- R8 #5 line 302: constructor mirrors the `readBudgetFromEnv` downgrade for the direct `budgetConfig` parameter. All production callers (CLI, `runQwenServe`, env-var fallback) validate upfront, but a future code path that injects `budgetConfig` directly without re-validating would re-introduce the silent fail-open. Defense in depth.

- R8 #4 line 1221: distinguish fresh vs `'already_held'` reservations in `runWithDiscoveryTimeout`'s timeout handler. New private `freshReservations: Set<string>` field marked when `weReservedSlot === true` inside `discoverMcpToolsForServerInternal` and cleared in finally / catch / success. Timeout handler now releases the slot ONLY when `freshReservations.has(serverName)` — meaning the slot was freshly reserved by THIS in-flight call. `'already_held'` reconnect timeouts (a previously-healthy server's transient hiccup) keep the slot so health-monitor retry doesn't have to compete for capacity with new servers admitted during the timeout window. Aligns the timeout handler with the connect-failure catch's `weReservedSlot` semantics — closes the asymmetry wenshao R8 #4 caught.

Deferred:
- R8 #3 line 332 (`tryReserveSlot` `'observed'` return value clarity): optional, non-blocking style improvement that ripples through 3 call sites + many tests for zero behavior change. Worth doing in a focused refactor PR; flagged as deferred polish, not in this fixup.

+ 3 new core regression tests (bulk discover-throw disconnects, warn-no-budget downgrade, constructor enforce downgrade). 679/679 focused tests pass; typecheck + lint clean.

* fixup(serve): address PR 14 review round 9 (#4247 wenshao eighth pass)

Addresses @wenshao's eighth review pass on PR #4247 (glm-5.1 via Qwen Code /review). Six actionable findings adopted; two threads explained as not-actionable (one stale-view, one reviewer hallucination).

Critical / real bugs:
- R9 #2 line 1534: `readResource` lazy-spawn connect-failure catch now does best-effort `await client.disconnect()` BEFORE `clients.delete` + `reservedSlots.delete`. Mirror of R7 #3 (per-server discovery) and R8 #1 (bulk discovery) — closes the same transport-leak class for the third spawn path. Pre-fix: connect() establishing the transport but throwing on a later handshake step would orphan the stdio child / socket.
- R9 #6 line 1521: `readResource` lazy `client.connect()` now wraps in `Promise.race` against `discoveryTimeoutFor(serverConfig)` — same per-server timeout the bulk + incremental paths use. Pre-fix a hung MCP server during a resource-read spawn would block forever and permanently consume a budget slot under enforce mode, cascading into total budget exhaustion. `serverConfig` lookup hoisted to the top of `readResource` so both lazy-spawn and existing-client branches use identical timeout behavior.
- R9 #8 line 1514: `readResource` lazy spawn now calls `this.startHealthCheck(serverName)` after a successful connect. Pre-fix a lazy-spawned server that later disconnected (crash, network) had no automatic reconnect — sat DISCONNECTED until the next readResource or incremental pass. Mirrors `discoverMcpToolsForServerInternal`'s finally-block pattern.

Operator-visibility:
- R9 #7 (general): `readBudgetFromEnv` now writes a stderr breadcrumb when the `(enforce|warn)`-without-budget downgrade fires. Pre-fix a Docker Compose / k8s env that set `QWEN_SERVE_MCP_BUDGET_MODE=enforce` but forgot the matching `_BUDGET=N` would silently boot with enforcement off and `mcp_guardrails` capability advertised — operator only signal was the snapshot's `budgetMode: 'off'`. Now mirrors the R7 #6 invalid-value breadcrumb pattern.

Doc fixes:
- R9 #4 line 81: `McpBudgetConfig.clientBudget` JSDoc now reflects the R4 per-session scope correction. The doc was a leftover from the original "per-workspace" framing — every other doc surface (protocol doc, user doc, type comments on the snapshot cell, capability tag) was rewritten in R4 except this one.
- R9 #5 line 870: `acpAgent.buildBudgetCells` now spells out the `liveCount` (`accounting.total`, CONNECTED only — operator observability) vs `reservedSlots.size` (all reserved including in-flight — enforcement) semantic distinction. The intentional gap was undocumented in the type signatures, JSDoc, and protocol doc; future PR 14b SSE event payloads should reference both.

Not adopted:
- R9 #1 acpAgent:15: claimed "MCP_BUDGET_WARN_FRACTION not exported + getMcpClient* methods don't exist + 4 tsc errors" — verified incorrect: the constant IS exported (mcp-client-manager.ts:61), the 3 methods ARE class members (lines 379, 407, 412), and `npm run typecheck` is clean across all 4 workspaces. Reviewer's tool hallucinated this critical finding.
- R9 #3 mcp:410: reported the bulk-path transport leak that R8 #1 (commit 7228813c5) had already closed. Reviewer was on the pre-R8 commit view.

+ 2 new core regression tests (readResource lazy connect-fail disconnects + R9 #7 stderr breadcrumb). 57/57 core tests + 679/679 focused suite pass. Typecheck + lint clean.

* fixup(serve): address PR 14 review round 10 (#4247 wenshao ninth pass)

Two non-blocking 🟢 nits — both adopted for symmetry / explicitness.

- R10 line 357: constructor downgrade now emits the same stderr breadcrumb the env-var path got in R9 #7. Pre-R10 the `(enforce|warn)`-without-budget downgrade was silent for the direct-`budgetConfig` path, so a future caller bypassing CLI / env-var validation would have shipped a daemon advertising `mcp_guardrails` while silently disabling enforcement. Now boot logs surface the misconfiguration uniformly across all three resolution paths.
- R10 line 1572: documented the `McpClient.disconnect()` cancel-pending-connect contract that the timeout-race cleanup relies on across all three spawn paths (lazy `readResource`, bulk `discoverAllMcpTools`, per-server `discoverMcpToolsForServerInternal`). The bulk path's production stability since #3889 is implicit evidence the contract holds; comment makes the assumption discoverable to the next reader and notes a follow-up unit test would be valuable. No behavior change.

57/57 core tests pass. Typecheck + lint clean.
2026-05-18 12:07:23 +08:00
jinye
f44ed09412
feat(serve): preflight and env diagnostics routes (#4175 Wave 3 PR 13) (#4251)
* feat(serve): introduce ServeErrorKind and BridgeTimeoutError (#4175 Wave 3 PR 13)

Lay the type foundation for `/workspace/preflight` and `/workspace/env` (and
the eventual MCP guardrails route) so cells emitted by all three share a
closed `errorKind` taxonomy:

- `SERVE_ERROR_KINDS` literal-list + `ServeErrorKind` union — the seven
  values from #4175 (`missing_binary`, `blocked_egress`, `auth_env_error`,
  `init_timeout`, `protocol_error`, `missing_file`, `parse_error`).
- `BridgeTimeoutError` typed class — `withTimeout` now rejects with this
  rather than a plain `Error`, letting `mapDomainErrorToErrorKind` recognize
  init / heartbeat / extMethod timeouts via `instanceof` instead of
  regex-matching message strings. Message format is preserved bit-for-bit.
- `mapDomainErrorToErrorKind` helper — one place to classify
  `BridgeTimeoutError`, `SkillError`, fs ENOENT/EACCES/EPERM, ModelConfigError
  subclasses (recognized by `name` field — they aren't on the public surface
  of `@qwen-code/qwen-code-core`), `SyntaxError`, plus message-regex fallbacks
  for legacy throw sites (`agent channel closed`, missing CLI entry path).
- `ServeStatusCell.errorKind` tightened from open `string` to the closed
  `ServeErrorKind` union. Backward compatible — PR 12 never assigned the
  field.
- SDK mirrors: `DAEMON_ERROR_KINDS` const + `DaemonErrorKind` type;
  `DaemonStatusCell.errorKind` tightened.

Tests: 11 new unit tests in `status.test.ts` covering each mapping rule plus
the BridgeTimeoutError shape.

No route changes; no behavior changes for any existing path.

* feat(serve): add buildEnvStatusFromProcess helper (#4175 Wave 3 PR 13)

Pure helper that constructs the `/workspace/env` payload from `process.*`
state. No I/O, no ACP roundtrip, no globals beyond `process.env`. The route
itself lands in the next commit.

- `ServeEnvKind` discriminant: `runtime | platform | sandbox | proxy | env_var`
- `ServeEnvCell extends ServeStatusCell` with `name` + optional `present` /
  `value`. Cells with `kind: 'env_var'` are presence-only — `value` is
  ALWAYS omitted to keep secret env vars off the wire even by accident.
- `ServeWorkspaceEnvStatus` envelope: `{ v, workspaceCwd, initialized: true,
  acpChannelLive, cells, errors? }`. `initialized` is structurally `true`
  because env answers from the daemon process directly; `acpChannelLive`
  reports whether a child is up but does not change the payload shape.

Whitelist policy:
- Auth/secret keys (presence-only): OPENAI/ANTHROPIC/GEMINI/GOOGLE/DASHSCOPE/
  OPENROUTER `_API_KEY`, `QWEN_SERVER_TOKEN`.
- Non-secret keys (also presence-only for shape uniformity): base URLs, locale,
  TZ, NODE_EXTRA_CA_CERTS, QWEN_CLI_ENTRY.
- Proxy vars (`HTTP_PROXY`/`HTTPS_PROXY`/`NO_PROXY`/`ALL_PROXY` + lowercase
  variants): credentials stripped via `redactProxyCredentials`, then
  `URL().host` so the wire only carries `host:port`. NO_PROXY is a host list
  rather than a URL so we pass the redacted form verbatim.

SDK mirrors: `DaemonEnvKind`, `DaemonEnvCell`, `DaemonWorkspaceEnvStatus`.

Tests: 9 unit tests covering the proxy-credential redaction, lowercase env
fallback, NO_PROXY pass-through, presence-only `env_var` invariant
(`'value' in cell === false`), whitelist enforcement, runtime tag detection,
and envelope shape.

* feat(serve): add GET /workspace/env route (#4175 Wave 3 PR 13)

Wire `buildEnvStatusFromProcess` from the previous commit through the
bridge, server, and SDK so remote clients can pre-flight the daemon's
runtime environment without spawning an ACP child.

- `workspace_env` capability tag (always advertised on a current daemon).
- `bridge.getWorkspaceEnvStatus()` answers entirely from `process.*` —
  the route never consults ACP. `acpChannelLive` reflects whether a child
  exists but does not change the payload, so an idle daemon and a busy
  one return the same env shape.
- `app.get('/workspace/env', ...)` mirrors PR 12's one-liner pattern.
- SDK: `DaemonClient.workspaceEnv()` returning `DaemonWorkspaceEnvStatus`.
- Docs: bullet in `docs/users/qwen-serve.md` calling out the
  presence-only redaction policy and the no-ACP-spawn guarantee.

Tests: server-level (env returned + `'value' in env_var === false`
assertion), bridge-level (idle and live both answer locally without
hitting ACP extMethod), SDK-level (recording-fetch round-trip on
`/workspace/env`). The `workspace_env` tag is added to the
`EXPECTED_STAGE1_FEATURES` capability list assertion.

* feat(serve): add /workspace/preflight daemon-cells path (#4175 Wave 3 PR 13)

Wire the preflight route. Daemon-level cells are populated unconditionally
from `process.*` and `node:fs`; ACP-level cells fall back to `not_started`
placeholders when no child is alive so a poll never spawns one.

- `workspace_preflight` capability tag.
- `ServePreflightKind` discriminant (12 values: node_version, cli_entry,
  workspace_dir, ripgrep, git, npm — daemon-level — plus auth, mcp_discovery,
  skills, providers, tool_registry, egress — ACP-level).
- `ServePreflightCell extends ServeStatusCell` with `locality: 'daemon' | 'acp'`
  + free-form `detail`. `ServeWorkspacePreflightStatus` envelope.
- `createIdleAcpPreflightCells()` factory: emits the six ACP-level cells with
  `status: 'not_started'` + a uniform `hint` so the bridge can stitch them in
  alongside daemon cells without ever calling ACP.
- `bridge.getWorkspacePreflightStatus()`:
  - Daemon cells via `buildDaemonPreflightCells` (Promise.all over Node-version,
    CLI-entry resolution mirroring `defaultSpawnChannelFactory`, `fs.stat` on
    `boundWorkspace` with ENOENT/EACCES/EPERM mapped to `missing_file`,
    best-effort `canUseRipgrep` / `getGitVersion` / `getNpmVersion` warnings).
  - ACP cells via `requestWorkspaceStatus` — idle factory returns the
    `not_started` placeholders; live path delegates to ACP via the
    `qwen/status/workspace/preflight` ext method (handler lands in next
    commit). Bridge-side timeout / channel-close while consulting ACP folds
    into envelope `errors[]` with `mapDomainErrorToErrorKind` classification;
    daemon cells still render.
- `app.get('/workspace/preflight', ...)` route + JSDoc bullet.
- SDK: `DaemonPreflightKind` / `DaemonPreflightCell` / `DaemonWorkspacePreflightStatus`
  mirrors; `DaemonClient.workspacePreflight()`.

Tests: server-level (route returns the bridge payload), bridge-level (idle
returns 6 daemon + 6 ACP `not_started` cells without spawning a channel),
SDK-level (`workspacePreflight()` round-trip). Capability test updated.

* feat(serve): wire ACP-side preflight cells (#4175 Wave 3 PR 13)

Populate the six ACP-level preflight cells inside the ACP child so
`/workspace/preflight` returns real values for live sessions.

- `extMethod(qwen/status/workspace/preflight, ...)` dispatches to a new
  `buildAcpPreflightCells(config)` private method.
- Five cell builders, each returning a `ServePreflightCell` with
  `locality: 'acp'`:
  - `auth`: `validateAuthMethod(authType, config)` returning non-null
    string → `auth_env_error`. Missing auth method → warning. Throws
    classified via `mapDomainErrorToErrorKind` with `auth_env_error`
    fallback.
  - `mcp_discovery`: rolls up `getMCPDiscoveryState()` + per-server
    `getMCPServerStatus(name)` counts. `connecting > 0` or in-progress
    discovery → warning + `init_timeout`; `disconnected > 0` post-discovery
    → error + `protocol_error`.
  - `skills`: `SkillManager.listSkills()`; SkillError throws are mapped
    via the helper (`PARSE_ERROR` → `parse_error`, `FILE_ERROR` →
    `missing_file`).
  - `providers`: `getAllConfiguredModels()`; empty list with a configured
    `authType` → warning + `auth_env_error`. ModelConfigError throws map
    to `auth_env_error`.
  - `tool_registry`: null registry → error + `protocol_error`. Otherwise
    surfaces tool count.
- `egress`: stays `not_started`. PR 14 plugs in the real probe.
- `errorCell` private helper extended with optional `errorKind` parameter;
  defaults to `mapDomainErrorToErrorKind(error)` so existing call sites
  (`mcp` / `skills` / `providers` envelope errors) automatically gain
  classification.

Tests: 2 new acpAgent tests — preflight returns the six expected ACP cells
with correct locality + statuses; preflight surfaces a `SkillError`
(`PARSE_ERROR`) on the `skills` cell as `errorKind: 'parse_error'`. The
core `vi.mock` block adds a SkillError class for `instanceof` matching
inside `mapDomainErrorToErrorKind`.

* docs(serve): preflight and env protocol section (#4175 Wave 3 PR 13)

Document `/workspace/env` and `/workspace/preflight` end-to-end:

- Common-cell shape: tighten `errorKind` from open `string` to the closed
  `DaemonErrorKind` enum (seven literals from #4175). Add an explicit
  redaction-policy paragraph covering env-var presence-only, proxy
  host:port reduction, and the whitelisted-secrets list.
- Capability-tag list: add `workspace_env` and `workspace_preflight`.
- New `### GET /workspace/env` section with sample payload, `DaemonEnvKind`
  / `DaemonEnvCell` types, and the redaction-policy paragraph spelling
  out which secret env vars are enumerated and how proxy URLs are
  reduced to `host:port`.
- New `### GET /workspace/preflight` section with idle sample payload,
  `DaemonPreflightKind` / `DaemonPreflightCell` types, the seven-value
  `errorKind` semantics table, and the bridge-error fallback contract
  (mid-request ACP channel close → cells drop to `not_started` + envelope
  carries one `errors[]` entry).
- Source-layout table: extend the `status.ts` row to mention the new
  `ServeErrorKind` / `BridgeTimeoutError` / `mapDomainErrorToErrorKind`
  surface; add a new `envSnapshot.ts` row.
2026-05-18 07:29:05 +08:00
jinye
60fe594e8f
feat(serve): add read-only status routes (#4241)
* feat(serve): add read-only status routes

Add read-only daemon status endpoints for workspace MCP, skills, providers, session context, and session supported commands.

Expose matching typed SDK helpers and document the new additive v1 status surface.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(serve): harden read-only status snapshots

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(serve): address read-only status review feedback

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

---------

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-17 21:37:20 +08:00
jinye
aef35c390e
feat(serve): session metadata and close/delete lifecycle (#4175 Wave 2.5 PR 11) (#4240)
* feat(serve): session metadata and close/delete lifecycle (#4175 Wave 2.5 PR 11)

Add explicit session close and metadata management to the daemon serve
infrastructure, closing the Stage 1 limitation that sessions could only
end via child crash or daemon shutdown.

- DELETE /session/:id — force-closes a live session (cancels active
  prompt, resolves pending permissions, publishes session_closed event)
- PATCH /session/:id/metadata — update mutable displayName
- Enriched GET /workspace/:id/sessions with createdAt, displayName,
  clientCount, hasActivePrompt
- session_closed + session_metadata_updated SDK event types with
  validation, reducer, and terminal event priority
- DaemonClient.closeSession / updateSessionMetadata + session client
  wrappers
- Capabilities: session_close, session_metadata

* fix(serve): address review feedback on session lifecycle PR

- Fix JSDoc on closeSession: clarify that bridge throws SessionNotFoundError
  (SDK absorbs 404 for client-side idempotency)
- Tighten event validators: isSessionClosedData checks closedBy type,
  isSessionMetadataUpdatedData checks displayName type
- PATCH /session/:id/metadata now returns effective stored metadata
  instead of echoing request fields, avoiding ambiguous no-op responses
- Only publish session_metadata_updated event when displayName changes
- Update chooseTerminalEvent comment to reflect session_closed

* fix: address PR 4240 review feedback

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix: address remaining PR 4240 suggestions

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix: update serve sessions test mock

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

---------

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-17 20:42:15 +08:00
jinye
d2d426fad0
feat(serve): SSE replay sizing + slow_client_warning backpressure (#4175 Wave 2.5 PR 10) (#4237)
* feat(serve): SSE replay sizing + slow_client_warning backpressure

#4175 Wave 2.5 PR 10. Closes the SSE replay / backpressure knobs
called out in #3803 §02 so chatty Stage 1 sessions get an honest
reconnect window and operators get a heads-up signal before clients
are summarily evicted.

- **`DEFAULT_RING_SIZE` 4000 → 8000.** Per-session replay ring depth
  now matches the #3803 §02 target for chatty sessions.
- **`--event-ring-size <n>`** CLI flag (default 8000) lets operators
  tune the ring per daemon. Threaded `ServeOptions` →
  `BridgeOptions.eventRingSize` → both `new EventBus()` construction
  sites (fresh sessions + restore path). Validation is fail-CLOSED
  (positive finite integer; 0 / NaN / negative throw at boot).
- **`slow_client_warning` SSE frame.** When a subscriber's queue
  crosses 75% full the bus force-pushes a synthetic
  `slow_client_warning` to that subscriber once per overflow
  episode, carrying `{queueSize, maxQueued, lastEventId}`. The flag
  re-arms after the queue drains below 37.5% (hysteresis, no flap
  near threshold). If the queue actually overflows after the
  warning, the existing `client_evicted` terminal frame path still
  fires. Like `client_evicted`, the warning has no `id` (synthetic
  frame; must not burn a sequence slot for other subscribers).
- **`?maxQueued=N`** query param on `GET /session/:id/events`
  (range `[16, 2048]`, default 256). Lets cold reconnect clients
  pre-size their per-subscriber backlog so a large `Last-Event-ID:
  0` replay doesn't trip the warning on the first publish. Range
  rationale: lower bound 16 (smaller is useless for any replay);
  upper bound 2048 (so a single subscriber can't pin ~1 MB just by
  asking). Out-of-range / non-decimal returns `400
  invalid_max_queued` BEFORE opening the SSE stream — clean 4xx
  beats half-opening a stream + emitting a `stream_error` (which
  EventSource would auto-reconnect on).
- **`slow_client_warning` capability tag** — single source of truth
  for the warning frame + `?maxQueued` query param + ring-size
  knob. Old daemons silently lack all of these; pre-flight via
  `caps.features`.
- **SDK extensions** (`@qwen-code/sdk`): typed
  `DaemonSlowClientWarningEvent` (added to known event union and
  `DaemonStreamLifecycleEvent`); schema-validated by a new
  `isSlowClientWarningData` predicate; reducer
  (`reduceDaemonSessionEvent`) increments `slowClientWarningCount`
  + stores `lastSlowClientWarning`. Warning is **non-terminal** —
  `alive` stays true (only `client_evicted` / `stream_error` /
  `session_died` close the stream). Re-exported from the public
  SDK entry.
- **Docs**: `qwen-serve-protocol.md` updates the features list (adds
  `slow_client_warning` and the previously-missing `client_identity`
  to match reality post-#4231), documents the `?maxQueued` query
  param, adds the warning frame to the event table, and notes the
  new default ring size. `qwen-serve.md` adds the `--event-ring-size`
  flag row.

Tests: 19 eventBus (4 new: warning at 75%, once per episode,
no `id` on the synthetic frame, hysteresis re-arm), 106 bridge
(2 new: validate eventRingSize accept/reject), 111 server (4 new:
?maxQueued accept/absent/non-decimal/out-of-range +
EXPECTED_STAGE1_FEATURES update), 14 SDK daemonEvents (2 new:
schema validation + non-terminal reducer behavior). 321 focused
tests total, all green.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* refactor(serve): adopt PR #4237 review feedback (eventBus polish)

Address the actionable items from the Qwen Code review bot's pass
on PR #4237:

- Pre-compute `warnThreshold` / `warnResetThreshold` per
  `InternalSub` at `subscribe()` time so `publish()`'s per-event
  hot path is one integer compare per subscriber instead of a
  multiply + compare. The `!warned` short-circuit still collapses
  the steady state to a single boolean read; this just shaves a
  multiply when the threshold check actually fires.
- Document the back-of-queue ordering choice for the synthetic
  `slow_client_warning` frame in `EventBus.publish()`: front-push
  was considered but mid-stream front-insertion would mis-count
  `forcedInBuf` in `BoundedAsyncQueue.next()`, and `forcePush`
  already short-circuits via `resolvers.shift()` for the
  active-consumer case — the back-of-queue path only matters for
  stalled consumers, who can't drain regardless of warning
  position.
- Reuse the existing `collect()` helper in the "default ring size
  8000" test for consistency with the rest of the file; the new
  test also tightens the assertion by checking that the first
  retained event id is 2 (id=1 dropped by the ring) and the last
  is 8001.
- Soften the "~500 B per session" magic number in
  `BridgeOptions.eventRingSize`'s JSDoc to a qualitative
  description (each retained `BridgeEvent` is a reference plus its
  serialized payload; ceiling scales as
  `ringSize × average-event-size`).

Rejected:
- Bot's claim that the error JSON contains `\`...\`` escape
  sequences — bot misread the JS template-literal source as the
  wire output; `JSON.stringify` does not escape backticks, and
  the existing `cwd` error messages use the same style.
- Bot's "use `Record<string, never>` instead of `[key: string]:
  unknown`" suggestion on `DaemonSlowClientWarningData` — every
  other event-data type in `sdk-typescript/src/daemon/events.ts`
  carries the same index signature for additive-field
  compatibility.
- Bot's "features list breaks alphabetical order" — the
  capability list is grouped by protocol lifecycle (health →
  capabilities → session lifecycle → events → permissions), not
  alphabetical.

Tests: 139 focused tests across eventBus + httpAcpBridge + SDK
daemon events — all passing. Behavior unchanged; this is
hot-path micro-opt + comment polish only.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): correct queue tagging + plumb maxQueued through SDK

Address both P2 findings from the Codex review pass on PR #4237.

**Bug 1: `BoundedAsyncQueue.forcedInBuf` position-invariant break**

The previous `forcedInBuf` counter only tracked LIVE-vs-FORCED
correctly when all forced entries lived at the FRONT of the buffer
(subscribe-time `Last-Event-ID` replay). The new mid-stream
`slow_client_warning` path force-pushes to the BACK of the queue
while the queue is still open, which the existing accounting was
not designed for:

  - publish 6 events at maxQueued=8 → 75% threshold trips →
    force-push warning at the back → buf=[1..6, warning],
    forcedInBuf=1.
  - consumer shifts `1` → forcedInBuf decremented to 0 (incorrect:
    `1` was a live frame, not the forced one).
  - consumer drains 2..6 + warning → buf=[], forcedInBuf=0, true
    live count = 0, but `size` getter and `push()` cap check then
    use `buf.length - forcedInBuf` which drifts over subsequent
    refills, causing premature warn / eviction before the cap is
    actually reached.

Replace the position-dependent counter with a per-entry
`{value, forced}` tag. `liveCount` is incremented in `push()` /
decremented in `next()` only when the shifted entry was non-forced
— position becomes irrelevant. `size` getter returns `liveCount`
directly. The class doc comment is rewritten to call out that the
new tag is the position-independent replacement for the old
"forced frames must stay at the front" invariant.

Regression test in `eventBus.test.ts` reproduces the codex trace
(warn at 75%, drain past warning, refill to cap) and asserts no
premature eviction.

**Bug 2: SDK does not expose `?maxQueued`**

`docs/users/qwen-serve.md` and `docs/developers/qwen-serve-protocol.md`
both document `?maxQueued=N` as something SDK clients can request,
but `SubscribeOptions` on `DaemonClient` only declared `lastEventId`
+ `signal`, and `subscribeEvents()` always fetched `/events` without
a query string. Typed-SDK consumers had no way to opt in without
hand-crafting URLs.

  - Add `SubscribeOptions.maxQueued?: number` with JSDoc noting the
    daemon range `[16, 2048]` and the pre-flight requirement on
    `caps.features.slow_client_warning`.
  - `DaemonClient.subscribeEvents` builds the URL with an optional
    `?maxQueued=<n>` segment. No client-side range validation —
    the daemon's `parseMaxQueuedQuery` is the source of truth and
    returns structured `400 invalid_max_queued`; duplicating the
    bounds in two layers would diverge on the next tweak.
  - `DaemonSessionSubscribeOptions extends SubscribeOptions` so the
    new field flows through `DaemonSessionClient` automatically.

Three new SDK tests:
  - subscribeEvents appends `?maxQueued=N` when set
  - omits the query string when absent (existing behavior preserved)
  - propagates a `400 invalid_max_queued` unchanged

Tests: 214 focused tests across eventBus / bridge / SDK
DaemonClient / DaemonSessionClient / daemonEvents, plus 111 in the
server suite. All green; the new eventBus regression case proves
the position-invariant fix.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* refactor(serve): adopt PR #4237 copilot review feedback

Address 6 of 8 copilot-reviewer findings on PR #4237; the other 2
(#1 forcedInBuf live-size corruption, #5 SDK lacks maxQueued) were
already fixed in bae42c88b — replied on the threads with the
commit hash.

- **[2] server.ts:1068** — `?maxQueued=` (present-but-empty) now
  fails closed with `400 invalid_max_queued` instead of silently
  falling back to the default queue cap. The API documents
  fail-closed for any malformed value before opening SSE, so an
  empty string is unambiguously malformed. New server.test.ts
  case locks this in.
- **[3] commands/serve.ts:93** — CLI help text for
  `--event-ring-size` no longer mis-shapes `Last-Event-ID` as a
  query parameter. It is an HTTP header, and the daemon's SSE
  route does not parse a `?Last-Event-ID=` query.
- **[4] docs/developers/qwen-serve-protocol.md:351** — clarify
  that `?maxQueued=N` controls the LIVE-event backlog cap.
  Replay frames are force-pushed and exempt from the cap; what
  consumes it is live events that arrive while the subscriber is
  still draining a cold-reconnect replay. Bumping for cold
  reconnects is still the right answer, but for the live tail,
  not for the replay frames themselves.
- **[6] eventBus.ts:214** — stale `ringSize=4000` performance
  comment updated to the new `ringSize=8000` default with a note
  about the O(n) `shift()` cost scaling.
- **[7] sdk-typescript events.ts:492** — `isSlowClientWarningData`
  now uses the existing `isFiniteNumber` helper instead of bare
  `typeof === 'number'`. Mirrors the sibling predicates and
  rejects `NaN` / `Infinity` payloads as schema garbage. New
  daemonEvents.test.ts assertions cover both.
- **[8] server.ts:127** — `createServeApp`'s default-bridge
  construction now also forwards `opts.eventRingSize` to
  `createHttpAcpBridge`, symmetric with the `runQwenServe.ts`
  path. Direct embeds / tests that called `createServeApp`
  without supplying their own bridge but did pass
  `ServeOptions.eventRingSize` were silently getting the
  default 8000 ring.

Tests: 326 focused tests across eventBus / bridge / SDK
DaemonClient / DaemonSessionClient / daemonEvents / server. All
green; the new server.test.ts case + the extended
daemonEvents.test.ts assertions cover the tightened guards.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* refactor(serve): adopt PR #4237 wenshao round-2 review feedback

Six adopted findings from @wenshao's second review pass on
PR #4237. The seventh ([10] forcedInBuf 3rd case invariant) was
already fixed in bae42c88b — replied on that thread.

- **[9] + [14] server.ts** — Sanitize attacker-controlled values
  before stderr interpolation in both `parseMaxQueuedQuery` and
  `parseLastEventId`. New `safeLogValue()` helper uses
  `JSON.stringify` to escape control characters (`\n`/`\r`/…) so a
  URL-encoded newline in `?maxQueued=%0a` can't inject extra log
  lines into journald/Loki/Splunk pipelines. Matches the
  `workspace_mismatch` sanitization style in `sendBridgeError`.
  Fixed in both helpers (the sibling pre-existing
  `parseLastEventId` had the same shape) so the file stays
  consistent.

- **[11] httpAcpBridge.ts** — `!Number.isFinite(eventRingSize)`
  was redundant: `Number.isInteger(NaN)` and
  `Number.isInteger(Infinity)` both return `false`, so the sibling
  `!Number.isInteger` already catches both. Drop the dead guard.

- **[12] httpAcpBridge.ts** — Add soft upper bound
  `MAX_EVENT_RING_SIZE = 1_000_000` on `eventRingSize` to catch
  operator typos (`--event-ring-size 80000000` vs `8000000`). At
  ~500 B per `BridgeEvent` an 1M-frame ring already pins ~500 MB
  per session — well past any realistic workload. Not a security
  boundary (operator-controlled flag), pure typo defense. Existing
  bridge construction test extended with an `80_000_000` case.

- **[13] commands/serve.ts** — CLI `--event-ring-size` flag now
  sources its default from `DEFAULT_RING_SIZE` (imported from
  `serve/eventBus.js`) instead of the hardcoded literal `8000`.
  Without this, a future bump of the bus default would silently
  not take effect for daemons launched through the CLI because
  the flag always overrides — single source of truth fixes that.

- **[15] eventBus.ts** — Drop unreachable `event.id ?? this.lastEventId`
  fallback in the `slow_client_warning` frame. `event` is locally
  constructed at the top of `publish()` with `id: this.nextId++`
  and is guaranteed defined. Use `event.id as number` directly +
  an inline note about the invariant.

Tests: 197 (eventBus 20 / bridge 107 / SDK DaemonClient 57 / SDK
daemonEvents 14) + 112 server. All green; the new upper-bound
bridge case + the existing log assertions pin the changed
behaviors.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

---------

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-17 19:30:43 +08:00
jinye
0a4a08e443
feat(serve): add client heartbeat (#4175 Wave 2.5 PR 9) (#4235)
* feat(serve): add client heartbeat route

Adds POST /session/:id/heartbeat plus SDK helpers so long-lived
adapters (TUI/IDE/web) can refresh the daemon's last-seen
bookkeeping. Bridge stores per-session and per-client timestamps
behind a getHeartbeatState() snapshot accessor that PR 12
read-only diagnostics and PR 24 revocation policy will consume.

- Capability tag: client_heartbeat (advertised on /capabilities.features)
- Identified clients must echo X-Qwen-Client-Id; the bridge validates
  the id BEFORE bumping any timestamp so a forged id can't mask
  client absence
- Per-client entries are dropped together with the registration
  ref-count in unregisterClient, so churn doesn't leak stale ids
- getHeartbeatState returns a snapshot Map; mutating it does not
  leak into bridge state
- Anonymous heartbeats bump only the per-session watermark

Errors mirror the rest of the routes — 404 SessionNotFoundError, 400
invalid_client_id (header malformed or unknown for this session).

Roadmap PR 9 from #4175. Depends on PR 7 (#4231 client identity,
merged) for the trusted clientId registry.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* feat(sdk): re-export HeartbeatResult from package root

The published @qwen-code/sdk only exposes the root entrypoint via
`exports`; daemon subpath imports are not part of the public API.
Adding HeartbeatResult to packages/sdk-typescript/src/daemon/index.ts
made it reachable internally but not for downstream consumers writing
`import type { HeartbeatResult } from '@qwen-code/sdk'` — every other
daemon result type (PromptResult, SetModelResult, DaemonSession, etc.)
is forwarded through the root barrel, so HeartbeatResult was the only
hole in the heartbeat helper's public surface.

Inserted alphabetically between DaemonStreamLifecycleEvent and
KnownDaemonEvent to match the existing ordering convention.
2026-05-17 18:57:28 +08:00
jinye
07e0e82258
feat(serve): advertise typed_event_schema + pin SDK public surface (#4175 PR 4 follow-up) (#4226)
* feat(serve): advertise typed_event_schema capability

Follow-up to #4217 (`feat(protocol): add typed daemon event schema v1`,
Wave 1 PR 4 of #4175), which landed the SDK-side typed schema +
`KnownDaemonEvent` union + reducer but did not register a daemon-side
capability tag for it. Without the tag, non-SDK clients (web debug
UI, third-party adapters, channel/IDE backends not yet on
`@qwen-code/sdk`) have no way to detect at the protocol envelope
level that the daemon promises to emit only `KnownDaemonEvent`-shaped
frames — they would either pin against SDK version, or pre-flight
every frame defensively.

Add `typed_event_schema: { since: 'v1' }` to `SERVE_CAPABILITY_REGISTRY`,
inserted right after `session_events` (the route that delivers the
frames whose schema this tag describes). The capability is purely
informational — `narrowDaemonEvent`/`asKnownDaemonEvent` already
fall back to "unknown" for older daemons that don't advertise the
tag, so the SDK does not gate any behavior off the tag.

Sync `EXPECTED_STAGE1_FEATURES` (server.test.ts) and the integration
test array (qwen-serve-routes.test.ts) with the registry order, the
same lockstep discipline #4214 codified.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* test(sdk): pin typed event surface at the public SDK entry, point DaemonSessionClient docstring at it

Two small follow-ups to #4217 (Wave 1 PR 4 of #4175).

1. Public-entry regression fence

   `@qwen-code/sdk` is a single-entry package: `package.json.exports`
   only exposes `.` (`dist/index.{cjs,mjs,d.ts}`), and the bundle
   is built from `src/index.ts`. Symbols re-exported only from
   `src/daemon/index.ts` are unreachable to consumers unless they
   are also forwarded by `src/index.ts`. #4217 forwards the typed
   event schema correctly today, but the two-layer chain has no
   compile-time test pinning it — a future daemon export that lands
   in `src/daemon/index.ts` but is missed by `src/index.ts` would
   ship invisibly.

   Add `test/unit/daemon-public-surface.test.ts` that imports
   `* as Public from '../../src/index.js'`, asserts at runtime that
   every PR 4 value is `typeof === 'function'` (or a primitive of
   the expected shape), round-trips a raw `DaemonEvent` through the
   public `asKnownDaemonEvent` to prove the wire-up actually works,
   and compile-imports every PR 4 type so any drift fails to build.

2. DaemonSessionClient docstring pointer

   The class docstring already deferred typed event consumption to
   "the protocol schema layer" without a concrete pointer. Now that
   #4217 has put `asKnownDaemonEvent` and `reduceDaemonSessionEvent`
   in `./events.js`, name them so future readers can find the
   typed surface without grepping. No code change.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)
2026-05-17 18:43:38 +08:00
ChiGao
c25e22b575
feat(serve): add session-scoped permission route (#4232)
Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
2026-05-17 17:48:30 +08:00
ChiGao
4d9cbe49c0
feat(serve): add daemon-stamped client identity (#4231)
* feat(serve): add daemon-stamped client identity

* fix(serve): harden daemon client identity handling

---------

Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
2026-05-17 16:19:30 +08:00
ChiGao
b90a2c91c9
feat(sdk): harden daemon session client (#4225)
Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
2026-05-17 15:05:37 +08:00
jinye
2453b82add
[codex] Add daemon session load/resume (#4222)
* feat(serve): add daemon session load resume

Adds HTTP and SDK support for restoring persisted daemon sessions through load/resume routes, including replay buffering for load and guarded concurrent restore handling.

Refs #4175

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(serve): address review feedback on daemon session load/resume

- Gate `defaultEntry` claim in `restoreSession` on
  `defaultSessionScope === 'single'`, mirroring `doSpawn`. Without the
  gate, a restored session silently became the omitted-scope attach
  target on `'thread'`-default daemons.
- Rename advertised capability `session_resume` to
  `unstable_session_resume` to match the underlying ACP method
  (`connection.unstable_resumeSession`). `session_load` stays stable.
- Seed `lastEventId: 0` in `DaemonSessionClient.resume`, symmetric with
  `load`. The agent's `unstable_resumeSession` schedules an
  `available_commands_update` via `setTimeout(0)`; without the seed the
  SDK consumer would miss that frame.
- Add HTTP-level test for the `RestoreInProgressError → 409` envelope.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* docs(serve): adopt review feedback comments on session load/resume

- Cross-reference the `POST /session` disconnect-cleanup rationale
  from `restoreSessionHandler`'s `!res.writable` branch so future
  maintainers find the BQ9tV race + tanzhenxin attach-rollback
  context without grep.
- Document `DaemonSessionState.{models, modes, configOptions}` in
  the SDK so callers can narrow to the ACP `SessionModelState` /
  `SessionModeState` / `SessionConfigOption` shapes.
- Add JSDoc on `DaemonClient.restoreSession` explaining why
  `loadSession` and `resumeSession` collapse into one transport.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): preserve restore state and harden in-flight restore races

Address the four Critical findings from PR #4222 review (wenshao):

- Coalesced restore waiters now observe the same ACP state the
  original restore caller did. `state: {}` in `restoreSession`'s
  coalesce branch was clobbering the spread `restored.state`, so
  concurrent callers got different payloads based purely on timing.
  Cache the load/resume response on `SessionEntry.restoreState` and
  return it from both the existing-byId early return and the
  coalesce branch.
- Drop the `defaultEntry` promotion on restore. Explicit
  `session/load` / `session/resume` is "give me THIS id"; it must
  not become the implicit attach target for subsequent omitted-id
  `POST /session` callers under `single` scope. Reserves
  `defaultEntry` for sessions created through `doSpawn` only.
- Reserve coalesced attaches synchronously via
  `InFlightRestore.coalesceState.count` so the spawn owner's
  `requireZeroAttaches` disconnect-reaper sees a non-zero
  `attachCount` on the freshly registered entry and skips the
  kill. Without this, B's `attachCount++` happened after `await
  inFlight.promise`, leaving a window where A's HTTP-disconnect
  cleanup could reap the session out from under B.
- Include `pendingRestoreIds` in the `killSession` channel-teardown
  decision. The last live session leaving while a restore is
  in-flight on the same channel would otherwise SIGTERM the
  channel mid-restore.
- Bump `RestoreInProgressError`'s `Retry-After` from 1s to 5s
  (matches `SessionLimitExceededError`); under the default
  `initTimeoutMs` of 10s, 1s pushed clients into tight loops.

Tests: new bridge cases covering state propagation through
coalesce, the spawn-owner-disconnect race, the
pendingRestoreIds-aware channel teardown, and the no-promote-
on-restore invariant. Existing "attaches twice" test rewritten
to assert the cached restore state propagates.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* test(serve): cover acpAgent load/resume + restore route error mappings

Close the test-coverage gaps wenshao called out in PR #4222 review:

- acpAgent.test.ts gains a `QwenAgent loadSession /
  unstable_resumeSession` block that locks down the new contract
  end-to-end at the agent layer:
  * `loadSession` missing persisted session → throws
    `RequestError.resourceNotFound("session:<id>")` (code -32002
    + `data.uri`).
  * `loadSession` existing session → returns LoadSessionResponse
    AND triggers `session.replayHistory(messages)` so SSE
    subscribers see the persisted turns.
  * `unstable_resumeSession` missing session → same
    resourceNotFound contract.
  * `unstable_resumeSession` existing session → returns the
    response WITHOUT replaying history (resume restores model
    context internally; UI replay is intentionally suppressed).
  Required extending the mocked `RequestError` with
  `resourceNotFound`, and mocking `SessionService` per case.
- server.test.ts adds the missing restore-route wire mappings:
  `WorkspaceMismatchError → 400 workspace_mismatch` and
  `SessionLimitExceededError → 503 + Retry-After: 5`. Combined with
  the existing 409 case for `RestoreInProgressError`, the route
  layer now has full structured-error coverage.
- Updated the 409 test's `Retry-After` expectation from `1` to `5`
  to match the bumped retry hint.

Disconnect-cleanup tests for the restore route were intentionally
not added — the cleanup branch is line-for-line identical to
`POST /session`'s handler (which itself ships without route-level
disconnect tests due to flaky supertest + Node http close-event
timing).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* docs(serve): document daemon session load/resume routes

Sync the docs to the routes that landed via PR #4222:

- `docs/developers/qwen-serve-protocol.md`:
  * Add `session_load` and `unstable_session_resume` to the
    advertised features list, with a note on the `unstable_`
    prefix mirroring ACP's underlying method name.
  * Document `POST /session/:id/load` and `POST /session/:id/resume`
    — request body, response shape (including the cached `state`
    field that late attachers observe), and the full error
    envelope: 404 unknown id, 400 workspace_mismatch, 503
    session_limit_exceeded (counts in-flight restores), 409
    restore_in_progress (cross-action race).
  * Note the SSE replay ring bound (4000 frames default) and the
    "subscribe immediately after load" guidance for long histories.
- `docs/users/qwen-serve.md`:
  * Add a "Loading and resuming a persisted session" section with
    the SDK example (`DaemonSessionClient.load` /
    `DaemonSessionClient.resume`) and the load-vs-resume
    decision table.
  * Update the durability model — sessions are still ephemeral
    across daemon restarts in Stage 1, but persisted sessions on
    disk can now be reloaded.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(test): use _meta payload to satisfy ACP SessionConfigOption types

The two new state-propagation tests in `httpAcpBridge.test.ts` used
`{ id, name, value }` as a `SessionConfigOption`, but ACP's actual
`SessionConfigSelect` shape requires `currentValue` + `options`. vitest
runs through esbuild and skips strict typechecking, so the local
`vitest run` passed; CI's `tsc --build` (run during `npm run prepare`)
caught it.

Switch the fixture to `_meta: { tag: '...' }` instead — `_meta` is
typed as `Record<string, unknown> | null` on the ACP response shapes,
so any payload survives. The assertions only need the bridge to
forward the state object intact, which `_meta` proves equally well
without committing the test to the full SessionConfigOption union.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): symmetric restore coalesce guard + transportClosed leak + defensive cleanup

Address the two new Critical findings + the test/cosmetic gaps from
wenshao's second review pass on PR #4222 (`a3f38da3a`):

- **[Critical] Symmetric coalesce guard.** The previous guard only
  rejected `load`-on-`resume`; `resume` arriving while a `load` was
  in flight silently coalesced and inherited the load's history-
  replay frames over SSE — directly violating resume's "no UI
  replay" contract (made worse by `DaemonSessionClient.resume()`
  seeding `lastEventId: 0`). Tighten the guard to
  `action !== inFlight.action` so any cross-action race throws
  `RestoreInProgressError`. Same-action coalescing is unaffected.

- **[Critical] `transportClosed` dangling rejection.** When
  `withTimeout` wins the `Promise.race` against `channel.exited`,
  the `.then(throw)` chain on `channel.exited` stays pending. A
  later channel exit (next session boundary, daemon shutdown, agent
  crash) fires the `throw` with no observer attached — Node 22 logs
  `unhandledRejection`, and `--unhandled-rejections=throw`
  deployments crash the daemon. Add `transportClosed.catch(() => {})`
  to suppress the dangling rejection after the race settles.

- **`isAcpSessionResourceNotFound` exact-match fallback.** The
  message-fallback path used `message.includes(expectedUri)`, which
  would falsely match a sessionId of `"a"` against a message
  containing `"session:abc"`. Tighten to exact equality on the
  canonical `Resource not found: <uri>` form. The primary
  `data.uri` path remains the dominant code path.

- **`loadSession` mcpServers default symmetry.** `loadSession` now
  uses `params.mcpServers ?? []` to mirror `unstable_resumeSession`.
  Defends against a future ACP schema loosening that makes
  `LoadSessionRequest.mcpServers` optional — without the
  null-coalesce, `newSessionConfig` would `TypeError` on iteration.

Tests added:
- `httpAcpBridge.test.ts`: `resume`-on-`load` rejection (mirror of
  the existing `load`-on-`resume` test); regression for the
  dangling `unhandledRejection` (resolves `channel.exited` after
  the restore promise has already settled and asserts no
  `unhandledRejection` event); shutdown-awaits-restore via
  `Promise.race`-based ordering.
- `server.test.ts`: 400 for non-string and over-length `cwd` on
  the restore routes (mirroring the equivalent `POST /session`
  cases for `parseOptionalWorkspaceCwd`).
- `acpAgent.test.ts`: load with `getResumedSessionData()` returning
  `undefined` — distinct code path that does NOT call
  `replayHistory`.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

---------

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-17 12:58:47 +08:00
ChiGao
80f1e266ba
feat(protocol): add typed daemon event schema v1 (#4217)
Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
2026-05-17 12:31:16 +08:00
ChiGao
57de269f45
feat(sdk): add DaemonSessionClient skeleton (#4201)
* feat(sdk): add daemon session client

* fix(sdk): harden daemon session event replay

* fix(sdk): replay attach-time daemon events

---------

Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
2026-05-17 01:01:12 +08:00
jinye
878f35fc4f
feat(serve): per-request sessionScope override on POST /session (#4175 Wave 2 PR 5) (#4209)
* feat(serve): per-request sessionScope override on POST /session

Resolves the FIXME at httpAcpBridge.ts:BridgeOptions.sessionScope from
#3803 — clients can now override the daemon-wide sessionScope per
request instead of being stuck with whatever boot-time value the
operator picked. A VSCode window that wants strict isolation can ask
for `'thread'` against a default-`'single'` daemon, and vice versa.

Wire change:
- POST /session body accepts optional `sessionScope: 'single' | 'thread'`
- Per-request value wins; daemon-wide default remains the fallback when
  the field is omitted (bit-for-bit backward compat for every existing
  caller)
- Invalid values yield 400 `{ code: 'invalid_session_scope' }`
- New capability tag `session_scope_override` advertised on
  /capabilities.features for negotiation

Bridge changes:
- BridgeSpawnRequest gains optional `sessionScope`
- spawnOrAttach validates the per-request value and resolves
  effectiveScope = req.sessionScope ?? defaultSessionScope
- doSpawn now takes effectiveScope and only stamps `defaultEntry`
  (the single-scope attach slot) when the spawn is single-scope —
  fixes a mixed-scope leak where a thread-first call would let a
  later omitted-scope call attach to the supposedly-isolated session

SDK:
- CreateSessionRequest gains optional `sessionScope`
- DaemonClient.createOrAttachSession conditionally spreads it into the
  JSON body so omitted callers send the same wire shape as before

Tests:
- 4 new bridge tests (override single→thread, override thread→single,
  mixed-scope leak regression, invalid-value rejection)
- 3 new server tests (valid passthrough, invalid 400, omitted backward
  compat)
- 2 new SDK tests (forwards/omits sessionScope on the wire)
- EXPECTED_STAGE1_FEATURES updated for the new capability tag

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(serve): address Wave 2 PR 5 review findings

Three independent review passes found three real issues:

1. Bridge `TypeError` on invalid `sessionScope` collapsed to opaque 500
   in `sendBridgeError` instead of the typed `400 invalid_session_scope`
   the route layer guarantees. Direct embed / test / future entry-point
   callers bypassing the route would see a generic 500 with stack noise
   on stderr — disagreeing with the route contract.

   Fix: add `InvalidSessionScopeError` class (alongside
   `SessionNotFoundError` / `WorkspaceMismatchError` /
   `SessionLimitExceededError`); the `spawnOrAttach` validator now
   throws it, and `sendBridgeError` translates to the same
   `{ error, code: 'invalid_session_scope' }` shape.

2. SDK `DaemonClient.createOrAttachSession` used a truthy check
   (`req.sessionScope ? ...`) for the conditional spread, silently
   erasing falsy-but-defined values (`''`, `null`, `0`) on the wire.
   A buggy caller would never see the daemon's 400 — it'd inherit the
   daemon-wide default while believing it requested a specific scope.
   Fix: use `!== undefined` (matching the bridge's own validation
   shape). Same fix to the server-side spread for consistency.

3. JSDoc and docs referenced `serve --sessionScope` as if it were a
   shipping CLI flag. It isn't — `ServeOptions` has no field, neither
   `runQwenServe` nor `serve.ts` plumbs one, and the production daemon
   default is hardcoded to `'single'`. Strike the references; note
   that #4175 may add the flag in a follow-up.

Test coverage expanded:
- Cap-bypass guard: per-request `'thread'` overrides cannot bypass
  `maxSessions` on a daemon-default-`'single'` deployment. Without
  this, a future refactor that gated the cap on `defaultSessionScope`
  instead of `effectiveScope` would silently let `'thread'` overrides
  amplify past the limit — the exact N-amplification cliff #3803 was
  about.
- Symmetric mixed-scope leak: daemon-default-`'thread'` +
  single-first-call followed by omitted-scope-second-call must produce
  distinct sessions. Mirrors the existing daemon-default-`'single'` +
  thread-first leak regression.
- Concurrent mixed-scope coalescing: simultaneous single + thread
  `spawnOrAttach` against the same workspace under slow `initialize`
  must not collide on `inFlightSpawns` (tracker keys differ by scope).
- Updated invalid-scope rejection test to assert
  `InvalidSessionScopeError` instance + carried `sessionScope` field.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)
2026-05-16 23:54:20 +08:00
jinye
264ed82273
[codex] feat(serve): add capability registry protocol versions (#4191)
* feat(serve): add capability registry protocol versions

Introduce a serve capability registry and advertise protocolVersions from /capabilities while preserving the existing v1 envelope and Stage 1 feature aliases. Update SDK wire types, docs, and focused tests for old-daemon compatibility.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(serve): clarify capability advertisement semantics

Address PR review feedback by preserving historical capability versions, separating registered and advertised feature helpers, testing protocol version metadata directly, and keeping runtime exports out of the serve types module.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

---------

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-16 18:07:38 +08:00
jinye
dcf7681d65
feat(core,cli): add generic atomicWriteFile, wire into Write/Edit tools, upgrade @types/node (#4096)
* feat(core): add generic atomicWriteFile and wire into Write/Edit tools

The Write and Edit tools used bare fs.writeFile, risking half-written
corrupt files on crash or power loss. Both tools' source code contained
explicit TODOs noting atomic write as the fix.

- Add atomicWriteFile() supporting string/Buffer with flush (fsync),
  permission preservation, symlink resolution, and EXDEV fallback
- Wire StandardFileSystemService.writeTextFile() through atomicWriteFile
- Refactor atomicWriteJSON to delegate to atomicWriteFile (adds fsync)
- Deduplicate renameWithRetry from runtimeStatus.ts
- Add flush:true to writeWithBackupSync for settings writes
- Upgrade @types/node to ^22.0.0 (flush option type support)

Closes the TODO in write-file.ts:371-385 and edit.ts:487-497.
Ref: #4095 (Phase 1)

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(core): address review comments on atomicWriteFile

- Fix permission window: separate existingMode from desiredMode so
  mode is set during writeFile (not just chmod after), eliminating
  the brief window where tmp file has overly permissive defaults
- Fix broken symlink handling: use lstat+readlink instead of realpath
  to correctly resolve symlinks whose targets don't exist yet,
  preventing the symlink from being replaced by rename
- Add test for writing through a broken symlink

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(core): address wenshao review on atomicWriteFile

- Fix Windows bug: use path.isAbsolute() instead of startsWith('/')
- Hoist path import to top-level static import
- Resolve full symlink chains via loop (handles A→B→C), with
  ELOOP guard at 40 hops matching POSIX SYMLOOP_MAX
- Mask stat.mode with 0o7777 to strip file-type bits
- Document EXDEV fallback atomicity loss in JSDoc
- Add tests for relative symlinks and multi-level symlink chains

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(test): fix CI failures from atomic write changes

- edit.test.ts: mock writeTextFile instead of chmod 444 for write error
  test — atomic write creates tmp file in same dir, so readonly target
  no longer triggers a write error
- atomicFileWrite.test.ts: skip permission tests on Windows — chmod is
  a no-op and stat.mode always returns 0o666

* fix(core): address deepseek review on atomicWriteFile

- Add try/catch around chmod calls to handle FAT/exFAT filesystems
  where POSIX permissions are not supported
- Add explicit type annotation to lstats variable

* fix: restore version numbers to 0.15.11 after rebase

* fix(core): resolve relative symlinks through directory symlinks

resolveSymlinkChain used path.dirname() to resolve relative symlink
targets, which is purely string-based. When intermediate path
components are themselves directory symlinks, the result would be
wrong (e.g. /a/link/file → ../target resolves to /a/target instead
of the kernel-resolved /b/target).

Use fs.realpath() on the parent directory to get the kernel-resolved
base for relative-target resolution.

* fix(test): normalize path separators in directory symlink test

Windows readlink returns native separators (backslashes), causing
the directory-symlink test to fail on Windows CI. Wrap both sides
of the symlink-target comparison with path.normalize.

* refactor(core): dedupe write/chmod logic in atomicWriteFile

- Extract writeOptions construction and tryChmod helper, removing
  duplication between the main write path and the EXDEV fallback
- Document atomicWriteJSON's symlink-preservation behavior

Addresses deepseek review on PR #4096.
2026-05-15 17:52:50 +08:00
tanzhenxin
f6315b378d
refactor(cli): revert dynamic slash command LLM translation (#4145)
* refactor(cli): revert dynamic slash command LLM translation (#4137)

Removes the runtime LLM-translation path for dynamic slash command
descriptions added in #3871, along with its `general.dynamicCommandTranslation`
setting and the `/language translate` subcommand tree.

Keeps the built-in locale coverage from the same PR untouched. Localization
of dynamic command descriptions should be solved at the source (manifest
fields, not runtime model calls); see #4137 for the proposed alternative.

* refactor(cli): drop translate prompts from mustTranslateKeys

Follow-up to the dynamic command translation revert: the 7 prompt keys
were stripped from every locale file in the previous commit, but the
allow-list in mustTranslateKeys still demanded them.

* refactor(cli): drop dead CommandService.fromCommands and vacuous tests

Follow-up cleanup after the dynamic command translation revert.

CommandService.fromCommands was introduced by #3871 solely to wrap the
LLM-translated command list. With the LLM-translation path gone, it has
no remaining non-test callers — remove it and the matching test mock.

Also drop two assertions in languageCommand.test.ts that checked for the
absence of a top-level /language cache command. They tested a migration
state that never existed in this branch and now pass vacuously.

* docs: drop /language translate references after revert

Two user-facing docs documented the /language translate subcommands
(status/on/off/cache refresh/clear) that were removed in the dynamic
command translation revert. Strip them so users following the docs
don't hit "Invalid command" errors.

* refactor(cli): drop unused localizeDescription field

The DynamicCommandLocalizationService that read this flag was removed in
the revert, leaving the field with five setters and zero readers. Drop the
field, its JSDoc, and the five `localizeDescription: true` assignments.
Also tidy the now-misleading `modelDescription` JSDoc and the stale
`reloadCommands` comment that referenced the removed feature.

* refactor(cli): drop unused getLanguageNameForTranslationTarget

The only caller was the removed DynamicCommandLocalizationService.
Remove the function from `i18n/languages.ts` and the matching
import + re-export from `i18n/index.ts`.
2026-05-15 16:01:16 +08:00
Shaojin Wen
790f2d0485
refactor(serve): 1 daemon = 1 workspace (#3803 §02) (#4113)
* refactor(serve): 1 daemon = 1 workspace (#3803 §02)

Stage 1 shipped with M-workspaces-per-daemon routing (`byWorkspaceChannel`
Map keyed by request `cwd`). The §02 architectural revision in
`docs/comparison/qwen-code-daemon-design/02-architectural-decisions.md`
narrows the bridge to 1 daemon = 1 workspace × N sessions: each daemon
binds to one canonical workspace path at boot; `POST /session` with a
mismatched `cwd` returns 400 `workspace_mismatch`. Multi-workspace
deployments run multiple daemon processes (one per workspace, supervised
externally — systemd / docker-compose / k8s / `qwen-coordinator`).

Bridge state collapses from maps to single optional slots:

- `byWorkspaceChannel: Map<string, ChannelInfo>` → `channelInfo?: ChannelInfo`
- `inFlightChannelSpawns: Map<string, Promise>` → `inFlightChannelSpawn?: Promise`
- `byWorkspace: Map<string, SessionEntry>` → `defaultEntry?: SessionEntry`
- `liveChannels: Set<ChannelInfo>` → not needed; `channelInfo` is the live
  reference, cleared only by `channel.exited` (preserves the tanzhenxin
  BkUyD invariant that `killAllSync` finds a target mid-SIGTERM-grace)

`BridgeOptions.boundWorkspace` becomes required. `WorkspaceMismatchError`
is thrown from `spawnOrAttach` when the request's canonical cwd doesn't
match the bound path, translated to 400 `workspace_mismatch` (with both
paths in the body) by the route layer. `CapabilitiesEnvelope.workspaceCwd`
surfaces the bound path so clients pre-flight check + omit `cwd` from
`POST /session` (it falls back to the bound workspace).

A new `--workspace <path>` CLI flag lets operators override
`process.cwd()` at boot. The previous `--http-bridge` / `--multi-workspace`
opt-in was never shipped; nothing changes for default users running
`qwen serve` in their project directory.

Removed code path: ~150 LOC of multi-workspace map machinery in
`httpAcpBridge.ts` plus the test cases that exercised it.

Test surgery:

- New `makeBridge()` helper in `httpAcpBridge.test.ts` injects
  `boundWorkspace: WS_A` by default; tests that need a different bind
  (the mismatch test) pass it explicitly.
- `does NOT reuse across workspaces` → `rejects cross-workspace requests
  with WorkspaceMismatchError` (the new semantics under §02).
- `shutdown kills every live channel` retargeted to single-channel
  multi-session shutdown.
- `killAllSync force-kills channels even after shutdown cleared
  byWorkspaceChannel (BkUyD)` retargeted to single-channel: the
  invariant is the same (channel reference must outlive eager shutdown
  clearing), the surface is just smaller.
- `listWorkspaceSessions` cross-workspace assertion now expects empty
  for the un-bound path.
- `--max-sessions` cap test uses two thread-scope sessions on `WS_A`
  instead of WS_A + WS_B.

Closes #3803 §02.

* fix(serve): address review findings on the §02 refactor

Two correctness fixes + four doc/test polish items surfaced by the
multi-agent review of #4113:

1. `killSession` → `spawnOrAttach` race (Critical). After killing
   the last session, `channel.kill()` runs through a 5s SIGTERM grace
   before SIGKILL. During that window a concurrent `spawnOrAttach`
   used to hit `ensureChannel`, find `channelInfo` still set, and
   reuse the dying transport — either landing the caller with a
   sessionId that 404s on every follow-up once `channel.exited`
   fires, or hanging until the newSession timeout.

   Fix: add an `isDying: boolean` flag on `ChannelInfo`, set
   synchronously by `killSession` / `doSpawn`-newSession-failure /
   `shutdown` BEFORE awaiting `channel.kill()`. `ensureChannel`
   treats a dying channel as absent and spawns a fresh one. The
   tanzhenxin BkUyD invariant ("`channelInfo` reference must outlive
   the kill-await for `killAllSync` mid-grace") is preserved — we
   set `isDying` but don't clear `channelInfo` until the OS reaps
   the child via `channel.exited`. A regression test in
   `httpAcpBridge.test.ts` pins the invariant: a never-resolving
   `kill()` keeps the SIGTERM grace open while a concurrent spawn
   verifies the factory was called twice (two distinct handles).

2. `boundWorkspace` canonicalization divergence (Critical).
   `server.ts` and `runQwenServe.ts` each computed
   `opts.workspace ?? process.cwd()` independently. The bridge
   canonicalized that string via `realpathSync.native` (resolving
   symlinks, case-folding on case-insensitive filesystems); the
   callers retained the raw form. On macOS HFS+ / APFS or any
   symlinked path, `/capabilities.workspaceCwd` advertised one
   spelling while the bridge enforced against another — clients
   echoing the advertised path back saw `POST /session` succeed but
   the response carry a different `workspaceCwd`.

   Fix: export `canonicalizeWorkspace` from `httpAcpBridge.ts` and
   call it once in `runQwenServe` (after the existence check) and
   once in `createServeApp`. Both paths land on the same canonical
   form; the bridge's own re-canonicalize is now a no-op
   (idempotent).

3. Reject `--workspace` pointing at non-existent directories at
   boot (Suggestion). `canonicalizeWorkspace`'s ENOENT fallback to
   `path.resolve` previously let the daemon boot pointed at a path
   that didn't exist; every `POST /session` then spawned a
   `qwen --acp` child with that cwd and the agent failed with an
   opaque ENOENT. Now `runQwenServe` `statSync`s the bound path at
   boot and rejects "directory does not exist" / "not a directory"
   with a clear message.

4. Stale docstrings (Nice to have). `types.ts` `ServeMode` JSDoc
   said "one `qwen --acp` child PER WORKSPACE" — directly
   contradicted the new `workspace` field's doc in the same file.
   `commands/serve.ts` `--http-bridge` description said "per
   workspace" — directly contradicted the `--workspace` flag's help
   in the same yargs builder. Both updated to "per daemon (the
   daemon binds to ONE workspace at boot)".

5. Stale `byWorkspace` comment references (Nice to have).
   `server.ts:188` ("orphaned in byId / byWorkspace") and
   `httpAcpBridge.test.ts:1210` ("still in byId/byWorkspace at the
   moment of crash") referenced the removed Map. Updated to
   `defaultEntry`.

6. `/capabilities` curl example in the Authentication section of
   `docs/users/qwen-serve.md` was missing the new `workspaceCwd`
   field — the Quickstart's curl example was updated but the
   parallel one in the auth section was not. Synced.

Tests added:
- `killSession marks the channel dying so concurrent spawnOrAttach
   gets a fresh channel` — pins fix (1).
- `--workspace flows end-to-end and surfaces on /capabilities` —
   exercises the runQwenServe → server.ts → bridge plumbing that
   no prior test covered.
- `rejects --workspace pointing at a non-existent directory` and
   `rejects --workspace pointing at a regular file` — pin fix (3).
- `rejects relative --workspace at boot` — covers the absoluteness
   check that exists but was untested.

Net: +238 / -24 across 8 files. All 149 serve tests pass.

* fix(serve): BkUyD overwrite race + Windows-fragile test + doSpawn-failure coverage

Round-2 review of #4113 caught three follow-up issues introduced by
or left open after round-1's fixes:

1. **BkUyD invariant overwrite race (Critical).** Round-1's `isDying`
   flag lets `ensureChannel` skip a dying channel and spawn a fresh
   one. When the fresh spawn completes, `channelInfo = info` overwrote
   the dying channel's reference — leaving NO global pointer to it.
   `killAllSync()` then iterated only `channelInfo` (the fresh one)
   and missed the dying child entirely. A double-Ctrl+C arriving
   mid-SIGTERM-grace would call `process.exit(1)` before the dying
   child's per-channel SIGKILL escalation timer fired, orphaning the
   child.

   Restore a `aliveChannels: Set<ChannelInfo>` (parallel to the
   original Stage 1 design, but justified by single-workspace too).
   Entries added in `ensureChannel`, removed by each channel's
   `channel.exited` handler. `killAllSync` iterates the SET, not the
   single attach-target slot. `shutdown` does the same — snapshots
   every alive channel and kills each, not just the current
   `channelInfo`.

   New regression test pins the invariant: spawn → killSession
   (channel marked dying, kill hangs) → spawnOrAttach (fresh channel
   overwrites `channelInfo`) → `killAllSync` — expect BOTH channels'
   `killSync` to fire. Pre-fix only the fresh one would have fired.

2. **Windows-fragile test path.** The new
   `rejects --workspace pointing at a regular file` test used
   `new URL(import.meta.url).pathname` to get a path to the test
   file. On Windows that returns `/C:/path/...` (leading slash);
   `fs.statSync` then resolves it as path-from-current-drive-root,
   fails with ENOENT, and the test sees the "does not exist" error
   message instead of the expected "not a directory" branch. CI runs
   `windows-latest`. Fix: `fileURLToPath(import.meta.url)` from
   `node:url`.

3. **doSpawn newSession-failure isDying path was untested.** The
   round-1 fix added `ci.isDying = true` to both `killSession` AND
   `doSpawn`'s newSession-failure catch, but only the killSession
   path had a regression test. Added a parallel one for the doSpawn
   path: thread-scope bridge with a `newSessionImpl` that throws on
   the first call → captures the rejection without awaiting it (the
   bridge's `await ci.channel.kill()` hangs in the test), yields
   enough cycles for the `isDying = true` sync prefix to settle, then
   confirms (a) the next `spawnOrAttach` produces a fresh channel
   and (b) `killAllSync` finds both channels in `aliveChannels`.

Also added a `newSessionImpl` option to the test FakeAgent — the
existing `initializeThrows` hook covered handshake-time failures, but
post-init `newSession` rejections (auth, bad config, mid-init
crashes) had no test affordance.

All 151 serve tests pass.

* docs(serve): update daemon-client-quickstart for §02 single-workspace

Round-3 review caught that the SDK example doc was the only one of the
three serve-related docs that the §02 refactor didn't touch. Updated:

- Boot log example now shows the `, workspace=/path/to/your-project`
  suffix that `runQwenServe` emits after the §02 changes.
- The "Hello daemon" example now reads `caps.workspaceCwd` off
  `/capabilities` and passes it back as `workspaceCwd` on session
  creation — illustrating the documented pre-flight pattern, not a
  hand-written literal that may not match the daemon's actual bind.
- Shared-session example makes the prerequisite explicit: the daemon
  must be bound to `/work/repo` (via `--workspace` or `cd`); under §02
  two clients can only share a session if they're both hitting a
  daemon already bound to that workspace.
- New "Workspace mismatch" section shows how to handle the
  `400 workspace_mismatch` error class: catching `DaemonHttpError`,
  branching on `body.code`, surfacing `boundWorkspace` /
  `requestedWorkspace` for the operator. This is a new error
  class SDK consumers' error handlers should branch on.

No code changes; docs only.

* feat(sdk,test): align SDK types + integration tests with §02 single-workspace

Round-4 review caught one type-drift gap + a set of integration-test
assumptions that the §02 refactor invalidated.

**SDK type drift.** `DaemonCapabilities` in
`packages/sdk-typescript/src/daemon/types.ts` was the SDK-side mirror
of `CapabilitiesEnvelope` on the daemon side. The §02 PR added
`workspaceCwd: string` to the daemon envelope (and the round-3 doc
example reads `caps.workspaceCwd` off the SDK client) but the SDK
type wasn't updated. A TypeScript consumer copying the doc snippet
verbatim would hit `TS2339 'workspaceCwd' does not exist on type
'DaemonCapabilities'`. The wire field is present so JS consumers
wouldn't notice — but the SDK is marketed as a TypeScript quickstart,
so this is a real onboarding break.

Fix: add `workspaceCwd: string` to `DaemonCapabilities` (parallel to
`DaemonSession.workspaceCwd` which is already there). The SDK unit
test for `client.capabilities()` was updated to put the new field
in the mocked response.

**Integration tests.** `qwen-serve-routes.test.ts` spawns a real
`qwen serve` daemon in `beforeAll`. Three breakages exposed:

1. The daemon was launched without `--workspace`, so it inherited
   the test runner's `cwd`. Tests then POST `workspaceCwd: REPO_ROOT`
   assuming the daemon is bound to the repo root — true when run via
   `npm test` from the repo, brittle from IDEs / launchers that have
   a different `cwd`. Added `'--workspace', REPO_ROOT` to the spawn
   args so the bound workspace is deterministic regardless of where
   the test runner is launched.

2. The `bad modelServiceId` test used `cwd: '/tmp'`. Under §02 this
   would now return 400 workspace_mismatch before the session was
   spawned. Switched to `REPO_ROOT` and softened the `attached`
   assertion (REPO_ROOT may already have a session from earlier
   tests in the suite under sessionScope:single).

3. Added three new integration tests pinning the §02 surface
   end-to-end through a real daemon process:
   - `rejects cross-workspace cwd with 400 workspace_mismatch` —
     posts `/tmp` and asserts the full structured error body
     (`code`, `boundWorkspace`, `requestedWorkspace`).
   - `omits cwd → falls back to bound workspace` — posts an empty
     body and asserts the response's `workspaceCwd` matches REPO_ROOT
     (verifies the runQwenServe → createServeApp → bridge fallback
     plumbing).
   - `GET /capabilities surfaces workspaceCwd` — asserts the new
     SDK type field is populated correctly off the wire.

All 422 unit tests pass (cli serve + sdk). Integration tests
typecheck clean.

* fix(serve): address /review feedback from gpt-5.5 + deepseek-v4-pro

Process the 7 inline /review comments on PR #4113:

- C1+C3 (SDK): make `DaemonCapabilities.workspaceCwd` and
  `CreateSessionRequest.workspaceCwd` optional in the SDK types.
  `workspaceCwd` is an additive field on the v=1 envelope per #3803
  §02; the protocol's "bump v only on incompatible changes" stance
  is honored by leaving the field optional at the type level.
  `DaemonClient.createOrAttachSession` now omits `cwd` from the body
  when `workspaceCwd` isn't passed, matching the PR description's
  "SDK accepts bound path or none". Adds a unit test pinning the
  empty-body shape.

- C2 (docs/users/qwen-serve.md): the `--http-bridge` row described
  the pre-§02 per-session model; updated to reflect one child per
  daemon with N sessions multiplexed via ACP `newSession()`.

- C4 (server.ts): `WorkspaceMismatchError` was silently 400'ing
  without a stderr breadcrumb, leaving operators blind to
  cross-workspace routing drift. Mirrors the SessionLimitExceeded
  /InvalidPermissionOption observability pattern.

- C5 (server.test.ts): the `/capabilities` fallback test compared
  `res.body.workspaceCwd` against raw `process.cwd()`; on macOS
  default tmpdir flows (`/var/folders/...` → `/private/var/...`)
  the canonicalize-once route value diverges. Use
  `realpathSync.native(process.cwd())` to match the route's
  canonicalization.

- C6 (server.ts): the cwd-not-absolute error said "cwd is required
  and must be an absolute path" but cwd is now optional under §02.
  Tightened wording to "must be an absolute path when provided".

- C7 (runQwenServe.ts): the `statSync` catch only wrapped ENOENT
  with a friendly diagnostic; EACCES / EPERM (typical for
  SIP-protected dirs on macOS or root-owned paths the daemon's UID
  can't traverse) re-threw as raw `SystemError`. Wrap both codes
  with a `--workspace`-context message so the boot failure points
  at the flag the operator set.

Docs: quickstart shows the explicit-pass-or-omit options side by
side; protocol reference notes `workspaceCwd` is additive to v=1.

* fix(serve/test): make /work/bound literals Windows-portable

Windows CI failed on this PR's two new tests because
 returns  (drive-relative
absolute), so the route's canonicalize step diverged from the hardcoded
literal. Mirror the WS_A/WS_B pattern already used in
httpAcpBridge.test.ts: define WS_BOUND / WS_DIFFERENT via
`path.resolve(path.sep, …)` and use the constants everywhere. The
400 workspace_mismatch test would still have passed (mock controls
both throw + assertion) but I aligned it for consistency.

Failures from CI run 25806528710:
  expected 'D:\work\bound' to be '/work/bound' (Object.is)

Affected tests:
  - createServeApp > GET /capabilities > reports the bound workspace
  - createServeApp > POST /session > 200 when cwd is omitted

* fix(serve): address second /review round (gpt-5.5 + deepseek-v4-pro)

Four new inline findings from the latest /review pass:

- N1 (integration-tests/cli/qwen-serve-routes.test.ts) — Critical:
  the `workspace_mismatch` assertion compared `requestedWorkspace`
  against the literal `'/tmp'`, but the bridge canonicalizes via
  `realpathSync.native` and on macOS `/tmp` is a symlink to
  `/private/tmp`. Compare against `realpathSync.native('/tmp')` so
  the assertion is portable.

- N2 (packages/cli/src/serve/types.ts):
  `CapabilitiesEnvelope.workspaceCwd: string` (server side) diverged
  from the SDK's `DaemonCapabilities.workspaceCwd?: string`. Made the
  server type optional too — matches the SDK, matches the protocol
  doc's "additive to v=1" framing, doesn't change runtime emission
  (the post-§02 server still always populates the field).

- N3 + N4 (packages/cli/src/serve/server.ts + sdk-typescript/.../DaemonClient.ts):
  the route's `cwd` validation treated every non-string body value
  (`null`, `123`, `{}`, `[]`) the same as omitted, silently falling
  back to `boundWorkspace`. That hid client/orchestrator
  serialization bugs as "session attached to wrong workspace".
  Now the route uses `'cwd' in body` to detect presence and rejects
  presence-but-not-a-string with `400 'cwd must be a string absolute
  path when provided'`. Empty string still hits the existing
  `path.isAbsolute` branch ("must be an absolute path when
  provided"), so an SDK caller passing `workspaceCwd: ''` no longer
  silently lands in the daemon's bound workspace.

  SDK side: reverted my conditional spread to `cwd: req.workspaceCwd`
  unconditional. `JSON.stringify` strips `undefined` automatically
  (so omitted `workspaceCwd` becomes "no `cwd` key" on the wire, as
  before), but empty-string is now forwarded verbatim and the server's
  400 surfaces the bug instead of the SDK swallowing it. Added a unit
  test pinning the empty-string-forwarded shape.

Server tests:
  - `400 when cwd is present but not a string` covers null / number /
    object / array via a sub-loop.
  - `400 when cwd is the empty string` pins the isAbsolute path.

  bridge: 73/73; server: 80/80 (was 78, +2 new); SDK: 40/40 (was 39,
  +1 empty-string test). tsc clean for SDK and PR-touched CLI files.

* fix(serve): use const cwd in POST /session (prefer-const lint)

CI lint failed with packages/cli/src/serve/server.ts:199:9 prefer-const: 'cwd' is never reassigned. The wave-4 rewrite split the original 'let cwd; if (!cwd) cwd = boundWorkspace' into a single ternary, which removes the only mutation path; the variable should be const accordingly.

* fix(serve): address third /review round (gpt-5.5 + glm-5.1 + deepseek-v4-pro)

Five new inline findings; M1 was already resolved in 1c7f5f069.

- M2 (httpAcpBridge.ts): drop the dead `ChannelInfo.workspaceCwd`
  field. Pre-§02 it was the routing key for `byWorkspaceChannel.get`;
  after the §02 collapse all reads target `SessionEntry.workspaceCwd`
  and `ChannelInfo.workspaceCwd` was only written, never read. Per-
  channel storage also suggests variance the "1 daemon = 1 workspace"
  model forbids. Removing the field encodes the single-workspace
  invariant in the type itself; left a stub comment so future
  readers don't reintroduce it.

- M3 (httpAcpBridge.ts): fast-path `canonicalizeWorkspace` when
  `req.workspaceCwd === boundWorkspace`. The §02 recommended client
  flow is `caps.workspaceCwd` → POST `cwd: caps.workspaceCwd`, and
  the omit-cwd route in server.ts synthesizes the same equality.
  Both hit the equality check and skip the sync `realpathSync.native`
  syscall. Non-equal inputs fall through to the full canonicalize
  (clients sending `/work/./bound`, mixed casing on case-insensitive
  FS, symlink aliases) so correctness is unchanged.

- M4 (httpAcpBridge.ts): operator stderr breadcrumb in the
  `channel.exited` handler. An agent crash (OOM / segfault) used to
  be silent on the daemon side — the child-stderr forwarder caught
  whatever the child wrote before dying (often nothing on
  SIGKILL/segfault), and SSE subscribers saw `session_died` frames
  but operators reading `qwen serve`'s own output had no signal that
  the agent process was gone. Log code+signal+affected-session-count
  so the line is the canonical "agent disappeared" indicator.

- M5 (server.ts): documentation-only. The reviewer wanted
  `createServeApp` to validate `opts.workspace` exists + is a
  directory (currently only `runQwenServe` does). Trade-off: doing
  that breaks 4 existing tests which pass synthetic `/work/bound` on
  purpose to exercise route-layer behavior without a real directory.
  Deferred the helper extraction; added a JSDoc note pinning the
  contract so future entry points binding `createServeApp` to user
  input know to replicate the validation.

- M6 (runQwenServe.ts): pass the already-canonical `boundWorkspace`
  into `createServeApp` via `opts.workspace`. `canonicalizeWorkspace`
  is idempotent so the server-side recanonicalize is a no-op today,
  but if a future refactor ever makes it non-idempotent the values
  the route advertises on `/capabilities` and the bridge enforces
  would diverge — landing clients in a "/capabilities says X, POST
  /session/X returns workspace_mismatch" contradiction. Removes the
  drift risk.

bridge: 73/73; server: 80/80; tsc clean for PR-touched files.

* fix(serve,sdk): address fourth /review round (deepseek-v4-pro x2)

Two new inline findings:

- O1 (server.ts): the POST /session route uses `'cwd' in body` against
  `safeBody`'s `Object.create(null)` output to distinguish "client
  omitted cwd" from "client sent cwd". The semantics quietly couple
  to `safeBody`'s literal strip list (`__proto__/constructor/prototype`).
  If a future maintainer adds a user-facing key (e.g. `cwd`) to that
  strip list, the route's presence-check would silently flip to
  "absent → fallback", masking the bug as "wrong workspace bound."
  Extracted `PROTOTYPE_POLLUTION_KEYS: ReadonlySet<string>` as a named
  module-scope constant; safeBody uses `.has()` on it (behavior
  unchanged); the route's comment now cross-references the const so
  the coupling is documented at both ends. The const's JSDoc spells
  out what to do if the strip set ever has to grow into user-key
  territory.

- O2 (sdk-typescript): `DaemonCapabilities.workspaceCwd` is
  `string | undefined` (additive to v=1; pre-§02 daemons omit). SDK
  consumers that pass it into a `string` context get a TS strict
  error or, against an old daemon, a runtime
  `Cannot read properties of undefined`. Added a `requireWorkspaceCwd`
  helper + `DaemonCapabilityMissingError` so consumers can opt into
  an actionable
  `DaemonCapabilities.workspaceCwd is missing — introduced in #3803 §02 …`
  error instead. Exported both from `@qwen-code/sdk`'s top-level
  module + the `daemon/` sub-module. Unit tests cover populated,
  missing, and empty-string inputs.

bridge: 73/73; server: 80/80; SDK DaemonClient: 43/43 (was 40, +3
new requireWorkspaceCwd cases). tsc clean for SDK and PR-touched
CLI files.

* fix(serve): address tanzhenxin REQUEST_CHANGES (cold-spawn + streaming-test bind)

Two findings from the CHANGES_REQUESTED review on PR #4113.

- T1 (integration-tests/cli/qwen-serve-streaming.test.ts) — high
  severity: the daemon spawn in `beforeAll` did not pass
  `--workspace REPO_ROOT`, so under §02 the daemon bound to
  whatever cwd the test runner was invoked from. Every later
  `createOrAttachSession({ workspaceCwd: REPO_ROOT })` then 400'd
  with `workspace_mismatch`, and the entire file — child-crash
  recovery, multi-client first-responder permission, Last-Event-ID
  resume — silently no-op'd once `SKIP_LLM_TESTS` was unset. The
  sibling `qwen-serve-routes.test.ts` got the same fix earlier in
  this PR; this file was missed in that pass. Added the flag with a
  comment pointing at the rationale so the omission can't recur.

- T2 (packages/cli/src/serve/httpAcpBridge.ts) — medium severity:
  cold-spawn window orphans the agent child on double-Ctrl+C. The
  `qwen --acp` child exists from the moment `channelFactory` spawns
  it, but pre-fix the bridge only added the channel to
  `aliveChannels` AFTER `connection.initialize()` returned. During
  the up-to-`initTimeoutMs` (default 10s) handshake window
  `aliveChannels` was empty, and a double-Ctrl+C in that window
  played out as: first SIGINT entered `shutdown()` and awaited the
  in-flight spawn; second SIGINT called `killAllSync()` against an
  empty set; `process.exit(1)` orphaned the child. Same class of
  bug the BkUyD invariant set out to close — the post-init
  overwrite race was covered, the pre-init handshake window wasn't.

  Fix: move `info` creation + `aliveChannels.add(info)` + the
  `channel.exited` handler registration BEFORE the `initialize`
  await. Init-failure / late-shutdown / child-crash-during-handshake
  all converge on the same cleanup path: mark `isDying = true`,
  `await channel.kill()`, let the exited handler `aliveChannels
  .delete(info)` once the OS reaps the process. `channelInfo` (the
  attach target) is still assigned LAST so `ensureChannel`'s
  fast-path never returns a still-handshaking channel.

  Regression test: `killAllSync force-kills the channel during the
  initialize handshake` uses a bespoke factory whose agent's
  `initialize` never resolves and asserts `killAllSync` fires
  killSync against the channel during the handshake window. Pre-fix
  the test would observe an empty `killSyncCalls` array.

bridge: 74/74 (was 73, +1 cold-spawn test); server: 80/80;
tsc clean for PR-touched files.

* fix(serve): address third /review round (gpt-5.5 + glm-5.1 + deepseek-v4-pro)

Eight new inline findings; six applied, two deferred-with-reply.

- P1 (httpAcpBridge.ts init-failure isDying comment): my comment
  overstated what `info.isDying` accomplishes on the init-failure
  path — concurrent `ensureChannel()` callers don't bypass via
  `isDying`, they coalesce on `inFlightChannelSpawn` and observe the
  same rejection. Reworded to describe the actual cross-path
  invariant marker.

- P2 (server.ts workspace_mismatch log injection): doudouOUC flagged
  log injection via `err.requested` (user-controlled). `path.resolve`
  + `realpathSync.native` preserve control chars in path segments,
  so a body `{"cwd": "/legit/path\nqwen serve: FAKE LOG"}` would
  emit two valid-looking daemon log lines on stderr — weaponizing
  line-based log shippers (Splunk / Loki / journald → SIEM).
  `JSON.stringify` both `err.bound` and `err.requested` in the log
  line escapes control chars + quotes the values, making any
  injection attempt visible-as-quoted-noise rather than forged-line.
  Bound is operator-controlled and inherently safe but quoted
  symmetrically for readability. The defense-in-depth alternative
  (reject control chars in canonicalizeWorkspace) is deferred —
  this single log site was the actionable interpolation; future
  workspace-path-into-stderr / -JSON / -templated-SQL flows can pick
  up the rejection if they ship.

- P3 (httpAcpBridge.test.ts): refactor the cross-workspace
  WorkspaceMismatchError test to a single `.catch((e) => e)` capture
  rather than firing the rejection twice (once for the `rejects
  .toBeInstanceOf` matcher, once for the field assertions). Logic
  unchanged.

- P4 (httpAcpBridge.ts channel.exited log): the `qwen serve:
  channel exited (...)` line fired on every channel exit including
  planned shutdown — alarming for operators who Ctrl+C'd a healthy
  daemon. Guarded with `if (!shuttingDown)` so the planned-shutdown
  case (operator already saw `received SIGINT, draining...`) stays
  silent. The killSession path (last session leaves, daemon stays
  up — no top-level context line) still logs, since the line is the
  only signal that the cleanup actually ran.

- P5 (httpAcpBridge.ts): light trim of the "pre-fix" narrative
  voice in two comment blocks (cold-spawn ensureChannel layout +
  BkUyD killAllSync aliveChannels iteration). Kept the invariant
  explanations — those carry maintenance value — dropped the
  "pre-fix the code did X" framing that's review-context not
  future-reader context.

- P6 (server.ts + runQwenServe.ts): `createServeApp` now accepts a
  pre-canonicalized `deps.boundWorkspace` to skip its own
  `canonicalizeWorkspace` syscall when the caller (runQwenServe)
  already did the work. Replaces my earlier `{...opts, workspace:
  boundWorkspace}` opts-mutation hack — cleaner separation of
  concerns + drops one `realpathSync.native` per boot. Direct
  callers (tests, embeds) that omit `deps.boundWorkspace` still get
  the in-body canonicalize path.

- P8 (httpAcpBridge.ts): defensive `aliveChannels.size > 2`
  warning. The set is intentionally multi-entry to cover the
  killSession-then-spawnOrAttach overlap window (size 2 is
  legitimate). Anything higher implies a `channel.exited` handler
  never fired for a prior channel — a real leak we'd otherwise
  catch only as gradually-growing RSS. The warning surfaces it the
  moment it happens.

- P7 (CreateSessionRequest.workspaceCwd optional): deferred with
  reply rationale. Making the field optional is the §02 design
  ("SDK accepts bound path or none"); the JSDoc already explains
  the omit-vs-explicit choice; Stage 1 has no shipping SDK
  consumers so there's no breakage to call out in a changelog file.
  No code change.

bridge: 74/74 (cross-workspace test refactor + behavioral assertions
unchanged); server: 80/80; SDK 43/43. tsc clean for PR-touched
files.

* fix(serve): apply auto-fixes from /review (#4113)

- canonicalizeWorkspace: narrow catch to ENOENT only, propagate other filesystem errors
- listWorkspaceSessions: add fast-path string equality to avoid realpathSync on every poll
- GET /workspace/:id/sessions: return 400 workspace_mismatch for cross-workspace queries
- SessionNotFoundError: accept optional extra message; clarify agent-crash-on-spawn case
- requireWorkspaceCwd: distinguish empty-string (post-§02 bug) from absent (pre-§02 daemon)

* fix(serve/test): bind workspace explicitly in GET /workspace tests

Wave-5 commit 0c6e963cd ("apply auto-fixes from /review (#4113)") added
a 400 workspace_mismatch reject path to GET /workspace/:id/sessions
for cross-workspace queries, but the existing two happy-path tests
queried `/work/a` / `/work/idle` against an unbound daemon (which
falls back to `process.cwd()`). Both turned to 400 in CI.

Bind the daemon to WS_BOUND in both happy-path tests and query the
same path. Add a third regression test that pins the §02
cross-workspace rejection contract — `code: workspace_mismatch`,
both paths in the body, bridge.listCalls untouched (no silent
fallback regression).

Brings server.test.ts from 80 → 82 tests, all passing.

* fix(serve,sdk): address fourth /review round (deepseek-v4-pro x2)

Six new inline findings; five applied, one defer-with-reply.

- Q1 (httpAcpBridge.ts + server.ts + tests): cwd length amplification
  through WorkspaceMismatchError. The error constructor interpolates
  `requested` into `.message` TWICE; `sendBridgeError` echoes it on
  stderr (now JSON.stringify-wrapped); `res.json` echoes it again — a
  ~10 MB `cwd` body (right under express.json's 10 MB cap) would
  amplify to ~60 MB per request × maxConnections (default 256). On
  loopback-default-no-token deployments this is pre-auth. Added
  `MAX_WORKSPACE_PATH_LENGTH = 4096` (Linux PATH_MAX); route rejects
  oversized `cwd` with a 400 BEFORE the bridge is touched, and the
  `WorkspaceMismatchError` constructor truncates `requested` as
  defense-in-depth for non-route callers (tests, embeds, future
  entry points that throw the error directly). Three new tests pin
  the route 400, the constructor truncation, and the normal-path
  passthrough.

- Q2 + Q5 (httpAcpBridge.ts docs): the `channelInfo` declaration
  comment + `ChannelInfo.sessionIds` JSDoc + `ChannelInfo.isDying`
  JSDoc all overstated when `channelInfo` is cleared. Post-§02 the
  BkUyD invariant is "ONLY `channel.exited` clears `channelInfo`"
  — teardown initiators (killSession last-session-leaving,
  doSpawn-newSession-failure, ensureChannel init-failure/late-
  shutdown, shutdown) set `isDying = true` but LEAVE `channelInfo`
  pointing at the dying channel until OS reap, so `killAllSync`
  can still reach it through `aliveChannels`. A future maintainer
  reading the old phrasing might "fix" killSession to also clear
  `channelInfo` and silently break the double-Ctrl+C force-kill
  path. Rewrote all three sites to describe the actual invariant +
  enumerate the 5 isDying set-sites + spell out the BkUyD rationale
  in one place (the `isDying` JSDoc) that other comments point at.

- Q3 (runQwenServe.ts): the "listening on …" boot summary goes to
  stdout but every other operational diagnostic (bearer auth, the
  workspace_mismatch breadcrumb, channel-exited, bridge errors) goes
  to stderr. Operators capturing only stderr (systemd / docker / k8s
  default) miss the `workspace=` indicator, which is the single
  piece of information they need most when triaging §02 migration
  issues. Added a `qwen serve: bound to workspace "X"` stderr line
  alongside the stdout one — keeps stdout untouched (integration
  tests + scripts parse it) while making the breadcrumb visible to
  stderr-only log shippers. `JSON.stringify` the boundWorkspace
  value (operator-controlled but cheap defense-in-depth against any
  future flow that lands a control char in the path).

- Q4 (integration-tests/tsconfig.json): the `paths` entry resolved
  `@qwen-code/sdk` to the SDK's built `dist/` directory; `dist/` is
  gitignored and stale dist (no `npm run build` first) yields TS2339
  errors on the integration tests' imports of new SDK fields.
  Pointed `paths` at SDK source instead — `tsc -p
  integration-tests/tsconfig.json` no longer requires a prior
  rebuild. The vitest config's runtime alias still resolves to
  `dist/index.mjs` so the actual test execution exercises the
  published-bundle shape; this paths entry only affects type
  resolution.

- Q6 (httpAcpBridge.ts): `createHttpAcpBridge` constructor called
  `canonicalizeWorkspace(opts.boundWorkspace)` even when the caller
  (`runQwenServe`) had already canonicalized and threaded the same
  value through `deps.boundWorkspace` into `createServeApp`. Two
  independent `realpathSync.native` calls can theoretically diverge
  on NFS-transient / mid-rename filesystems, landing the bridge with
  a canonical form different from what `/capabilities` advertises
  and from `createServeApp`'s view. Dropped the bridge's
  re-canonicalize; kept `path.isAbsolute` (structural, not a
  syscall); documented the caller contract on `BridgeOptions
  .boundWorkspace` ("MUST be pre-canonicalized; tests/embeds call
  `canonicalizeWorkspace` first"). Tests use
  `path.resolve(path.sep, ...)` which is already canonical-or-
  fallback for non-existent paths, so no test changes needed.

bridge: 76/76 (was 74, +2 WorkspaceMismatchError truncation tests);
server: 82/82 (was 80, +2 length cap + the auto-applied helper).
tsc clean for SDK, CLI PR-touched files, and integration-tests'
qwen-serve-*.
2026-05-15 12:44:36 +08:00
Shaojin Wen
870bdf2a9d
feat(cli,sdk): qwen serve daemon (Stage 1) (#3889)
Some checks are pending
Qwen Code CI / Classify PR (push) Waiting to run
Qwen Code CI / Lint (push) Blocked by required conditions
Qwen Code CI / Test (macos-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (ubuntu-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Test (windows-latest, Node 22.x) (push) Blocked by required conditions
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Blocked by required conditions
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run
* feat(cli): scaffold `qwen serve` HTTP daemon (Stage 1, #3803)

Adds a `serve` subcommand that boots an Express 5 listener with bearer
auth, host allowlist, and CORS modeled on `vscode-ide-companion/src/
ide-server.ts`. Ships only `/health` and `/capabilities` to begin with;
session/prompt/event routes will land in follow-up PRs once the per-
session ACP child-process bridge in `httpAcpBridge.ts` is wired.

Defaults to 127.0.0.1 with auth disabled so local development needs no
configuration. Binding beyond loopback (e.g. `--hostname 0.0.0.0`)
refuses to start without a token (`--token` or `QWEN_SERVER_TOKEN`).

Capabilities envelope versioned at v=1 with a `features` array — clients
should gate UI off `features`, never off `mode`, so subsequent PRs can
add capability tags without breaking older clients.

Per design issue's Stage 1 scope (~700-1000 LOC). Adds ~430 LOC of
implementation + tests in this scaffold; the remaining budget belongs
to the route wiring + bridge implementation in follow-ups.

* feat(cli): wire HttpAcpBridge + POST /session for `qwen serve` (#3803)

Stage 1 follow-up to the scaffold. Implements the bridge between the
HTTP daemon and the existing ACP child agent, plus the first session
endpoint.

`HttpAcpBridge.spawnOrAttach`:
  - Spawns `node $cliEntry --acp` per workspace via an injectable
    `ChannelFactory` (default uses `process.argv[1]`; tests use an
    in-memory `TransformStream` pair so they don't fork real processes).
  - Drives the ACP `initialize` + `newSession` handshake via the SDK's
    `ClientSideConnection`, with a 10s timeout that kills the channel.
  - Under `sessionScope: 'single'` (default), reuses the live session
    when the same canonical workspace cwd is requested again — backs
    the `attached: true` flag.
  - The `Client` impl on the bridge side proxies file reads/writes to
    local fs (daemon and agent share the host) and buffers
    `sessionUpdate` notifications for the SSE wiring in the next PR.
    `requestPermission` returns `cancelled` until the
    `/permission/:requestId` route lands.

`POST /session`:
  - 400 on missing or relative `cwd`.
  - 200 with `{sessionId, workspaceCwd, attached}` on success.
  - 500 on bridge failure (the failing channel is killed, not leaked).

`runQwenServe` constructs the bridge and ties `bridge.shutdown()` into
the listener-close path so SIGINT/SIGTERM drain children before the
socket closes.

Tests (14 new, 0 regressions in the 4967-test baseline):
  - 9 bridge cases over an in-memory channel — fresh spawn, single-scope
    reuse, cross-workspace isolation, thread-scope independence, path
    canonicalization, relative-path rejection, init failure cleanup,
    init timeout, multi-channel shutdown.
  - 4 route cases for /session (missing/relative/200/500).
  - 1 lifecycle case asserting `runQwenServe.close()` calls
    `bridge.shutdown()` before closing the listener.

Verified end-to-end: `qwen serve` boots, `POST /session` spawns a real
`qwen --acp` child and returns the SDK-assigned `sessionId`, repeat
calls under the same cwd return `attached: true`, `SIGTERM` reaps the
child along with the listener.

* feat(cli): wire POST /session/:id/prompt + /cancel for `qwen serve` (#3803)

Stage 1 follow-up after the bridge scaffold. Adds the two routes a client
needs to actually run a turn against the daemon.

Bridge:
  - `sendPrompt(sessionId, req)` looks up the session, FIFO-queues the
    call against the per-session prompt queue, and forwards through the
    SDK `ClientSideConnection.prompt`. Concurrent calls observe ACP's
    "one active prompt per session" invariant — second waits for first.
  - A failed prompt does NOT poison the queue; the tail catches and
    keeps draining so the next caller still runs (the original caller
    still sees its own rejection).
  - `cancelSession(sessionId, req?)` bypasses the queue and forwards
    the ACP notification immediately. ACP semantics: the agent winds
    down the *currently active* prompt; queued work is unaffected.
  - Both methods throw `SessionNotFoundError` (a typed Error subclass)
    when the id is unknown so route handlers can map cleanly to 404
    without brittle message matching.
  - Both methods overwrite the `sessionId` field in the request body
    with the routing id — a stale or spoofed body would otherwise be
    dispatched to the wrong agent process.

Routes:
  - `POST /session/:id/prompt` → 200 with PromptResponse, 400 on
    missing/non-array prompt, 404 on unknown session, 500 on agent
    error.
  - `POST /session/:id/cancel` → 204 always (cancel is a notification),
    404 on unknown session.

Tests (14 new — 7 bridge + 7 route, 0 regressions in the 4981 baseline):
  - sendPrompt: success forwards & returns response · routing-id
    overrides body sessionId · concurrent prompts FIFO-serialize
    (verified via per-prompt start/end ordering with a release latch) ·
    failed prompt doesn't block subsequent prompts · 404 for unknown id.
  - cancelSession: forwards with routing id · 404 for unknown id.
  - Routes: 200/400/404/500 paths for prompt; 204 with body or empty +
    404 for cancel.

Verified end-to-end against a real `qwen --acp` child:
  - POST /session/:id/prompt with `[{type:'text',text:'hi'}]` → 200
    `{"stopReason":"end_turn"}` in ~3.4s.
  - POST /session/:id/cancel → 204.
  - POST /session/does-not-exist/prompt → 404 with the unknown id
    surfaced in the body.

* feat(cli): wire SSE streaming for `qwen serve` events (#3803)

Stage 1 follow-up that turns prompt into a real streaming experience.
Replaces the in-memory `notifications: SessionNotification[]` buffer
on each session with a per-session EventBus and exposes it through
`GET /session/:id/events` as an `text/event-stream` SSE feed.

EventBus (`packages/cli/src/serve/eventBus.ts`):
  - Monotonic per-session ids (`v: 1` schema). Each `publish` chains an
    id, returning the materialized BridgeEvent.
  - Bounded ring (default 1000) backs `Last-Event-ID` reconnect — a
    consumer that drops can resume from `lastEventId` and replay any
    still-buffered events before live events flow.
  - Per-subscriber bounded queue (default 256). When a slow consumer
    overruns its queue, the bus appends a synthetic `client_evicted`
    terminal frame and closes that subscription so it can't hold the
    daemon hostage. Other subscribers are unaffected.
  - `subscribe()` returns an AsyncIterable — registration is synchronous
    so events `publish`ed immediately after the subscribe land in the
    queue (a generator-style implementation deferred registration to
    first `next()` and raced with publishes).
  - AbortSignal-aware: aborting the signal closes the iterator promptly.

Bridge (`httpAcpBridge.ts`):
  - `BridgeClient.sessionUpdate` now publishes onto the session's
    EventBus instead of pushing to a plain array — every ACP
    notification the agent emits becomes a stream event automatically.
  - New `subscribeEvents(sessionId, opts?)` returns the bus's
    AsyncIterable; throws `SessionNotFoundError` for unknown ids.
  - Shutdown closes every live event bus before killing channels so
    pending consumers unwind cleanly.

Route (`server.ts`):
  - `GET /session/:id/events` sets the SSE content type, advertises a
    3s reconnect hint, and writes a 15s heartbeat comment frame to
    keep proxy/NAT connections alive.
  - Forwards the `Last-Event-ID` header to the bus.
  - `req.on('close')` triggers an AbortController that propagates into
    the bridge subscription so disconnects don't leak subscribers.
  - 404 when the bridge can't find the session.

Capabilities envelope: `STAGE1_FEATURES` now advertises
`session_create`, `session_prompt`, `session_cancel`, `session_events`
in addition to `health`/`capabilities` so clients can light up UI for
the routes that have actually shipped.

Tests (16 new, 0 regressions in the 4995 baseline):
  - 9 EventBus unit cases — id sequencing, live delivery, replay,
    replay+live splice, fan-out to N subscribers, eviction on
    overflow, abort-signal unsubscribe, bus.close() drains
    subscribers, ring-size eviction.
  - 4 bridge subscribe cases — 404, sessionUpdate→event publishing
    via real ACP fake-agent, shutdown closes live subscriptions.
  - 4 SSE route cases against a live HTTP listener — frame format,
    Last-Event-ID forwarding, 404, abort propagation on disconnect.

Verified end-to-end against a real `qwen --acp` child:
  - Subscribed to `/session/$SID/events`, fired `POST /session/$SID/prompt`
    with text content. Captured 13 distinct `event: session_update`
    SSE frames in real time during the model's response — `available_
    commands_update` metadata, 9 `agent_thought_chunk` frames carrying
    the model's chain-of-thought, 3 `agent_message_chunk` frames with
    the actual reply, and a final usage frame with token totals.
  - Frames carry monotonic ids 1..13, the daemon-side counter, and
    are valid SSE per the EventSource spec.

* feat(cli): wire POST /permission/:requestId for `qwen serve` (#3803)

Stage 1 follow-up that turns `BridgeClient.requestPermission` from a
hardcoded `cancelled` placeholder into a real first-responder vote
loop, and ships the HTTP route any attached client uses to cast the
deciding vote.

Bridge:
  - `requestPermission` generates a UUID requestId, registers a
    pending entry on a daemon-wide map (and the owning session's
    `pendingPermissionIds` set), publishes a `permission_request`
    event onto the session's EventBus (so SSE subscribers see it),
    and awaits the resolution.
  - New `respondToPermission(requestId, response)` resolves the
    pending promise with the supplied outcome. First call wins —
    subsequent calls return false. On success the bridge publishes a
    `permission_resolved` event so other attached clients can update
    their UI when the race is decided.
  - `cancelSession` and `shutdown` both resolve every still-pending
    permission for the affected session(s) as
    `{ outcome: { outcome: 'cancelled' } }` per the ACP spec
    requirement that a cancelled prompt MUST resolve outstanding
    requestPermission calls with cancelled.
  - New `pendingPermissionCount` getter exposes inflight count for
    inspection / tests.

Route (`server.ts`):
  - `POST /permission/:requestId` validates the body's `outcome` is
    either `{ outcome: 'cancelled' }` or `{ outcome: 'selected',
    optionId: string }`, then forwards to `bridge.respondToPermission`.
  - 200 on accepted vote, 404 when the requestId is unknown or
    already resolved (Stage 1 doesn't differentiate), 400 on a
    malformed outcome.

Capabilities envelope: STAGE1_FEATURES gains `permission_vote`.

Tests (14 new — 9 bridge + 5 route, 0 regressions in the 5011 baseline):
  - Bridge: publishes permission_request with a generated requestId
    and waits; respondToPermission first-responder wins; publishes
    permission_resolved on vote; respondToPermission false for
    unknown requestId; cancelSession resolves outstanding as
    cancelled; shutdown resolves outstanding as cancelled.
  - Route: 200 on selected outcome; 200 on cancelled outcome; 404 on
    unknown requestId; 400 on malformed outcome; 400 on missing
    outcome.

Verified end-to-end against a real `qwen --acp` child:
  - Subscribed to /session/$SID/events, sent a prompt asking the
    agent to write a file at /tmp/qwen-serve-permission-e2e-test.txt.
  - The agent triggered a permission_request via the bus, surfacing
    the three options Qwen Code presents (Allow Always / Allow /
    Reject) with their option ids.
  - POSTed `{outcome:{outcome:"selected",optionId:"proceed_once"}}`
    to /permission/$requestId — got HTTP 200.
  - Bus published the matching permission_resolved event.
  - Agent proceeded with the writeTextFile tool call; file was
    actually created on disk with the expected content.

* feat(sdk): add DaemonClient for the qwen serve HTTP API (#3803)

Stage 1 follow-up that proves the cross-mode protocol-isomorphism design
assumption: an SDK client can drive the daemon's HTTP routes end-to-end
without going through ProcessTransport's stdio + stream-json path.

DaemonClient is a sibling of ProcessTransport, not a replacement. The two
speak different protocols (ACP NDJSON over HTTP vs stream-json over
stdio). Existing `query()` users keep getting subprocess-mode unchanged;
applications that want daemon-mode (cross-client attach, shared MCP
pool, network reachability, first-responder permissions) opt in by
constructing a DaemonClient against a running `qwen serve`.

API surface (`packages/sdk-typescript/src/daemon/`):
  - `new DaemonClient({ baseUrl, token?, fetch? })`. The `fetch` override
    is for tests; defaults to `globalThis.fetch`. Trailing slashes on
    `baseUrl` are stripped.
  - `health()`, `capabilities()` — discovery.
  - `createOrAttachSession({ workspaceCwd, modelServiceId? })` — `attached:
    true` on the response indicates a session was reused under
    sessionScope:single.
  - `prompt(sessionId, { prompt: ContentBlock[] })` — returns
    PromptResult with stopReason.
  - `cancel(sessionId)` — tolerates 204; throws on 404.
  - `subscribeEvents(sessionId, { lastEventId?, signal? })` — async
    iterator over parsed SSE frames; AbortSignal-aware. Native Node
    AbortController only — jsdom polyfills are incompatible with undici.
  - `respondToPermission(requestId, response)` — first-responder vote;
    returns true on 200, false on 404 (lost the race or unknown id),
    throws on 400/500.

`DaemonHttpError` is thrown for any non-2xx (besides the 404
"already-resolved" case on permission votes); carries `status` and
`body` so callers can branch on standard daemon HTTP semantics.

`parseSseStream(body)` is the underlying SSE parser; exported separately
so applications can consume daemon SSE outside the DaemonClient surface.
Handles split-chunk frames, comment/retry directives, malformed JSON
(skip), trailing frame without final newline.

Wire types live SDK-side (no SDK→CLI dep); the capabilities envelope's
`v` field signals breaking changes.

Tests (26 new, 0 regressions in the 201 baseline):
  - 7 SSE parser cases — single frame, multiple frames, comments,
    chunked-split frame, malformed JSON skip, trailing frame on close,
    empty stream.
  - 19 DaemonClient cases — health success/error, capabilities, bearer
    auth presence/absence, createOrAttachSession success/400, prompt
    body shape + sessionId url-encoding, cancel 204/404, permission
    200/400/404, subscribeEvents header forwarding + 404, baseUrl
    normalization.

Verified end-to-end against a real `qwen serve` daemon driving a real
`qwen --acp` child:
  - `client.capabilities()` returned `{v:1, mode:"http-bridge", features:
    [...7 tags]}`.
  - First `createOrAttachSession` returned `attached:false`; second
    returned `attached:true` with the same sessionId.
  - `client.prompt(...)` with text content yielded `{stopReason:
    "end_turn"}` while the parallel `subscribeEvents` iterator streamed
    10 distinct frames during the same turn.
  - AbortController on the events iterator cleanly severed the SSE
    connection.

* feat(cli,sdk): list workspace sessions + set session model (#3803)

Closes the §04 Stage-1 routes table for `qwen serve` with the two
remaining endpoints, plus matching SDK methods.

`GET /workspace/:id/sessions`
  - `:id` is the URL-encoded canonical absolute workspace path
    (Express decodes path params automatically; clients pass
    `encodeURIComponent(cwd)`).
  - Returns `{ sessions: [{ sessionId, workspaceCwd }, ...] }` for live
    sessions whose canonical workspace matches.
  - Empty array (not 404) when the workspace is idle so picker UIs
    don't have to special-case "no sessions yet".
  - 400 when the decoded path isn't absolute.

`POST /session/:id/model`
  - Body: `{ modelId: string, ... }`. The route's `:id` overrides any
    spoofed sessionId in the body.
  - Forwards to ACP's `unstable_setSessionModel` and publishes a
    `model_switched` event onto the session bus so cross-client UIs
    update.
  - 200 with the agent's response on success, 400 on missing/empty
    modelId, 404 on unknown session.
  - The SDK method is currently unstable; documented in the bridge
    comment in case the spec renames the method when it stabilizes.

Bridge:
  - New `listWorkspaceSessions(workspaceCwd)` iterates `byId.values()`
    and filters by canonical workspace path; works for both `single`
    and `thread` session scopes.
  - New `setSessionModel(sessionId, req)` forwards through
    `connection.unstable_setSessionModel`, normalizes sessionId,
    publishes `model_switched`, throws SessionNotFoundError on
    unknown ids.

`STAGE1_FEATURES` capabilities envelope grows to 9 tags, adding
`session_list` and `session_set_model`.

SDK (`DaemonClient`):
  - `listWorkspaceSessions(workspaceCwd)` URL-encodes the cwd and
    returns the parsed `sessions` array directly.
  - `setSessionModel(sessionId, modelId)` POSTs the body and returns
    the agent response (currently opaque per ACP unstable spec).
  - Wire types `DaemonSessionSummary` and `SetModelResult` exported
    from the SDK barrel.

Tangential cleanup: `sendBridgeError` now extracts a useful message
from non-Error values via a small `errorMessage` helper. JSON-RPC
errors from the agent (`{code, message, data}`) used to surface as
`"[object Object]"` in the 500 response body; they now show the
inner `message` field. Caught while running the model-set e2e.

Tests (17 new — 9 bridge + 7 route + 4 SDK, 0 regressions in the
5022 + 227 baselines):
  - Bridge listWorkspaceSessions: matching cwd returns the live
    sessions; canonicalizes the lookup; empty for relative paths.
  - Bridge setSessionModel: forwards modelId + overrides body
    sessionId; publishes model_switched event; 404 unknown session.
  - Route /workspace/:id/sessions: returns the bridge list; empty for
    idle workspace; 400 for relative path.
  - Route /session/:id/model: 200 success; 400 missing modelId; 400
    empty modelId; 404 unknown session.
  - SDK listWorkspaceSessions: URL-encodes the cwd; throws on 400.
  - SDK setSessionModel: posts body; throws on 404.

Verified end-to-end against a real `qwen serve`:
  - SDK reports 9 capability features, list returns the existing
    session, attached:true on repeat create, and `setSessionModel`
    rejects with HTTP 500 when the modelId isn't registered (with the
    daemon now surfacing "Internal error" instead of "[object Object]").
  - 404 path through SDK on unknown sessionId works.

* fix(cli,sdk): audit round 1 follow-ups for `qwen serve` (#3803)

Self-review pass on PR #3889. Two real correctness bugs and an
ergonomics gap, plus the test-coverage holes the audit surfaced. The
loudest finding ("host allowlist no-op when bind=localhost") was a
false positive — the conditional was misread; existing tests already
prove the validator is active on `localhost` binds.

Real fixes:

  - Bearer-auth timing-attack: `parts[1] !== token` short-circuits per
    byte, leaking which prefix is correct via response latency. Replace
    with SHA-256 of both sides + `crypto.timingSafeEqual` so comparison
    is constant-time regardless of token length.

  - Concurrent `spawnOrAttach` race in single-scope: two parallel
    callers for the same workspace both passed the `byWorkspace.get`
    check, both spawned, and one entry ended up orphaned in `byId`
    while the other won `byWorkspace`. Violates the
    "at most one session per workspace" invariant. Coalesce via an
    `inFlightSpawns` map: parallel callers attach to the in-flight
    promise and report `attached: true`. The slot is cleared on both
    success and rejection so a failed spawn doesn't poison the
    workspace forever. New test asserts ONE channel spawns under
    parallel calls and that retry works after rejection.

  - `Number.parseInt('1.5e10z', 10)` returns 1, so a malformed
    `Last-Event-ID` header silently passes through. Tighten
    `parseLastEventId` to `^\d+$` so anything not a pure decimal
    integer is dropped. New test exercises 'abc', '-1', '1.5e10z'.

Ergonomics:

  - `LOOPBACK_BINDS` and `LOOPBACK_HOST_BINDS` now include `::1` and
    `[::1]`. IPv6 loopback users no longer have to set a token.
    Host-allowlist allows `[::1]:port` Host headers.

Documentation:

  - `BridgeClient` doc-comment now states the Stage 1 trust model
    explicitly: agent runs as the same UID, the file-proxy methods
    are NOT a workspace-cwd sandbox, restricting them would be
    theatre. The audit flagged this as a "design gap" but the
    daemon-and-agent-on-same-host posture makes a sandbox here
    redundant — Stage 4+ remote-sandbox swaps the Client for a
    sandbox-aware variant.

SDK fix:

  - `DaemonClient.failOnError` previously called `res.json()`, which
    consumes the body even on parse-failure; the subsequent
    `res.text()` returned empty. New impl reads once as text and
    attempts JSON-parse; raw text is the fallback. New test asserts
    a `text/plain` 502 surfaces the body verbatim.

Test gap fills (audit-flagged):

  - Bridge: in-memory file-proxy tests for `BridgeClient.{read,write}
    TextFile` including line/limit slicing.
  - SSE route: `stream_error` synthetic frame on iterator throw
    mid-stream; numeric Last-Event-ID forwarded; malformed
    Last-Event-ID dropped.
  - DaemonClient: text/plain error body coerced to `body` field;
    `respondToPermission` 5xx throws; `subscribeEvents` null-body
    throws; `cancel`/`respondToPermission` URL-encode session/request
    ids that contain slashes.

Verified end-to-end with a token-required daemon: right token → 200,
wrong/missing/malformed → 401. All paths return uniform 401 messages
so a side-channel can't distinguish between "no header", "bad scheme",
and "wrong token".

Test counts: cli serve **89** (was 81, +8), sdk daemon **35** (was
30, +5). Full suites still green.

* fix(cli): audit round 2 follow-ups for `qwen serve` (#3803)

Second self-review pass on PR #3889. Three real bugs (one
correctness, one resource-cleanup, one cosmetic) plus consolidation
of the loopback bindings into a single source of truth.

Real fixes:

  - Shutdown could hang forever on a long-lived SSE consumer:
    `server.close` waits for every in-flight connection to drain,
    and a paused EventSource client never disconnects. Added a
    `SHUTDOWN_FORCE_CLOSE_MS` (5s) timer that calls
    `server.closeAllConnections()` to force-destroy stuck sockets,
    then resolves so `process.exit(0)` can run. New test asserts
    close completes well under 5.5s even when an SSE GET is in
    flight.

  - Signal-handler race during shutdown: round 1 detached the
    SIGINT/SIGTERM listeners *up front* in `handle.close()`. If a
    second SIGTERM arrived during the drain, no handler existed and
    Node's default termination ran, orphaning agent children. Switch
    to detaching at the *end* of the close path (in `finish()`):
    during the drain window the handler is still attached and the
    `if (shuttingDown) return` guard makes a second signal a no-op;
    after drain completes we can safely remove the listeners (this
    also fixes a test-suite MaxListenersExceededWarning that fired
    once we ran the runQwenServe tests >10 times in a single
    process).

  - SSE response had no `error` listener. When the underlying TCP
    socket died (RST, kill -9 on the client), the next `res.write`
    threw EPIPE and Express forwarded it to the default error
    handler, logging noisily. Added `res.on('error', cleanup)` so
    the failure is absorbed and triggers the same teardown path the
    `req.on('close')` handler uses.

Validation:

  - `createHttpAcpBridge` now throws on invalid `sessionScope` (anything
    other than `'single'` or `'thread'`) and on `initializeTimeoutMs <= 0`.
    Misconfigured callers used to silently degrade to thread behavior;
    now they fail loudly.

Cleanup:

  - The `LOOPBACK_BINDS` set was duplicated between `auth.ts` and
    `runQwenServe.ts` (round 1 missed this). Extracted into
    `packages/cli/src/serve/loopbackBinds.ts` with a single
    `isLoopbackBind(hostname)` helper. Both files now import; drift is
    impossible.

  - `res.flushHeaders?.()` lost the optional chaining. The method is
    on `http.ServerResponse` since Node 1.6; our `engines` floor is 20.

Tests added:

  - bridge: `sessionScope` validation, `initializeTimeoutMs` validation.
  - server: shutdown force-close timeout, SIGINT/SIGTERM listener
    detach-after-drain.

False positives from the round 2 audit (verified and dismissed):

  - "EventBus nextId overflow at 2^53" — theoretical only (would
    require ~9 quadrillion publishes per session). No code change.
  - "Subscribe-during-close race" — JS is single-threaded; the close()
    flag is set synchronously before the loop touches state.
  - "Queued prompts on shutdown" — by design; documented via the
    promptQueue tail comment.
  - "10MB body parser limit" — design choice for Stage 1's in-memory
    buffering model; revisit if ACP streaming lands in Stage 2.
  - "Unbounded body read in DaemonClient.failOnError" — daemon is
    local in Stage 1; the threat surface for adversarial-large error
    bodies is the same as the daemon's other unbounded buffers.

Test counts: cli serve **93** (was 89, +4), full cli **5047** (no
regressions), sdk **236** (no regressions).

* docs(cli): audit rounds 3 + 4 follow-ups for `qwen serve` (#3803)

Two more self-review passes on PR #3889. No correctness bugs surfaced
this time — round 3 found a HIGH-severity Windows-path claim that
turned out to be a false positive (`path.win32.isAbsolute('/foo/bar')`
returns true; verified against Node 20). Round 4 confirmed every
prior decision and surfaced one latent-but-not-currently-triggered
concurrency note.

Changes are pure documentation + a tiny optional-chain cleanup:

  - Drop `?.` on `server.closeAllConnections()` in runQwenServe.ts —
    the method exists since Node 18.2 and our `engines` floor is 20.
    The optional chain dated from before round 2's force-close timer
    landed; clean it up.

  - Help text for `qwen serve --port` now documents that port 0 means
    "OS-assigned ephemeral port" (which the implementation has always
    supported but never advertised).

  - `defaultSpawnChannelFactory` gains a comment near the spawn site
    documenting the FD-budget implication (~3 FDs per session, bump
    `ulimit -n` for many concurrent sessions) and the `stdio:
    ['pipe', 'pipe', 'inherit']` choice (child stderr lands in the
    daemon's stderr, interleaved across sessions). Both are
    Stage-1-accepted; Stage 2/4+ revisit each.

  - Comment on the bridge's `byWorkspace`/`byId` Maps documenting the
    known gap that a child crashing between requests leaves a garbage
    SessionEntry until daemon shutdown — surfaced as a per-prompt
    failure when the dead session is touched, not a hang. Stage 2's
    in-process bridge eliminates the spawned-child failure mode
    entirely so this gap goes away naturally.

  - `EventBus.subscribe` doc-comment now states explicitly that the
    returned iterator is NOT safe to drive from concurrent
    `.next()` callers — the underlying queue isn't atomic. Daemon
    usage is the sequential `for await ... of` inside the SSE route,
    so this is safe in production. Documented so a future fan-out
    consumer doesn't accidentally rely on undefined behavior.

False positives verified and dismissed (round 3 + 4 combined):

  - `path.isAbsolute('/foo/bar')` Windows breakage — `path.win32.
    isAbsolute('/foo/bar')` is true; verified empirically.
  - "Windows drive divergence" causing duplicate sessions — different
    drives are different on-disk paths; sessions intentionally
    differ.
  - "parseSseStream early-break leaks reader" — `for await ... break`
    triggers `iterator.return()` which runs the generator's `finally`
    that calls `releaseLock`. Standard JS semantics.
  - "Promise executor sync-throw fragility in requestPermission" —
    sync throws inside `new Promise(executor)` reject the outer
    promise; functionally correct, just stylistic.
  - "Force-close timeout test elapsed assertion flakiness" — assertion
    is `< 5500ms` but the natural happy-path is sub-100ms. Generous
    headroom; not flake-prone in practice.
  - "fetch reference stale after polyfill" — `globalThis.fetch.bind`
    captures at construction; tests inject `opts.fetch` instead of
    polyfilling, which is the correct pattern.

Test counts unchanged (cli serve **93**, sdk **236**); typecheck +
lint clean. STAGE1_FEATURES still matches every implemented route
1:1, fakeBridge in tests implements every HttpAcpBridge method.

* fix(cli): PR #3889 review round 1 — critical correctness (#3803)

Addresses the four critical findings from the PR #3889 reviewer pass:

  1. ACP `ReadTextFileRequest.line` is 1-based per spec, but the
     bridge's `BridgeClient.readTextFile` was treating it as a
     0-based slice index. A client asking for `{line:1, limit:2}`
     ("first two lines") was getting lines 2-3 — a sign-off-by-one
     bug that breaks every editor / SDK client following the ACP
     schema. Convert to 0-based via `Math.max(0, line - 1)`. The
     existing slice test was asserting the wrong behavior; updated
     to expect the spec-correct result and added a second `line:3,
     limit:2` case to lock in the offset.

  2. `modelServiceId` was accepted by the SDK + server `POST /session`
     path, forwarded into `bridge.spawnOrAttach`, and then silently
     dropped: `doSpawn` never wired it into the agent. Callers
     requesting a specific model got the agent's default and no
     indication anything was wrong. Now `doSpawn` issues
     `unstable_setSessionModel` immediately after `newSession`. If
     the agent rejects the model id, the half-initialized session is
     torn down and the spawn rejects so the caller can retry cleanly
     instead of inheriting silent drift. Three new bridge tests:
     happy path, omit-when-undefined, agent-rejection cleanup.

  3. The CORS middleware used `cors({ origin: (o, cb) =>
     cb(new CORSError(...), false) })` for browser-Origin requests.
     `cors` flows the Error into Express's error chain; without an
     explicit error handler that produces a 500 + HTML body, which
     is misleading for what is really a deterministic 403 denial.
     Replace with a tiny `RequestHandler` that checks
     `req.headers.origin` directly and returns
     `403 { error: 'Request denied by CORS policy' }` JSON. Drops
     the `cors` and `@types/cors` dependencies — there's no other
     consumer in the cli package.

  4. The SSE `stream_error` synthetic frame hard-coded `id: 0`,
     which would regress the client's `Last-Event-ID` tracker and
     trigger duplicate replays on reconnect. The frame is terminal
     and daemon-emitted — it has no place in the per-session
     monotonic sequence. Refactor `formatSseFrame` to omit the
     `id:` line when the input event has no id field, and emit
     `stream_error` without one. Test updated to assert
     `frames[1].id === undefined` while the preceding
     `session_update` still carries its monotonic id.

Tangential cleanup: `errorMessage` now formats the SSE error body
(was `err.message` only — would have shown `[object Object]` for
JSON-RPC errors mid-stream, mirroring the round-1 SDK fix).

Test counts: cli serve **96** (was 93, +3 modelServiceId cases);
existing readTextFile slice test rewritten in place. Full
typecheck + lint + suite green.

* fix(cli,sdk): PR #3889 review round 2 — SSE robustness + EventBus polish (#3803)

Second batch of reviewer-flagged fixes for PR #3889. Addresses 7
robustness issues across the daemon's SSE pipeline + the bus + the
SDK's stream parser.

Daemon SSE (`server.ts`):

  - SSE writes now respect backpressure. `res.write` returns false when
    the kernel send buffer is full; the previous code ignored that and
    Node accumulated payloads in user-space memory unboundedly. A slow
    consumer on a chatty session could balloon daemon RSS. New
    `writeWithBackpressure` helper awaits `drain` (or `close`/`error`)
    before scheduling the next write — for both per-frame writes and
    heartbeats.

  - `parseLastEventId` rejects values > `Number.MAX_SAFE_INTEGER`. With
    the prior `^\d+$` regex a malicious 25-digit value would parse to
    a number that loses precision and confuses replay comparisons.

EventBus (`eventBus.ts`):

  - `Last-Event-ID` replay events now `forcePush` past `maxQueued`. A
    client reconnecting with a 1000-event gap on a subscriber whose
    cap is 256 was silently losing entries 257-1000 — a sign-off-by-
    nothing breakage of the resume contract. Live publishes still go
    through the normal cap (slow live consumer must be evictable);
    historical replay is bypassed.

  - `onAbort` now disposes the subscription immediately instead of
    only closing the queue. An aborted-but-never-iterated subscriber
    used to linger in `bus.subs` until the consumer drove `next()` /
    `return()`. New tests cover both abort-after-subscribe and
    already-aborted-at-subscribe paths.

  - `BoundedAsyncQueue.next` now checks `buf.length > 0` before
    shifting instead of `buf.shift() !== undefined`. The bus never
    pushes `undefined` today but the queue is generic — the prior
    pattern would mis-handle a queue whose element type legitimately
    includes undefined.

SDK SSE parser (`sse.ts`):

  - Now flushes the TextDecoder on stream close. Without the final
    `decoder.decode()`, an incomplete multi-byte UTF-8 sequence at
    the tail of the last chunk was silently dropped — corrupting any
    frame whose JSON ended mid-character. New test feeds a stream
    split mid-byte through "中" (3-byte UTF-8) and asserts the
    character round-trips.

  - Frame separators now accept both `\n\n` and `\r\n\r\n`. SSE spec
    allows CRLF, and intermediaries (corporate proxies, some Node
    http servers) sometimes normalize. Frame field splitter also
    accepts `\r?\n`. Two new tests cover pure CRLF + mixed-LF/CRLF.

Test counts: cli serve **99** (was 96, +3 EventBus); sdk daemon-sse
**10** (was 7, +3). Full typecheck + lint + suite green.

* docs(cli,sdk): PR #3889 review round 3 — minor + docs (#3803)

Last batch from the PR #3889 reviewer pass: mostly docs + a
ReDoS-tooling-silencing rewrite + a yargs-key cleanup.

  - `commands/serve.ts` ServeArgs interface dropped the camelCase
    `httpBridge` mirror; the handler now reads `argv['http-bridge']`
    matching the declared option name. The dual surface relied on
    yargs's camelCase expansion behavior — fragile if yargs config
    ever changes.

  - `DaemonClient` constructor's `baseUrl.replace(/\/+$/, '')` (which
    is end-anchored and linear, but CodeQL's polynomial-regex
    detector flags any `\/+$` pattern on attacker-controlled input)
    swapped for a hand-rolled `stripTrailingSlashes` loop. Same
    behavior, no rule trigger.

  - `defaultSpawnChannelFactory`'s `cwd: workspaceCwd` flow into
    `spawn` is the second CodeQL finding ("uncontrolled data used in
    path expression"). It IS user-controlled, by design — that's the
    Stage 1 trust model. Added a `// lgtm[js/shell-command-
    constructed-from-input]` suppression with a comment explaining
    the model and pointing at issue #3803 §11 for the Stage 4+ remote-
    sandbox replacement.

  - Stale doc comment on `createServeApp` that still listed only
    `/health`, `/capabilities`, `POST /session` as shipped — now
    enumerates all 9 routes that match §04 of the design.

  - Stale doc comment on `HttpAcpBridge` saying "Stage 1 buffers them
    in-memory; SSE wiring lands in the next PR" — SSE wiring landed
    in commit 41aa95094. Replaced with a description of the actual
    flow through EventBus + SSE.

No behavior change; tests + lint + typecheck still green. cli serve
still **99**, sdk **38** (was 30 before this batch — daemon-sse +3,
DaemonClient +5 from rounds 1+2). Full e2e against built daemon
re-verified: CORS denial returns 403 JSON (was 500 HTML), bad
`modelServiceId` now causes spawn to fail with HTTP 500 (was: silent
default-model substitution), `POST /session` without modelServiceId
unaffected.

* fix(cli,sdk): self-audit round 5+ — close orphaned EventBus + DaemonEvent.id optional (#3803)

Two more fixes from a final post-review-comment audit pass on PR #3889.
Both are subtle correctness gaps that fell out of the round-1 critical
fixes (modelServiceId apply + SSE id-less stream_error).

  - In `httpAcpBridge.ts:doSpawn`, when `unstable_setSessionModel`
    rejects after `newSession` succeeded, we tear down the entry from
    `byWorkspace` + `byId` (round 1 fix) but did NOT close the
    EventBus we'd just constructed for that entry. The agent could
    have published a session_update notification during init that
    queued in the (now unreachable) bus's ring buffer; without an
    explicit close the bus + buffer linger until the next GC cycle.
    Bounded leak (1 bus per failed spawn × 1000-event ring) but
    cleaner to close it. New regression test exercises the retry path
    after a model-rejection failure to lock in that we don't reuse
    the orphan and that subscribers on the fresh session see an empty
    iterator on immediate abort.

  - SDK `DaemonEvent.id` is now `id?: number` instead of `id: number`.
    The round-1 SSE fix made the daemon emit `stream_error` frames
    *without* an `id:` line so they don't pollute the per-session
    monotonic sequence. The SDK parser correctly returns `undefined`
    for the missing field, but the type still advertised `id: number`
    — TypeScript consumers persisting `lastSeenId = event.id` would
    accidentally store `undefined`. Made the field optional and added
    a doc comment instructing consumers to skip frames without an id.

Plus one more false-positive verified and dismissed:

  - "writeWithBackpressure Promise double-settle race": the auditor
    flagged that `res.write(chunk, callback)` could fire its callback
    after the synchronous `ok=true` resolve. Verified harmless —
    Promise double-settle is a no-op, the callback only rejects on
    error (caught separately by `res.on('error', cleanup)`), and
    multiple parallel writes register independent listener sets that
    each remove their own pair after firing.

Test counts: cli serve **100** (was 99, +1 retry-after-model-rejection
regression). SDK unchanged at 239. Full typecheck + lint + suites
green; flow re-verified end-to-end.

* fix(cli,sdk): PR #3889 review round 4 — child-crash recovery + SSE/permission/SSE polish (#3803)

Fourth and final batch of reviewer-flagged fixes for PR #3889. 14
inline threads addressed, plus 8 spam threads up for resolution.

Critical correctness:

  - `eventBus.test.ts`'s ring-eviction test wrapped its assertion in a
    `void (async () => { … })()` IIFE that returned synchronously to
    vitest — the inner `expect` could fail without ever surfacing.
    Hoisted to a top-level `await` so the harness actually waits and a
    broken eviction would now fail loudly.

  - `runQwenServe.ts handle.close()` is now idempotent. Concurrent
    callers (test harness + signal handler firing simultaneously,
    explicit caller + finally-block fallback) used to each construct a
    new shutdown promise, arm a fresh force-close timer, and call
    `bridge.shutdown` redundantly. Cache a single `closePromise`;
    repeat calls return it. New test exercises 3 overlapping callers
    + a post-settle call → exactly one bridge.shutdown.

  - `POST /permission/:requestId` now rejects `outcome.selected` with
    an empty `optionId`. The string-typeof check passed `""` through;
    bridge would forward an opaque "unknown option" error from the
    agent. Tighten the validator + add a 400 test.

  - `denyBrowserOriginCors` now has explicit unit tests (3 cases:
    Origin-bearing GET → 403 JSON, no-Origin GET → 200, Origin-bearing
    POST → 403 + bridge untouched). The CSRF defense was previously
    implicit-only.

Channel-exit recovery:

  - `AcpChannel` interface gains an `exited: Promise<void>` that
    resolves on either planned `kill()` or unexpected child crash.
    Bridge subscribes via `channel.exited.then(...)`: if the entry is
    still in `byId` when exit fires (i.e. unexpected crash), it
    cancels pending permissions, publishes a `session_died` event so
    SSE subscribers get notified, closes the bus, and removes the
    entry from `byWorkspace`/`byId`. Without this, a crashed child
    used to leave its `SessionEntry` stuck — under
    `sessionScope:'single'` (default) the whole workspace was
    unreachable until daemon restart.

  - `defaultSpawnChannelFactory` now wires `child.once('error', …)` in
    addition to `'exit'`. Without an `error` listener Node treats an
    async spawn failure (ENOMEM, EACCES, …) as an unhandled error and
    crashes the daemon.

  - Two new bridge tests: `crash()` simulates an unexpected exit →
    asserts `session_died` event + entry removed + retry spawns a
    fresh child; planned shutdown asserts the cleanup handler no-ops
    when the entry is already gone (no double-publish).

SSE robustness:

  - SDK `parseSseStream` now calls `reader.cancel()` (not just
    `releaseLock`) in its `finally`. Early-break consumers were
    leaving the underlying HTTP body stream open; cancel propagates
    upstream so the connection drops promptly. New test asserts the
    underlying ReadableStream's `cancel()` runs.

  - SDK `parseSseStream` accepts `data:` (no space after colon) AND
    multiple `data:` lines per frame (joined by `\n` per spec). Two
    new tests cover both cases.

  - SDK `DaemonClient.subscribeEvents` now validates response
    Content-Type before delegating to the parser. A misconfigured
    proxy returning 200 + JSON was silently producing zero events;
    now throws `DaemonHttpError` with the actual mime type.

  - Daemon SSE route's initial `retry: 3000` write now `.catch(()=>{})`s.
    A socket that errors before the first write would have surfaced as
    an unhandled rejection.

Documentation (deferred items now noted in code):

  - `EventBus.publish` ring shift is O(n) when full. Comment notes
    the deferral; circular-buffer refactor only if profiling flags it.

  - SSE heartbeat doesn't detect dead connections without TCP RST.
    Comment notes Stage 2 may add an explicit idle timeout.

  - `defaultSpawnChannelFactory` won't run a `.ts` entry directly —
    `npm run dev` users must build first. Comment in the spawn site.

Test counts: cli serve **107** (was 100, +7), SDK daemon **42**
(was 38, +4). Full typecheck + lint + suite green.

* test(integration): qwen serve daemon — routes + streaming + recovery (#3803)

Persists the e2e validation of every PR #3889 fix as vitest
integration tests under `integration-tests/cli/`. Two files split by
auth requirement:

`qwen-serve-routes.test.ts` (18 cases, no LLM credential needed)
  - Bearer auth timing-safe compare: right token / wrong-same-length /
    wrong-shorter / missing / Basic-scheme.
  - CORS browser-Origin denial: GET-with-Origin → 403 JSON; no-Origin
    → 200.
  - Capabilities envelope: all 9 Stage 1 features advertised in order.
  - POST /session validation: relative cwd → 400; two parallel POSTs
    same workspace coalesce; bad modelServiceId tears down half-init.
  - POST /permission/:requestId validation: empty optionId → 400;
    missing optionId → 400; valid vote on unknown id → 404.
  - SDK SSE Content-Type guard: throws DaemonHttpError when upstream
    returns 200 + JSON.
  - Last-Event-ID strict parsing: malformed value accepted but
    ignored (`'1abc'` doesn't get parsed as 1).
  - Cancel idempotent + listWorkspaceSessions returns the live session.

`qwen-serve-streaming.test.ts` (3 cases, gated by SKIP_LLM_TESTS)
  - Real `qwen --acp` child SIGKILL → daemon publishes
    `session_died`, removes the entry from `byWorkspace`/`byId`,
    next createOrAttachSession spawns fresh. Uses `pgrep -P` to
    locate the daemon's direct child by PID.
  - Two SSE subscribers + a tool requiring permission: both observe
    the same `permission_request` requestId; two concurrent POST
    votes resolve as exactly one 200 + one 404 (first-responder
    wins).
  - SSE reconnect with `Last-Event-ID: N` after consuming N frames
    yields events with `id > N` from the bus's replay ring.

Both files spawn `node packages/cli/dist/index.js serve --port 0
--token …` per `beforeAll` and clean up in `afterAll`. Use the
existing `@qwen-code/sdk` alias the integration-tests vitest config
already wires to the built SDK bundle.

Run with the existing `npm run test:integration:cli:sandbox:none`
(or any of the integration-tests target). The streaming file is
skip-able via `SKIP_LLM_TESTS=1` for environments without auth.

Verified locally: 18/18 routes pass in ~6.8s; 3/3 streaming pass in
~23s against a real model.

* fix(cli): PR #3889 review round 5 — claude-opus-4-7 audit (#3803)

Seven new substantive findings from a `/qreview` pass on PR #3889.
Six real bugs + one type-safety gap; all addressed.

Critical correctness:

  - **EventBus replay overflow + eviction race**. Round 4's
    `forcePush` for `Last-Event-ID` replay bypassed the per-subscriber
    cap, but `BoundedAsyncQueue.push`'s cap check was `buf.length >=
    maxSize` — so the very next live publish saw the inflated buf,
    rejected, and triggered the `client_evicted` terminal frame.
    Concrete sequence the audit walked through: client reconnects
    after 300+ events, replay force-pushes 300 entries, next live
    event evicts them. Defeats the resume contract.

    Fix: track force-pushed items separately (`forcedInBuf` counter).
    `push()` cap is now on `(buf.length - forcedInBuf)`. `next()`
    decrements `forcedInBuf` as the consumer drains (force-pushed
    entries are FIFO at the front of `buf` since `forcePush` only
    runs at subscribe time, before any live `push`). Two new
    regression tests: (1) live publish after a >cap replay does
    NOT evict; (2) eviction triggers only after the LIVE backlog
    (excluding replay) hits the cap.

Performance + UX:

  - **Eager express import on every `qwen` invocation**. The
    `serve` subcommand statically imported `../serve/index.js`,
    which transitively pulled express + body-parser + qs into
    cold-start path of every CLI invocation (interactive, mcp,
    channel, etc). ~50ms tax on the 99% of invocations that never
    run `serve`. Defer to dynamic `import()` inside the handler;
    types are still imported for the builder shape.

  - **Middleware order**: `express.json({limit:'10mb'})` ran
    BEFORE `bearerAuth`. Unauth POST got full JSON.parse before
    401. Trivial DoS amp on non-loopback deployments. Reorder so
    auth + Host allowlist + CORS run first; body parser runs
    only for requests that pass the gate.

  - **`sendPrompt` no AbortSignal**. A stuck/dead child poisons
    the per-session FIFO; HTTP client disconnect didn't propagate
    so daemon CPU stayed tied up. `HttpAcpBridge.sendPrompt` now
    accepts `signal?: AbortSignal`. Route handler creates an
    AbortController and wires `req.on('close')` to abort it. On
    abort, bridge sends an ACP `cancel` notification; the agent
    winds down → prompt resolves with `stopReason: 'cancelled'`
    → next queued prompt can run. New test exercises real
    socket disconnect via `node:http` (jsdom AbortSignal isn't
    compatible with undici).

Security:

  - **`--token` on argv leaks via `/proc/<pid>/cmdline`**. Default
    Linux permissions allow any local user to `ps auxww | grep
    'qwen serve'` and read the bearer token. Daemon now warns to
    stderr when `--token` is used and recommends
    `QWEN_SERVER_TOKEN` (which uses `/proc/<pid>/environ`,
    owner-only).

  - **Token inherited by spawned `qwen --acp` child**. `env:
    process.env` in `defaultSpawnChannelFactory` passed
    `QWEN_SERVER_TOKEN` into the child. The agent runs
    user-supplied prompts with shell-tool access — leaving the
    token in env enables prompt-injection-into-self-call attacks.
    Strip `QWEN_SERVER_TOKEN` from the child's env before spawn.

Robustness:

  - **`BridgeClient` publishes lacked try/catch on closed bus**.
    `BridgeClient.requestPermission` and `sessionUpdate` called
    `entry.events.publish(...)` directly. Shutdown closes the bus
    *before* killing the channel, so a late `sessionUpdate` from a
    not-yet-dead agent throws. For `requestPermission` the throw
    was particularly bad: `registerPending` had already mutated
    the daemon-wide map, so the throw left the registry
    inconsistent. Cleaner fix: make `EventBus.publish` a no-op on
    closed bus (returns undefined) instead of throwing. Removes
    the need for try/catch at every call site and keeps state
    consistent.

Type safety:

  - **`STAGE1_FEATURES: readonly string[]`** widened the inferred
    tuple-of-literals back to `string[]`. A typo'd feature
    (`'sesion_set_model'`) compiled silent. Drop the annotation +
    add `as const`; export `Stage1Feature` literal-union for
    SDK-side `features.includes(...)` checks to narrow against.

Test counts: cli serve **112** (was 105, +7); SDK unchanged at
243. Full typecheck + lint + suite green.

* fix(cli): PR #3889 review round 6 — gpt-5.5 audit (#3803)

Four new findings from a `/review` pass on PR #3889. Three real
correctness bugs + one Stage 1 design-gap documentation.

Critical:

  - **`[::1]` bind ENOTFOUND**. `LOOPBACK_BINDS` accepts `[::1]` for
    the auth gate, but `app.listen()` wants the unbracketed `::1`;
    `qwen serve --hostname [::1]` passed the gate and then crashed
    with ENOTFOUND. Strip brackets at bind-time, keep them for the
    printed URL. New test asserts the listener actually binds when
    the operator types `[::1]`.

  - **`sendPrompt` no transport-close detection**. The chained
    `entry.connection.prompt()` could hang indefinitely if the
    `qwen --acp` child wedged or the underlying stream broke
    mid-flight (the SDK's pending JSON-RPC promise never delivers
    a response). Because the per-session FIFO tail derives from
    that promise, a single stuck prompt poisoned every subsequent
    caller for the same session. Round 4's `channel.exited` is
    already wired to remove the entry, but the in-flight prompt
    itself wasn't racing it.

    Fix: race `entry.connection.prompt(...)` against
    `entry.channel.exited` inside `sendPrompt`; when the transport
    closes mid-flight, the prompt fast-fails with a descriptive
    error rather than hanging the queue. New test exercises this
    via a stuck fake agent + manual `crash()`.

Real correctness:

  - **`spawnOrAttach` attach-path ignored modelServiceId**. Under
    `sessionScope:'single'` (default) a client requesting a
    specific model on attach got `attached:true` while continuing
    to use whatever model the shared session already had — a
    silent contract drift. Refactor the per-session
    `unstable_setSessionModel` call into a shared
    `applyModelServiceId(entry, modelId)` helper that runs both at
    create-time (existing path) AND on attach-with-model. Same
    helper publishes the `model_switched` event so cross-client
    UIs see the change. New tests cover apply-on-attach and the
    omit-modelServiceId-on-attach no-op case.

Stage 1 design:

  - **`BridgeClient.{readTextFile, writeTextFile}` raw fs proxy**.
    The audit flagged that the bridge reimplements file I/O with
    `fs.{read,write}File` instead of delegating to core's
    filesystem service — divergence on BOM handling, non-UTF-8
    encodings, original line endings. Wiring core's
    FileSystemService through the bridge is invasive (constructor
    dep, reaches into core's runtime), and Stage 2's in-process
    bridge eliminates the proxy entirely. Documented as a
    known gap with the exact user-visible scenarios; no behavior
    change in this PR.

Test counts: cli serve **116** (was 112, +4); full cli **5070**
(was 5066, +4); SDK unchanged at 243. Lint + typecheck green.

* fix(cli): PR #3889 review round 7 — match CodeQL suppression to fired query (#3803)

Single new CodeQL alert (#201) on `workspaceCwd → spawn({cwd})`. The
round-3 suppression I added (`lgtm[js/shell-command-constructed-from-
input]`) referenced the WRONG query id — the alert fires the
`js/path-injection` query, not the shell-command one. The misnamed
suppression also lived 30+ lines above the actual flagged spawn call,
out of CodeQL's annotation scope.

Move the suppression onto the line immediately preceding the spawn
call and use the matching query id `js/path-injection`. The
function-level comment block above still documents the Stage 1 trust
model rationale (operator-controlled cwd is intentional; agent runs
as same UID with shell-tool access; Stage 4+ remote sandbox replaces
this factory entirely).

Defense-in-depth note added: `workspaceCwd` is canonicalized via
`path.resolve()` in `spawnOrAttach` before reaching this factory, and
spawn's `cwd` doesn't pass through any shell.

No behavior change. Test counts unchanged (cli serve 116, full cli
5070).

* fix(cli): self-audit round 8 — concurrency + listener leak + IPv6 + CodeQL honesty (#3803)

Multi-round audit pass on PR #3889 commits 5/6/7. Four findings, one
real high-severity.

High:

  - Attach-with-modelServiceId had no error recovery and no FIFO. If
    the agent rejected the new model on attach, `applyModelServiceId`
    threw, the route 500'd, and the existing session kept running the
    OLD model — caller sees a 500 with no easy way to detect the
    state. Worse, two simultaneous attaches with different
    modelServiceIds would race the `unstable_setSessionModel` calls
    with no serialization. Add a per-session `modelChangeQueue`
    (parallel to `promptQueue`); `applyModelServiceId` now chains
    through it. On failure publishes a `model_switch_failed` event to
    the bus so OTHER attached clients can see what happened (the
    failed-caller still gets the 500). Two new bridge tests cover
    rejection observability + concurrent FIFO.

Medium:

  - `sendPrompt` was adding a `.then` listener to
    `entry.channel.exited` PER CALL, accumulating linearly with
    prompt count over a session's lifetime. ~hundreds of bytes per
    prompt; trivially observable on chatty long-running sessions.
    Cache a single `transportClosedReject` lazy-init promise on
    SessionEntry; every subsequent prompt's race uses the same
    promise.

Low:

  - `[host]:port` IPv6 syntax in `--hostname` was being naively
    bracket-stripped to `host]:port`, which Node rejects with a
    cryptic ENOTFOUND at startup. Tighten the strip to only
    accept pure `[addr]` forms; reject the URL-with-port form
    upfront with a useful error pointing at `--port`.

  - `BoundedAsyncQueue.forcedInBuf` invariant comment was wrong: it
    claimed force-pushed items were always at the front of `buf`,
    but the eviction-frame path force-pushes at the BACK. The
    miscount that follows is functionally inert (`close()` blocks
    the next cap check), but the comment was actively misleading.
    Rewrote it to honestly describe both call paths and explain
    why the eviction-case miscount is harmless.

CodeQL honesty:

  - Round 7's `// lgtm [js/path-injection]` comment doesn't actually
    suppress alerts — GitHub Code Scanning ignores inline `lgtm`
    annotations (LGTM.com retired 2021). Replaced the misleading
    `// lgtm` line with a NOTE block stating the constraint
    explicitly: suppression requires UI dismissal or
    `.github/codeql/codeql-config.yml`, both out of scope for a
    code-only PR. The function-level comment that explains the
    Stage 1 trust model rationale stays.

Test counts: cli serve **119** (was 116, +3); full cli **5073**
(was 5070, +3, no regressions).

* fix(cli): self-audit round 9-10 — reject empty-bracket --hostname (#3803)

Final fix from rounds 9-10 of the audit chain. One real concern + three
nice-to-have test gaps that the code already handles correctly.

  - `--hostname '[]'` (empty brackets) used to slip past the bracket
    validator: `slice(1, -1)` produced `''`, which Node interprets as
    "bind to all interfaces". An operator typing `[]` clearly meant
    something specific, not wildcard. Reject the empty-inner case
    upfront with the same useful error as the `[host]:port` case.
    New test asserts the rejection.

Round 10 ran a clean convergence pass and signed off:
  - Cross-cutting state invariants (byWorkspace, byId, inFlightSpawns,
    pendingPermissions, plus all per-entry queues and caches) — all
    mutations paired and async holes safe.
  - All test names match assertions.
  - Public type surface clean (DaemonEvent.id?, Stage1Feature
    CLI-only, DaemonClientOptions.fetch shape correct).
  - Production paths verified: non-executable child times out at 10s
    init, multiple-daemon EADDRINUSE rejects cleanly via
    `server.once('error', reject)`.
  - Three "missing test" notes (transportClosedReject cache sharing,
    full subscribe-publish-evict sequence, modelChangeQueue failure
    isolation) are diagnostic gaps — the code paths are correct and
    covered by adjacent tests.

Test counts: cli serve **120** (was 119, +1 empty-bracket); SDK
unchanged at 243.

* docs(cli): note SSE single-line data emit vs multi-line parser (#3803)

formatSseFrame emits the payload as a single `data:` line. The
EventSource spec also allows a frame to span multiple `data:` lines
(joined by `\n` on parse), and the SDK receive-side parser handles
that variant — but we never emit it because the JSON payload has no
embedded newlines after JSON.stringify. Document the in/out asymmetry
so future readers don't mistake the absence of newline splitting for
a bug. Closes review thread AMgP0.

* fix(cli,sdk): close 11 #3889 review threads — race + leak + IPv6 + SSE

Critical correctness:
- setSessionModel now serializes through `entry.modelChangeQueue` so
  POST /session/:id/model can't race with the attach-with-different-
  modelServiceId path that already chains on the same queue. Without
  this two concurrent model changes interleave and the published
  `model_switched` event may not match the agent's actual model.
- POST /session reaps the spawned child when the client disconnected
  during the 1-3s spawn window (`req.aborted && !session.attached`).
  Without this, every aborted request leaks one orphan child the
  daemon can't address by sessionId. Attached sessions skip the kill
  — another client legitimately owns them.
- spawnOrAttach refuses dispatch once shutdown has started
  (`shuttingDown` flag set at the top of `shutdown()`). Late-arrivers
  on already-established HTTP connections that pass `server.close`'s
  rejection of NEW connections would otherwise spawn children the
  shutdown snapshot already missed. Late re-check inside `doSpawn`
  (after `connection.newSession` resolves) catches the in-flight case
  and tears down the half-built channel.
- sendPrompt early-aborts pre-aborted callers before queuing — saves
  a queue trip and gives a clean trace for retry-after-abort flows.

Defensive:
- parseSseStream caps the unread buffer at 16 MiB. Without this, an
  upstream that returns non-SSE (misconfigured proxy, long-lived
  non-streaming body) feeds `buf` until the consumer OOMs.
- parseSseStream now accepts an optional AbortSignal that is checked
  at each iteration, and DaemonClient.subscribeEvents forwards
  `opts.signal` into it. Post-200 aborts now actually stop iteration
  instead of buffering frames until the upstream closes.
- DaemonClient.fetchTimeoutMs (30s default) wraps every short-poll
  method (health/capabilities/createOrAttachSession/listWorkspaceSessions/
  setSessionModel/cancel/respondToPermission) with `AbortSignal.timeout`.
  Composes with caller-provided signals via `AbortSignal.any`. `prompt`
  is intentionally exempt (long-lived: model + tool turns can take
  minutes); `subscribeEvents` is exempt (long-lived SSE).
- New `bridge.killSession(sessionId)` API mirrors the shutdown teardown
  for a single session — used by POST /session orphan-reap above and
  exposed for future routes that need targeted cleanup.

Stale + cosmetic:
- Bridge map header comment said "no path that removes a session...
  when its child process crashes between requests" — out of date since
  the `channel.exited` cleanup landed in an earlier audit round.
  Rewritten to describe the actual cleanup chain.
- runQwenServe now wraps IPv6 hostname literals in brackets when
  building the URL (`http://[::1]:4170` not `http://::1:4170`). The
  bracket-stripping logic on `listenHostname` already handled
  `app.listen()` correctly; this fixes the printed/copy-paste URL.
- Dead `mode: ServeMode` variable in serve.ts removed (the runQwenServe
  call hardcodes `mode: 'http-bridge'`); the warning condition is now
  inlined.

Test plan:
- `vitest run` cli/serve: 120/120 + 49/49 (httpAcpBridge) pass
- `vitest run` sdk-typescript daemon: 42/42 pass
- tsc --build packages/cli packages/sdk-typescript: clean
- ESLint: clean

* chore(lint): allow mime/lite in import/no-internal-modules (#3803)

`packages/core/src/utils/fileUtils.ts` and its test import `mime/lite`,
which is mime@4's documented public sub-export (a smaller bundle that
omits the legacy mime DB) — not an internal module. The rule has been
flagging these on PR CI runs even though main's CI happens to pass
(likely stale-cache vs fresh-install timing). Add `mime/lite` to the
allowlist so lint is consistent across main and PR runs.

* fix(cli,sdk): close 14 review threads — env whitelist + races + Windows tests + structured errors (#3803)

Critical correctness:
- registerPending now resolves orphaned permissions as cancelled when
  the entry has been torn down between the agent's `requestPermission`
  decision and the bridge handler firing. Previously the permission
  would hang the agent forever (killSession's pendingPermissionIds
  iteration didn't include the just-orphaned id, shutdown's clear()
  dropped it without resolving).
- Workspace key now goes through `realpathSync.native` (with a
  resolved-but-uncanonicalized fallback for non-existent paths) so
  case-insensitive filesystems (macOS APFS, Windows NTFS) don't
  silently degrade `sessionScope: 'single'` into "one session per
  spelling". Matches how `config.ts` / `settings.ts` / `sandbox.ts`
  resolve workspace paths.
- killChild gets a hard 10s deadline after SIGKILL so a child stuck
  in uninterruptible sleep (D-state, e.g. NFS read on a dead server)
  can't block `bridge.shutdown()`'s `Promise.all` forever.
  `SHUTDOWN_FORCE_CLOSE_MS` in `runQwenServe` only covers
  `server.close()` — without this hard kill, daemon shutdown hangs.
- setSessionModel now races the agent call against
  `transportClosedReject` and wraps in `withTimeout`, matching what
  `sendPrompt` and `applyModelServiceId` already do. Without the
  race, a wedged child blocks `POST /session/:id/model` forever.
  Also publishes a `model_switch_failed` SSE event on rejection so
  passive subscribers see the failure (matches `applyModelServiceId`).
- shutdown() now awaits `inFlightSpawns` so the late-shutdown re-check
  inside `doSpawn` finishes its half-built channel teardown before
  `bridge.shutdown()` resolves. Without the await, `runQwenServe.close()`
  returns and `process.exit(0)` is queued before the orphan tears
  itself down, surfacing a stderr error AFTER the daemon claimed
  graceful shutdown.
- sendPrompt re-checks `signal.aborted` immediately after
  `addEventListener` so a microsecond-window synchronous abort that
  fires between the early-exit check and listener registration still
  triggers the agent `cancel` notification.

Security:
- `defaultSpawnChannelFactory` now passes an *allowlisted* environment
  to the spawned `qwen --acp` child instead of `{ ...process.env }`
  with `QWEN_SERVER_TOKEN` deleted. The agent runs user-supplied
  prompts with shell-tool access; anything in its env (OPENAI/
  ANTHROPIC/DASHSCOPE keys, AWS/GCP credentials, DB passwords,
  OAuth tokens) is reachable by prompt injection. Allowlist covers
  HOME/PATH/USER/LOGNAME/LANG/LC_*/TMPDIR/TEMP/TMP/NODE_PATH plus
  Windows essentials (SYSTEMROOT/USERPROFILE/APPDATA/...). The
  explicit `delete childEnv['QWEN_SERVER_TOKEN']` stays as
  defense-in-depth — anyone grepping for the token name finds the
  scrub explicitly named.

Observability:
- 5xx responses now carry structured `code` and `data` fields when
  the underlying error has them (JSON-RPC errors from the ACP SDK
  forward as `{code, message, data}`). Without this, every distinct
  failure (quota / rate-limit / auth / crash) collapses to the same
  opaque "Internal error" string at the client.
- 5xx errors log to stderr (via `writeStderrLine`, not `console.error`,
  to keep the no-console lint rule happy). Stop-gap until structured
  access/error logging lands.
- Eviction frame on EventBus subscriber overflow no longer consumes
  a `nextId` slot. The synthetic frame burning a sequence id meant
  healthy subscribers saw gaps (3 → 5) that the resume ring couldn't
  back-fill — silently broke the `BridgeEvent.id` "monotonic per-
  session" contract. `BridgeEvent.id` is now optional on the type
  to make the absence honest. Same pattern as `stream_error`.

Cross-platform:
- httpAcpBridge.test.ts now derives expected paths via
  `path.resolve(path.sep, 'work', 'a')` (factored out as `WS_A`/
  `WS_B`/`SESS_A` constants) instead of hardcoded POSIX literals
  like `/work/a`. On Windows `path.resolve('/work/a')` returns
  `D:\work\a` so the literal expectation drifted; the bridge's
  internal canonicalization to that form was correct, the tests
  were wrong. Fixes 3 Windows CI matrices that have been red since
  the PR opened.

Compatibility:
- `DaemonClient.fetchWithTimeout` now feature-detects
  `AbortSignal.timeout` and `AbortSignal.any` with polyfills, so the
  SDK actually works on its declared minimum runtime (Node >=18.0.0).
  `AbortSignal.any` was added in Node 20.3 — without the fallback
  every non-streaming call throws on Node 18.0–20.2.

Documentation:
- `cancelSession` now explicitly documents that cancel only affects
  the currently active prompt; previously POST'd queued prompts
  continue to execute. Multi-prompt queueing is a daemon-introduced
  behavior (not in ACP spec), so the contract for queued prompts is
  ours to define and was previously implicit.
- Removed misleading "still reliable on Node 20" comment around
  `req.aborted` and switched the orphan-cleanup signal to
  `res.writable` — the right "can we still send a response to this
  client?" check (`req.destroyed` is too eager: clients close their
  writable end after sending the body even though they're still
  listening for the response).

* fix(cli): close 3 more review threads — case-insensitive Host, trim token, sliceLineRange (#3803)

- hostAllowlist now lowercases the Host header before comparison. Per
  RFC 7230 §5.4 Host is case-insensitive; Express normalizes header
  *names* but not values, so a Docker proxy that capitalizes the
  hostname (`Host: Localhost:4170`) or a platform with case-preserving
  DNS (`HOST.docker.internal`) was getting 403 with an exact-match
  compare.
- `runQwenServe` now `.trim()`s the token from both `--token` and
  `QWEN_SERVER_TOKEN`. Common gotcha: `export QWEN_SERVER_TOKEN=$(cat
  token.txt)` keeps the file's trailing `\n`, so the hashed-then-
  compared token never matches what well-behaved clients send. Every
  request returns the generic 401, no breadcrumb pointing at the
  whitespace, operators chase ghosts.
- `BridgeClient.readTextFile` partial-read path no longer
  `content.split('\n')`s the entire file. New `sliceLineRange` walks
  `indexOf('\n', …)` forward only to the end-of-range boundary and
  returns a single substring. For a 100 MB file with `{line: 1,
  limit: 2}` this avoids a ~100 MB `String[]` allocation.

* fix(sdk): close 2 #3889 polyfill leaks — abortTimeout + composeAbortSignals

Two copilot review threads on commit 11567a43c's AbortSignal
polyfill code:

- `abortTimeout` polyfill scheduled `setTimeout` but never cleared
  it. Even after the awaited fetch resolved, the pending timer kept
  the event loop alive until it fired; on a heavily-used client the
  per-call timers accumulated. Fix: `.unref()` the handle (so a
  fast-resolving fetch doesn't pin the loop) AND clear it on the
  controller's `abort` event (so the composed-signal-aborted-first
  path also drops the timer). Defensive `typeof handle.unref` so
  the polyfill works in any runtime that returns a non-NodeJS
  Timeout shape.

- `composeAbortSignals` polyfill added an `abort` listener to every
  input signal but never removed them. Long-lived caller signals
  (e.g. a session-scope cancel signal that lives for the whole SDK
  client) accumulated one listener per SDK call — slow leak that
  retained the closure + controller of every prior call. Fix:
  track per-input cleanups in an array, detach all on the first
  abort (whichever input fires) AND on the composed controller's
  own abort path (defense-in-depth for callers that abort the
  composed signal independently).

Both leaks only fire on the polyfill path — runtimes with native
`AbortSignal.timeout` / `AbortSignal.any` (Node 20.3+) take the
early-return path and bypass the leak surface entirely.

29/29 DaemonClient.test.ts pass; tsc + ESLint clean.

* fix(cli,sdk): close 13 deepseek review threads — error handling + race + log noise (#3803)

Correctness:
- `applyModelServiceId` now races against `transportClosedReject` like
  `setSessionModel` and `sendPrompt` already do, so a child crash
  during attach-with-different-model fails fast instead of waiting
  the full 10s `withTimeout`.
- `POST /session` disconnect guard now handles the `attached` case:
  previously `!res.writable && session.attached` fell through to
  `res.json` and threw EPIPE through Express's default handler.
- `POST /session/:id/prompt` now drops `AbortError` silently. When
  the HTTP client closes mid-prompt the bridge re-throws as
  `AbortError`; routing it through `sendBridgeError` produced a
  noisy 500 + stderr stack trace that under active use generated
  dozens of misleading log lines per second.
- `POST /session/:id/prompt` now rejects empty arrays (`[]`) and
  non-object elements with a 400 instead of letting the ACP SDK
  surface 500s on degenerate input.
- `readTextFile` rejects `limit <= 0` up front (previously
  `sliceLineRange` hit the `end < start` path with surprising
  results).
- `inFlightSpawns` tracks ALL `doSpawn` promises now, not just
  single-scope ones. Under `thread` scope, `shutdown()` previously
  resolved before in-flight spawns finished their child cleanup,
  surfacing stderr noise after the daemon claimed graceful shutdown.
  Use a unique `${workspaceKey}#${randomUUID()}` key per thread-scope
  spawn so simultaneous spawns don't collide.

Shutdown ordering:
- The 5s force timer is now armed AFTER `bridge.shutdown()` resolves,
  so it only races `server.close()` (the listener drain) — not the
  bridge's own 10s `KILL_HARD_DEADLINE_MS` child cleanup. The earlier
  arrangement could resolve this promise while the bridge was still
  killing children, orphaning anything not yet at the deadline.

Express error handling:
- Final 4-arg error middleware catches `express.json()`'s
  `SyntaxError` on malformed bodies and returns JSON `400` instead of
  Express's default HTML page (which trips SDK clients that expect a
  JSON body on every response).
- SSE `res.on('error')` handler now logs the error before cleanup, so
  operators get a breadcrumb for flaky-network triage instead of
  silent disconnect.

Performance:
- `ALLOWED_CHILD_ENV_KEYS` moved to module scope so the 22-element
  Set is allocated once at load instead of rebuilt on every
  `defaultSpawnChannelFactory` call. (Renamed from `ALLOWED_ENV_KEYS`
  for clarity.)

Documentation:
- `canonicalizeWorkspace` now explicitly notes the cross-module
  contract with `config.ts`/`settings.ts`/`sandbox.ts`. A shared
  utility was considered but deferred — the call sites use slightly
  different fallback policies and Stage 2 in-process collapses the
  bridge into core, removing the bridge-side path resolution
  entirely.

Tests:
- Two new DaemonClient tests exercise `fetchWithTimeout`'s
  AbortSignal.timeout / composeAbortSignals polyfill paths against
  a never-resolving fetch promise. Previously every test used
  `recordingFetch` with synchronous resolution, so those polyfills
  shipped untested — a logic error there would only surface when a
  real daemon became unresponsive.

* docs(serve): close §08 Stage 1 doc gap — user guide + protocol reference + DaemonClient example (#3803)

Stage 1 of issue #3803 §08 budgeted "Documentation + examples + e2e tests"
as the closing 1d task. The e2e tests landed (22 cases under
integration-tests/cli/), the docs did not. After merge, anyone who
discovers `qwen serve` via `qwen --help` had nowhere in-repo to read
about it — the only complete description lived on the PR page itself.

This commit fills that gap with three complementary docs and a README
mention:

- `docs/users/qwen-serve.md` — operator-facing quickstart: 5-step curl
  walkthrough (start → /health → /capabilities → /session → /prompt →
  /events), CLI flag table, default-deployment threat model summary,
  and a pointer to the orchestrator-shaped multi-session future.
- `docs/developers/qwen-serve-protocol.md` — full HTTP protocol
  reference: per-route request/response shapes, auth contract, error
  envelope, SSE frame format and event-type table, Last-Event-ID
  reconnect semantics, environment variables, source layout.
- `docs/developers/examples/daemon-client-quickstart.md` — TypeScript
  end-to-end snippet with the SDK's DaemonClient: capabilities probe,
  spawn-or-attach, subscribe-before-prompt event handling, reconnect
  via Last-Event-ID, first-responder permission voting, shared-session
  collaboration between two clients, auth, cancel.
- README.md — "Daemon mode" added to the 5-way usage list + a short
  section under Usage with three doc links.
- `docs/users/_meta.ts` and `docs/developers/_meta.ts` — sidebar
  entries for the new pages.

No code changes; no test changes.

* docs(serve): close 8 deepseek doc-review findings (#3803)

Inline doc review on the Stage 1 doc set caught real issues:

- `qwen-serve-protocol.md`: `session_died` (and `client_evicted`,
  `stream_error`) now explicitly marked as terminal — SSE stream
  closes after the frame; subscribers should reconnect via POST
  /session for `session_died`.
- `qwen-serve-protocol.md`: documented coalesced spawn failure path
  — when the underlying spawn fails, all coalesced callers receive
  the same error and the in-flight slot is cleared so a follow-up
  call can retry.
- `qwen-serve-protocol.md`: clarified the `modelServiceId` (back-end
  provider, picked at session create) vs `modelId` (model within an
  already-bound service, picked via POST /session/:id/model)
  distinction, and explained why `/capabilities`'s `modelServices`
  array is always `[]` in Stage 1.
- `qwen-serve-protocol.md`: typo "Re-races" → "Races" on the model
  switch description.
- `qwen-serve.md`: reordered quickstart so SSE subscribe (now step 4)
  comes before the prompt POST (now step 5). Previously, step 4's
  blocking prompt resolved before step 5's `curl -N` was open, so
  readers following the steps verbatim never saw a streaming event.
  Also expanded the event-types paragraph to call out which frames
  are terminal.
- `daemon-client-quickstart.md`: closed a TOCTOU race in the example
  — `sendPrompt` fired before the SSE handshake completed, so
  fast-starting agents could emit events into the ring before the
  iterator was actually pulling. Pass `lastEventId: 0` so the
  daemon's replay buffer covers the gap; comment in the example
  explains the rationale.
- README.md: "Loopback bind has no auth" → "no auth by default"
  (since the user can opt into bearer auth on loopback by setting
  `QWEN_SERVER_TOKEN`).

* fix(cli,sdk,docs): close 21 review threads — env regression + races + doc accuracy (#3803)

CRITICAL regression fix:
- Child env scrub flipped from allowlist back to denylist (just
  QWEN_SERVER_TOKEN). The earlier allowlist was overzealous: it
  dropped OPENAI_API_KEY / ANTHROPIC_API_KEY / GEMINI_API_KEY /
  QWEN_* / DASHSCOPE_API_KEY / custom modelProviders[].envKey, all of
  which the agent legitimately needs to authenticate to the LLM.
  Daemon-mode users with env-only auth would start the daemon, attach
  a session, then watch every prompt fail with auth errors. Threat-
  model rationale documented at the call site: prompt-injected shell
  tools can already read ~/.bashrc, ~/.aws/credentials, etc., so env
  passthrough isn't the security boundary; the user-as-trust-root is.
  QWEN_SERVER_TOKEN stays scrubbed to prevent agent → its own daemon
  escalation.

Other code fixes:
- doSpawn no longer tears down the session when create-time model
  switch fails. The session is still operational on the agent's
  default model; tearing it down left the caller with a 500 and no
  sessionId to retry against. The model_switch_failed SSE event is
  the visible signal; caller can retry via POST /session/:id/model
  once they have the sessionId.
- doSpawn now uses applyModelServiceId for the create-time model
  switch (was raw conn.unstable_setSessionModel + withTimeout). The
  helper races against transportClosedReject too, so a child crash
  during model switch fails fast instead of consuming the full init
  timeout.
- sendPrompt's abort handler now calls cancelPendingForSession
  before the ACP cancel notification (matching cancelSession). A
  client disconnecting mid-permission was leaving the agent stuck
  waiting on a vote that no SSE subscriber would ever cast.
- shutdown() and killSession() now publish a terminal `session_died`
  SSE event before closing the bus. Previously the channel.exited
  handler's "byId.get(...) !== entry" guard short-circuited (entry
  already removed), so SSE subscribers couldn't tell daemon shutdown
  from a transient network error.
- Express error middleware now special-cases `status: 413`
  (EntityTooLargeError from body-parser when a request exceeds the
  10 MB JSON limit) and returns a JSON 413 instead of a misleading
  500.
- /health is now registered BEFORE bearerAuth middleware, so
  liveness probes work without credentials when the daemon was
  started with --token. CORS deny + Host allowlist still apply.
- SSE writes serialize through a per-connection chain so the
  heartbeat interval can no longer interleave with the main event-
  write loop. Two concurrent res.write calls would otherwise bypass
  the backpressure guard and could interleave bytes between SSE
  frames on the wire.

SDK:
- abortTimeout / composeAbortSignals exported for direct unit
  testing. The existing test claimed to cover the polyfill paths via
  subscribeEvents, but subscribeEvents calls _fetch directly (not
  fetchWithTimeout), so composeAbortSignals never ran in the test.
  New tests exercise the helpers directly across native + polyfill
  runtimes.

Doc accuracy fixes:
- daemon-client-quickstart.md: createOrAttachSession({ cwd: ... })
  → ({ workspaceCwd: ... }) (SDK type), client.sendPrompt → prompt,
  client.cancelSession → cancel. The example wouldn't typecheck.
- qwen-serve.md: "binds one workspace" claim removed — a single
  daemon hosts sessions for any cwd the caller passes; the
  per-instance constraint is per-user / scale, not per-workspace.
  Auth verification example switched from /health to /capabilities
  (since /health is now exempt from bearer auth).
- qwen-serve-protocol.md: env var was QWEN_E2E_LLM, real var is
  SKIP_LLM_TESTS (inverted polarity). Streaming test count was 4,
  actually 3. Added Stage 1 limitation notes for "no DELETE
  /session" and "no permission timeout". Added client-side
  ring-buffer gap detection guidance for Last-Event-ID reconnect.

Test updates:
- httpAcpBridge.test.ts: rewrote two tests for the new
  doSpawn-on-model-switch-fail contract (publish event, keep
  session). Updated shutdown-closes-subscriptions test to expect
  the new terminal `session_died` frame.
- server.test.ts: switched bearer-auth rejection probes from
  /health to /capabilities (since /health is now exempt). Added a
  test that locks /health's exemption.

* docs(serve): close 2 last review threads — prompt timeout limitation note (#3803)

A05Yk (deepseek): document that `POST /session/:id/prompt` has no
server-side timeout. The bridge only races against the agent child
exiting + the caller's HTTP-disconnect AbortSignal; a wedged-but-alive
agent blocks the per-session FIFO. Long-running prompts are
legitimate (deep research / large-codebase analysis) so a default
deadline is deliberately not set; Stage 2 will expose a configurable
opt-in. Callers should set their own client-side timeout and
disconnect / POST /session/:id/cancel on expiry.

AyoUy (copilot): same env-allowlist concern as A09HB — already
addressed by the allowlist→denylist revert in the previous commit
(e74aa9919). No additional code change needed; the resolve here just
acks that the upstream fix covers it.

* fix(serve): close 3 copilot review threads — SSE envelope shape + integration test ordering (#3803)

A8uSe / A8uSt — the SSE frame examples in qwen-serve.md and
qwen-serve-protocol.md showed `data:` containing only the inner ACP
payload (e.g. `{"sessionUpdate": ...}`). The daemon actually emits
the full event envelope — `{id?, v, type, data, originatorClientId?}`
— JSON-stringified on a single line. Readers copying the curl output
and writing parsers against the documented shape would extract garbage
or fail JSON-shape validation. Both docs now show the real envelope
and call out the SSE-level `id:` / `event:` lines as EventSource
convenience that duplicates fields already inside the JSON envelope.

A8uSz — integration `qwen serve — bearer auth` tests probed `/health`
for 401 assertions, but `/health` is now intentionally registered
BEFORE the bearer middleware (per the A8dZT fix in the previous
commit) so liveness probes work without credentials. Switched probes
to `/capabilities`, plus added a `/health exempt` test that locks the
exemption so a future middleware ordering change can't silently break
liveness probes.

Also: integration `bad modelServiceId tears down half-init session`
asserted the OLD doSpawn-on-model-switch-fail behavior (throw + clear
maps). Per #3889 review A05Ym the new behavior keeps the session
operational on the agent's default model and surfaces the failure
via the `model_switch_failed` SSE event. Test renamed to
`bad modelServiceId keeps the session alive on the default model`
and rewritten to assert the new contract.

* fix(serve): close 3 copilot review threads — sync write throw, polyfill name, blockquote (#3803)

A800o (server.ts:360): `res.write(chunk, cb)` callback isn't documented
to receive an error argument in Node — errors come on the `'error'`
event, which the surrounding code already wires up. The dead `(err) =>
if (err) reject(err)` branch was misleading. The real concern was
that `res.write()` can throw synchronously when the socket is already
destroyed (typical EPIPE shape), and the throw escaped the promise
executor. Wrapped the `res.write` call in try/catch so that surfaces
as a rejection on the returned promise instead of an unhandled
exception.

A8008 (DaemonClient.ts:375): `abortTimeout` polyfill called
`new DOMException('TimeoutError')`, which sets the *message* to
"TimeoutError" and leaves `name` at its default ("Error"). Native
`AbortSignal.timeout()` aborts with `name === 'TimeoutError'` (per
WHATWG), so callers doing `if (err.name === 'TimeoutError')` to
distinguish timeout from user-abort would see the polyfill behave
differently from the native runtime. Constructor signature is
`new DOMException(message, name)` — fixed both args.

A801J (qwen-serve-protocol.md:254): blockquote was broken — one
line in the middle of the multi-line `>` block was missing the `>`
prefix, which dropped the rest of the list out of the quote and
rendered awkwardly. Added the missing `>`.

* fix(cli,sdk): close 8 review threads — DoS cap + SDK plumbing + cleanup (#3803)

Critical:
- A9UEi — `EventBus` had no subscriber cap and evicted subscribers
  lingered in the `subs` Set until the consumer drove `next()`. An
  attacker opening thousands of SSE connections to one session would
  amplify each `publish()` (O(N) over subs) into a CPU/memory DoS,
  with each evicted-but-stalled connection's `BoundedAsyncQueue`
  pinned in memory forever. Two fixes: per-bus subscriber cap of 64
  (refuses new subs at the limit by returning an empty iterable),
  AND `subs.delete(sub)` immediately when a subscriber is evicted so
  subsequent publishes don't pay the dead-sub iteration cost. Also
  set `server.maxConnections = 256` on the listener to bound socket
  descriptors against connections that never finish their headers.

SDK:
- A9UEv — `prompt()` now accepts an optional `AbortSignal`. Caller
  cancellation forwards through the underlying TCP close, which the
  daemon already translates into an ACP `cancel` notification. The
  bridge's `sendPrompt(sessionId, req, signal)` always supported it;
  only the SDK surface was missing the parameter.
- A9UEn — `subscribeEvents` now applies `fetchTimeoutMs` to the
  CONNECT phase only (request → headers received). The SSE body
  itself stays uncapped (it's long-lived by design), but a daemon
  that's TCP-open but never returns headers no longer blocks
  callers indefinitely. Implementation: a setTimeout-driven
  AbortController composed with the caller's signal, cleared in
  `finally` once `_fetch` returns.
- A9UEr — `respondToPermission` now drains the response body via
  `res.body?.cancel()` on both 200 and 404. undici keeps the
  underlying socket pinned waiting for an unconsumed body; long-
  running clients with frequent permission votes would exhaust
  the connection pool.

Cleanup:
- A9UNF — `MAX_BUF_BYTES` renamed to `MAX_BUF_CHARS` (the guard
  checks `buf.length`, which is UTF-16 code units, not bytes). The
  cap's job is "stop runaway non-SSE bodies", not exact accounting,
  so the proxy is intentional — but the name now matches the unit.
  Error message updated.
- A9UNb / A9UNp — both integration tests' boot-timeout `setTimeout`
  is now stored and `clearTimeout`'d on success and on early exit.
  Without the clear the un-cancelled 10s timer outlived the spawn
  promise and could keep the vitest event loop alive past the test,
  manifesting as intermittent timeouts on slow CI.

A9UEy was already addressed by the prior commit's `status === 413`
branch in the Express error middleware (body-parser sets both
`status: 413` and `type: 'entity.too.large'` on body-too-large
errors); resolve only.

* fix(cli,test): close 2 copilot review threads — case-insensitive bearer + Windows skip (#3803)

A9sCe (auth.ts:88): bearer scheme parsing was case-sensitive
(`parts[0] !== 'Bearer'`). Per RFC 7235 §2.1 / RFC 7230 §3.2.6 the
auth scheme token is case-insensitive — `Bearer` / `bearer` /
`BEARER` are all valid, and conformant clients may send any. The
old code returned 401 on those. Switched to a regex-based split that
also tolerates runs of whitespace between scheme and credentials,
then `.toLowerCase()`s the scheme before comparing. The token value
itself stays case-sensitive (it's user-defined opaque material).

A9sCw (qwen-serve-streaming.test.ts): the streaming integration
suite shells out to `pgrep` / `kill -KILL` to simulate child-process
crashes for the `SIGKILL → session_died` test. Those binaries are
POSIX-only — on Windows runners the suite would fail even when
`SKIP_LLM_TESTS` is unset. Added `process.platform === 'win32'` to
the SKIP gate. A Windows-equivalent (`taskkill /F /PID …`) needs
different scaffolding; deferred.

* fix(cli,sdk,docs): close 6 review threads — CodeQL regex, body cancel, env doc (#3803)

A90nk (auth.ts:93): CodeQL flagged the new bearer-scheme regex
`^(\S+)\s+(.+)$` as a polynomial-regex risk on user-controlled
input — `\s+` and `.+` overlap on whitespace-heavy adversarial
headers (the alert example: `'!\t' + '\t'.repeat(N)`). Replaced
with a hand-rolled split (`indexOf(' ')` + manual whitespace
skip) so there's no backtracking. Behavior unchanged: scheme is
still case-insensitive, runs of whitespace between scheme and
credentials still tolerated, scrubs `header.charCodeAt() === 0x20`
explicitly so we don't accidentally consume tab/newline as scheme
separator.

A90oi / A96Q8 (qwen-serve.md:117): the threat-model bullet still
claimed the spawned child runs with an "allowlisted environment"
(HOME / PATH / USER / LOGNAME / LANG / etc), but the prior commit
flipped the implementation to a denylist (only `QWEN_SERVER_TOKEN`
scrubbed) so the agent could authenticate to LLM providers. Doc
now matches code: explicit pass-through with a one-key scrub, plus
the threat-model rationale (user-as-trust-root, env passthrough is
not the boundary).

A90ou (qwen-serve-protocol.md:300): `stream_error` example showed
the inner ACP-style payload `{"error":"<message>"}` instead of the
full envelope `{v, type, data:{error}}` that other SSE-frame
examples in the same doc already use. Updated to match.

A96RL (DaemonClient.ts:352): `subscribeEvents` threw on a 200 with
the wrong content-type without consuming the response body first.
On undici-backed `fetch` an unconsumed body keeps the underlying
socket pinned waiting for the consumer; long-running clients
hitting this path repeatedly would exhaust the connection pool.
Same `await res.body?.cancel()` pattern as `respondToPermission`.

A96RR (server.ts:167): prompt-element validation accepted any
non-null object, but `typeof [] === 'object'`, so `prompt: [[]]`
slipped past with a confusing 500 from the ACP SDK layer downstream.
Added `!Array.isArray(item)` so the 400 actually catches array
elements.

* fix(cli,sdk,docs): close 10 review threads — DoS observability + race + tests (#3803)

Code:
- A-Ur8 (httpAcpBridge.ts:1319): SCRUBBED_CHILD_ENV_KEYS gets a
  prominent WARNING that the denylist-only design is correct ONLY
  because the agent has unrestricted shell-tool access. Any future
  sandbox-locked variant MUST switch back to allowlist or expand
  the denylist to cover provider/CI/cloud secret prefixes.

- A-XfH (auth.ts:60): Host allowlist now accepts the no-port form
  (`localhost`, `127.0.0.1`, `[::1]`, `host.docker.internal`) when
  the bind port is 80. Per RFC 7230 §5.4 clients may legitimately
  omit the port suffix when it matches the URI scheme default.

- A-UsJ (httpAcpBridge.ts:564): unify model-switch failure handling.
  The create-session path swallows the error to keep the session
  alive on its default model; the attach path now does the same
  (was: throwing a 500 with no sessionId, denying the caller any
  way to recover). Both paths surface failure via the
  `model_switch_failed` SSE event.

- A-UsN (httpAcpBridge.ts:621): extracted the lazy-init
  `transportClosedReject` pattern into `getTransportClosedReject`
  helper. Three call sites (`applyModelServiceId`, `sendPrompt`,
  `setSessionModel`) collapsed to one, single-listener invariant
  documented at one place.

- A-UsH (eventBus.ts:194): subscriber-cap rejection is now
  observable. EventBus.subscribe throws a typed
  `SubscriberLimitExceededError` (was: silent empty iterable). SSE
  route catches it, logs to stderr, and emits an SSE-shaped
  `stream_error` terminal frame so the rejected client sees a
  readable failure rather than a closed-with-no-frames stream.

- A-UsO (server.ts:72): `/health` is now exempted from bearerAuth
  ONLY on loopback binds. On non-loopback the route is registered
  AFTER bearerAuth so probes must carry the token — otherwise an
  unauthenticated caller could probe arbitrary IP:port to confirm
  a `qwen serve` exists. Doc updated.

Tests added:
- A-UsP: new test sends an 11 MB body to verify the 413 path in
  the Express error middleware returns the actionable
  "Request body too large" JSON instead of a generic 500.
- A-UsQ: new test for `DaemonClient.prompt(sessionId, req, signal)`
  AbortSignal forwarding through to fetch.
- A-UsS: two new tests for `subscribeEvents` connect-timeout
  (never-resolving fetch aborts; fast-resolving fetch clears the
  timer so it doesn't leak as a dangling handle).
- A-UsU: new test for `sendPrompt` abort path resolving pending
  permissions as cancelled — the bug being regressed: an HTTP
  client disconnecting mid-permission would leave the agent stuck
  waiting on a vote that no SSE subscriber would ever cast.

Test contract updates:
- `publishes model_switch_failed and surfaces the error when the
  agent rejects` rewritten for the new attach-path swallow contract:
  attach now returns the existing session with `attached: true`
  and the `model_switch_failed` event is the visible failure
  signal instead of a thrown error.

* fix(serve): add missing v field on subscriber-limit stream_error frame (#3803)

`tsc --build` (which CI runs as part of the lint job) caught what
`tsc --noEmit` (the local typecheck script) missed: the new
`stream_error` frame in `server.ts:344` was constructed without the
`v` field, but `OmitId<BridgeEvent>` requires it. Local typecheck
in the previous commit was clean; the build's stricter project
graph reported `error TS2345` and broke both Lint and Test
(Ubuntu) jobs.

Set `v: 1` to match the existing `stream_error` construction in
the SSE iterator-throw path in the same file.

* docs(users): close 1 copilot review thread — GitHub canonical casing in nav (#3803)

A_U2e: nav label "Github Actions" was inconsistent with the
canonical "GitHub" casing used elsewhere in the repo (skills,
README, etc.). Rename to "GitHub Actions" for consistent branding.

Pre-existing entry in `docs/users/_meta.ts` adjacent to the
`'qwen-serve'` line this PR added — flagged in the diff context.

* fix(serve): close 4 deepseek review threads — closed-bus race + per-session stderr + entry override (#3803)

BBb9H (correctness): `BridgeClient.requestPermission` could orphan
a pending permission if the bus closed between `registerPending`
and `entry.events.publish` (the shutdown path closes per-session
buses BEFORE awaiting `channel.kill()`, so the agent can still
issue `requestPermission` in that window). Pending was registered
in the daemon-wide map but `publish()` returned `undefined`
(closed bus) → no SSE subscriber ever saw the request → no client
voted → agent's `requestPermission` hung forever, blocking the
daemon's `Promise.all` over child kills. Now: check publish's
return; if `undefined`, roll back the pending via a new
`rollbackPending` callback that resolves it as `cancelled`.

BBb8e (Critical observability): child stderr was `'inherit'` —
all sessions' stderr interleaved on the daemon's stderr stream
unattributed. Switched to `'pipe'` and forward each line with a
`[serve pid=<n> cwd=<dir>]` prefix; operators can now
`grep pid=12345` to pull one session's trace cleanly. Updated
the now-stale doc comment that claimed inherit was current.

BBb8- (deployability): `process.argv[1]` is brittle — fails on
non-`qwen` launchers (bundled binaries, npx wrappers, `node -e`,
`tsx`, container images that relocate the script). Added
`QWEN_CLI_ENTRY` env override as the higher-priority resolution
path. Improved the failure message to suggest the env var as
the actionable fix.

BBb82 (documented limitation): `withTimeout` REJECTS but doesn't
ABORT the underlying ACP op. For `unstable_setSessionModel` this
means a timed-out caller perceives failure while the agent may
eventually complete the switch — drift between caller's perceived
model and agent's actual model + contradictory SSE events.
Documented as a Stage 1 limitation in the `withTimeout` JSDoc;
acceptable because (1) ACP doesn't expose a cancel signal for
`unstable_setSessionModel` yet so we couldn't abort even if we
wanted to, (2) model switches complete in milliseconds in
practice — a timeout means genuinely wedged, not just slow.
Stage 2 will add abort plumbing once ACP exposes the hook.

* ci(noop): re-trigger workflow for f8509dde5 (#3803)

* fix(cli,sdk): close 8 review threads — sse abort + queue drain mode + perf + doc engine drift (#3803)

Correctness:
- BCcd6 (sse.ts:80): trailing flush at EOF used `splitFrames(buf)`
  which returned `[buf]` — a multi-byte split that completed
  multiple frame separators in the final `decoder.decode()` would
  merge the frames into one parse and silently drop events.
  Switched the EOF flush to `consumeFrames()` (same walker the
  main loop uses), then attempt one more `parseFrame` on any
  trailing fragment. Removed the now-unused `splitFrames` helper.

- BCybH (sse.ts:67): `parseSseStream` only checked `signal.aborted`
  before each `reader.read()`, leaving the generator parked inside
  a pending `read()` if the upstream went idle right when the
  caller aborted — contradicting the docstring's "AbortSignal
  cleanup is prompt" claim. Added a one-shot abort listener that
  calls `reader.cancel()` (cleared in `finally`), so abort
  reliably terminates even on a stalled stream.

- BCce_ / BCycT (eventBus.ts:391/253): subscribe documented "abort
  closes the iterator promptly" but `BoundedAsyncQueue.next()`
  drained any items already in `buf` before honoring `closed`.
  Aborted SSE subscribers could keep yielding hundreds of queued
  events to a closed socket. Added a `close({drain: false})` mode
  that truncates `buf` immediately, used by the abort path; the
  default drain-on-close behavior is preserved for the eviction
  path (which needs the synthetic `client_evicted` terminal frame
  to reach the consumer before the iterator unwinds).

Performance:
- BCcfe (auth.ts:72): `hostAllowlist` was allocating a fresh `Set`
  + 4 interpolated strings on every request. Cache once per
  resolved port (relevant because tests bind to ephemeral 0 and
  the port is only known after `listen()`); SSE heartbeats and
  high-frequency probes now skip the allocation.

- BCcgJ (DaemonClient.ts:137): `fetchWithTimeout` used
  `AbortSignal.timeout()` — the timer fires regardless of whether
  the fetch resolved early. On a fast-resolving request with the
  default 30s timeout, the pending timer hangs around. Switched
  to `AbortController` + `setTimeout` + explicit `clearTimeout`
  in `finally`, so each timer is released the moment its fetch
  settles. Also `.unref()`s the timer so it doesn't pin the event
  loop on its own.

Doc accuracy:
- BCyc0 (DaemonClient.ts:468): the `abortTimeout` /
  `composeAbortSignals` JSDoc claimed Node 18-20.2 polyfill
  compatibility, but `engines.node` is `>=22.0.0` now. Reframed
  as a generic feature-detect for non-Node runtimes (browsers /
  edge workers) so future maintainers don't reason about the
  wrong floor.
- BCydi (server.ts:368): "Always present in Node >= 20" → "on the
  supported Node versions (engines.node >=22)".

CodeQL alert #207 (httpAcpBridge.ts:1342, `js/path-injection` on
`cwd: workspaceCwd`) is the renumbered version of the
already-accepted #201 — same trust-model rationale documented at
the call site, same need for maintainer UI dismiss / config
exclusion.

* feat(serve): close 3 chiga0 audit items — ringSize 4000, --max-sessions, /health?deep=1 (#3803)

Three "30-minute" items from chiga0's external architecture audit
(2026-05-11). All actionable within Stage 1 scope; remaining items
in chiga0's review (SaaS positioning, multi-token to Stage 1.5,
acp-bridge package extraction, reference orchestrator) are larger
scoping decisions deferred to Stage 1.5/2.

DEFAULT_RING_SIZE 1000 → 4000 (Risk 4):
- A single long turn can emit hundreds of frames (test plan reports
  13 for a SHORT turn, real workloads can be 10× that). 1000 was
  exhausted by a moderate turn before a 5s reconnect window
  finished. 4000 gives ~30× headroom over a typical busy turn at
  the cost of a few hundred KB RAM/session. Updated user + protocol
  docs and the daemon-client-quickstart example.

--max-sessions <n> (default 20) (Rec 3):
- New `ServeOptions.maxSessions` + matching `BridgeOptions`. Bridge
  throws `SessionLimitExceededError` when `byId.size +
  inFlightSpawns.size >= max` BEFORE issuing a fresh spawn. Attaches
  to existing sessions (single scope) bypass the cap so an idle
  daemon's reconnects keep working at-capacity. `0` disables.
  Default of 20 sized below the design's N≈50 cliff (per-session
  ~30–50 MB RSS + FD pressure). HTTP route maps to 503 with
  `Retry-After: 5` and `code: session_limit_exceeded`. Tests cover:
  cap rejection under thread scope, attach-not-counted under single
  scope, `0` disables. Documented in CLI flags table + protocol
  Common-error section.

/health?deep=1 (Risk 3):
- Default `/health` stays cheap (no bridge access). With `?deep=1`
  the response includes `sessions` and `pendingPermissions` from
  the bridge — touches state so a wedged bridge surfaces as 503
  `{status: "degraded"}` instead of "200 ok" on a zombie daemon
  (the `k8s rolling deploy will see healthy` failure mode chiga0
  flagged). Loopback-vs-non-loopback bearer-exempt logic from the
  earlier A8dZT fix is preserved via a shared handler. Tests cover:
  cheap default, deep response shape, throwing-getter → 503.

* fix(serve,sdk,docs): close 9 review threads — req.on('close') prompt-cancel bug + doc + types (#3803)

Critical correctness:
- BQAnZ (server.ts:225): `POST /session/:id/prompt` wired
  cancellation to `req.on('close')` — but Node's `IncomingMessage`
  fires that event when the request body has been fully consumed,
  even when the client is still listening for the response. Result:
  ordinary prompt calls were getting cancelled the moment their
  upload finished, returning `{stopReason: "cancelled"}` instead
  of completing. Switched to `res.on('close')` guarded by
  `!res.writableEnded` (the documented "client gave up before we
  could send the response" pattern, same as the POST /session
  disconnect-detection from earlier in the PR).

Already addressed earlier — resolve as ack:
- BQAna (httpAcpBridge.ts:767): no global session cap. Already
  shipped in commit 66ffd7cc6 — `--max-sessions` flag + bridge
  enforces with `SessionLimitExceededError` mapped to 503; both
  in-flight spawns and live sessions count against the cap.

Doc fixes:
- BDAOf (DaemonClient.ts:49): `fetchTimeoutMs` JSDoc said it
  applies to "every non-streaming method including prompt", but
  `prompt()` actually bypasses fetchWithTimeout (model+tool turns
  are minutes-scale, can't be 30s-capped). Doc now lists the
  short-lived methods explicitly and notes prompt's exemption.
- BDAPY (qwen-serve-protocol.md:283): blockquote was broken — the
  `POST /session/:id/cancel` line was missing the leading `>` and
  a stray "- POST /session/:id/cancel." rendered orphaned outside
  the quote. Reformatted as a single coherent quote.

Reviewer-tooling resilience:
- BQAnf / BQAng (integration-tests/...:325/185): added explicit
  `DaemonSessionSummary` type to two `.find` / `.every` callbacks.
  Local typecheck infers the type fine via the SDK's source
  declarations; the reviewer's environment resolves
  `@qwen-code/sdk` against a possibly-stale `dist/index.d.ts`
  (per `integration-tests/tsconfig.json` `paths` mapping) and the
  `s` parameter widens to `any`. Annotation makes both envs happy.

Reviewer-only artifacts (no code action):
- BQAnb / BQAnc (integration-tests/...:26/30) — same SDK-dist
  staleness; the imports are correct and resolve fine when
  `packages/sdk-typescript` has been built.
- BQAni (server.test.ts:8 supertest module not found) — Node 20
  setup blocker the reviewer noted; resolves cleanly under
  Node >=22 (our declared engines floor) with `npm install`.

* fix(serve,sdk,test): close 7 review threads — fetchTimeoutMs negative + bridge-error context + perm scope contract (#3803)

Real fixes:
- BQPRo (DaemonClient.ts:136): `fetchTimeoutMs` accepted any number,
  including negatives that would slip past the `Number.isFinite`
  check inside `fetchWithTimeout` and fire `setTimeout(-1)` →
  immediate abort, killing every request before it could complete.
  Coerce non-positive / non-finite to 0 (the documented disable
  sentinel) at the constructor so call-site math stays simple.
- BQLdO (server.ts:725): `sendBridgeError` now accepts a `ctx`
  arg `{ route, sessionId }` folded into the stderr log line.
  Bare `ECONNRESET` / `ENOMEM` traces are no longer unattributable
  on a busy daemon — operators see `qwen serve: bridge error
  (POST /session/:id/prompt session=abc-123): ...`. All five route
  call sites pass context.
- BQI-6 (qwen-serve-streaming.test.ts:123): `sseFrames` test helper
  forwards `opts.signal` into `parseSseStream` so post-connect
  abort terminates iteration immediately (the parser's own abort-
  -wired-to-reader.cancel landed earlier; this just plumbs through
  the test harness).

Doc / contract:
- BQNqL / BQNqM (httpAcpBridge.ts:692, server.ts:199):
  `cancelPendingForSession` cancelling all session permissions on
  client disconnect is intentional under the per-session FIFO + ACP
  spec — permissions are issued inline DURING an active prompt,
  the agent awaits them, so the only outstanding permissions at
  any moment belong to the prompt being cancelled. Cross-client
  caveat (B's vote 404s when A disconnects mid-A's-prompt) is
  the right behavior — a vote on a cancelled-prompt's permission
  wouldn't drive the agent forward. Documented the scope contract
  + multi-client caveat in `cancelPendingForSession` JSDoc.

Already addressed (resolve as ack):
- BQI-c (qwen-serve-protocol.md): blockquote was already
  reformatted in the previous round (`POST /session/:id/cancel`
  now sits inline on a single quoted line); copilot reviewed an
  older commit.
- BQI-v (DaemonClient.ts): `fetchTimeoutMs` JSDoc was already
  updated last round to explicitly note `prompt()` is excluded;
  copilot reviewed the older shape.

* fix(serve,test,docs): close 6 review threads — TEST_CLI_PATH + Stage 2 markers + SSE phantom-conn warning (#3803)

Real fix:
- BQpu6 / BQpvW (integration-tests/cli/...): both qwen-serve test
  files hardcoded `../../packages/cli/dist/index.js`, while the
  rest of the integration suite reads `process.env.TEST_CLI_PATH`
  (set by `globalSetup.ts` to the root `dist/cli.js` bundle). The
  difference made our tests sensitive to which build step
  (`build` vs `bundle`) ran last. Now read `TEST_CLI_PATH` first,
  fall back to per-package dist for direct vitest invocations
  that bypass globalSetup.

Operator-facing doc:
- BQsOD (server.ts:497 KNOWN GAP): added an operator warning to
  `docs/users/qwen-serve.md`'s threat-model section about phantom
  SSE connections behind NATs that swallow TCP RSTs (kernel
  keepalive ~2h Linux default → can accumulate to the 256-conn
  ceiling on `--hostname 0.0.0.0` deployments). Stage 2 will add
  application-level idle deadline; until then operators on such
  networks may want to lower `server.keepAliveTimeout` via reverse
  proxy.

Stage 2 maintenance markers (no code change, just visible TODOs):
- BQsOA (httpAcpBridge.ts:1247): added `FIXME(stage-2)` on the
  sync `realpathSync.native` call so the Stage 2 in-process
  refactor doesn't ship without removing this event-loop-blocking
  syscall.
- BQsOB (server.ts:243): added a SECURITY NOTE on the
  `...(body as object)` passthrough explaining the spec-defined
  `_meta` forwarding contract + the rule that an explicit pick is
  required if any new bridge field starts being trusted by name.
  Pattern repeats on cancel/model — note covers all four sites.
- BQsOF (httpAcpBridge.ts:1041): `FIXME(stage-2)` noting that
  `setSessionModel` reuses `initTimeoutMs` (default 10s) for the
  in-flight model swap — conceptually distinct from cold-start
  init, currently sharing only by coincidence; Stage 2 should
  split into `modelSwitchTimeoutMs` and remove the no-abort
  `withTimeout` race-condition once ACP exposes a cancel signal
  for `unstable_setSessionModel`.

* fix(serve): close 4 review threads — unhandled rejection + maxSessions plumbing + 2 docs

- httpAcpBridge.sendPrompt: attach .catch(() => {}) to the
  abort-listener cleanup chain. The chain is `racedPromise.finally
  (...)` and we never await it; if `racedPromise` rejects, the
  finally returns a rejected promise that surfaces as an unhandled
  rejection (Node's default behavior on unhandled rejection is
  process termination). The route's own catch handles the original
  rejection — only the cleanup chain needs the swallow.
- httpAcpBridge.sendPrompt: FIXME(stage-2) for absolute prompt
  deadline — buggy agent ignoring cancel + alive channel = slow
  prompt-promise leak.
- server.createServeApp: forward opts.maxSessions when constructing
  the default bridge. Direct callers (tests, embeds) were silently
  falling back to DEFAULT_MAX_SESSIONS (20); only the runQwenServe
  path piped the option through.
- docs/users/qwen-serve.md: clarify Host allowlist is loopback-only;
  non-loopback binds rely on bearer + operator-managed front proxy.

* docs(sdk): close 1 review thread — sse.ts MAX_BUF_CHARS docstring lead-line said "bytes"

Doc lead-line claimed "Hard cap on accumulated unread bytes" while the
implementation enforces the cap via `buf.length` (UTF-16 code units),
which the rest of the same docstring already correctly explained.
Fix the lead-line so a reader skimming the first sentence isn't
misled.

The runtime error message and constant name (MAX_BUF_CHARS) already
say "code units" — only the docstring lead-line needed alignment.

* fix(serve,sdk): close 5 review threads — disconnect/attach race + 3 spec fixes + 1 doc

- httpAcpBridge: add SessionEntry.attachCount + new
  killSession({requireZeroAttaches:true}) opt to fix the BQ9tV race.
  When client A spawned (attached:false) but disconnected mid-spawn,
  A's disconnect-reaper (server.ts) could tear down a session that
  client B had just attached to. spawnOrAttach now bumps attachCount
  on each attached:true return, and killSession with the new opt
  bails when attachCount > 0. The check + the eager byId/byWorkspace
  deletes both run in killSession's synchronous prefix, so the
  guard is atomic across the await boundary.
- server.ts disconnect-reap path now passes requireZeroAttaches:true.
- loopbackBinds.ts: lowercase the operator-supplied hostname before
  Set lookup so --hostname Localhost / LOCALHOST aren't forced to
  require a token. Aligns boot-time detection with the runtime
  Host-header check (auth.ts already lowercases).
- auth.ts bearer parsing: accept HTAB (0x09) in addition to SP
  between scheme and credentials per RFC 7230 §3.2.6 BWS.
- sdk sse.ts parseFrame: guard against `null` / primitive JSON
  parses so the AsyncGenerator<DaemonEvent> contract isn't
  violated by a misbehaving proxy emitting `data: null`. Daemon
  itself never emits these — defense-in-depth only.
- docs/developers/qwen-serve-protocol.md: document the
  modelServiceId-rejection-on-fresh-session corner case + tell
  subscribers to pass Last-Event-ID:0 to replay the spawn-time
  model_switch_failed event from the ring.
- 3 new unit tests: BQ9tV positive + negative race paths,
  BQ9ze parseFrame null guard.

* fix(serve): close 4 review threads — 2 critical (NaN cap, stderr buffer) + IPv6 zone-id + deep doc

- httpAcpBridge maxSessions normalization (BRApy [Critical] gpt-5.5):
  NaN / negative values previously fell through `!Number.isFinite(...)`
  to `Infinity`, silently disabling the daemon's session cap (fail-OPEN
  on a typo). Now throw TypeError on NaN / negative; explicit 0 and
  Infinity remain valid "unlimited" sentinels.
- httpAcpBridge stderr line buffer (BRAp3 [Critical] gpt-5.5): the
  per-spawn `buf` accumulating stderr until `\n` had no length cap; a
  child that wrote a huge line or never emitted a newline could grow
  daemon memory unboundedly per session. Cap at 64 KiB per line and
  force-flush with a `[truncated]` marker — keeps the prefix-attributed
  log line, bounds memory, no content drop.
- runQwenServe.formatHostForUrl (BQ-6V copilot): RFC 6874 requires
  `%` in IPv6 zone IDs (e.g. `fe80::1%lo0`) to be percent-encoded as
  `%25` in URLs. Now encode on the raw-IPv6 path; already-bracketed
  input is the operator's responsibility.
- /health?deep=1 (BQ-6F copilot): the 503 path is unreachable for
  the real bridge (counter getters are simple Map-size accessors that
  don't throw). Reframed in code + protocol doc as INFORMATIONAL
  observability ("capacity dashboards, not real liveness"); keep the
  try/catch as defense-in-depth for custom bridge impls.
- 2 new unit tests: BRApy NaN/negative throws + 0/Infinity ok;
  BQ92B Localhost case-insensitive boot.

* fix(sdk): close 1 review thread — sse parseFrame tighter shape guard (BREsR followup to BQ9ze)

The previous parseFrame guard only rejected null/primitive JSON; arrays
and shape-incomplete objects still cast through to DaemonEvent. Tighten
to require: non-null non-array object with v === 1 and type: string.
Now the generator's static AsyncGenerator<DaemonEvent> type is a
genuine runtime guarantee instead of a structural hope.

Daemon never emits malformed frames (formatSseFrame always serializes
{v: 1, type: string, ...}); guard remains defense-in-depth against
misbehaving proxies / alternate implementations. Existing test fixtures
already conform to the shape so no other tests needed updating.

* fix(sdk): close 1 review thread — fetchWithTimeout keeps timer alive through body consumption (BRN1o)

Pre-fix: `fetchWithTimeout` cleared the timer in `finally` the moment
the underlying `fetch` resolved. But `fetch` resolves at headers, not
at body completion. A daemon or proxy that sent headers and then
stalled mid-body left `await res.json()` (and `failOnError`'s
`res.text()`) without any deadline — calls to `health()`, `capabilities()`,
`createOrAttachSession()`, `listWorkspaceSessions()`, `setSessionModel()`,
`cancel()`, `respondToPermission()` could hang indefinitely past
`fetchTimeoutMs`.

Refactor `fetchWithTimeout<T>` to take an optional `consume(res)`
callback whose execution is included in the timer scope. The composed
abort signal still flows through to fetch's body stream, so an
in-progress `res.json()` rejects cleanly when the timer fires. All
JSON-returning routes updated to pass the body-read code as the
callback. SSE (subscribeEvents) + prompt are unchanged: they bypass
fetchWithTimeout intentionally (long-lived).

Regression test: response with a never-emitting body that errors via
the composed AbortSignal — pre-fix would hang for 5s+, post-fix
rejects within ~80ms (configured timeout).

* fix(serve,sdk): close 8 review threads — coalescing race fix + --max-connections + 5 docs/cleanups

- httpAcpBridge spawnOrAttach (BRSCi [Critical] DeepSeek): the BQ9tV
  attachCount fix was incomplete for the in-flight coalescing path.
  When two callers await the same doSpawn and the second has a
  modelServiceId, the attach-bump landed AFTER an extra await for
  applyModelServiceId — leaving a microtask window in which A's
  killSession sync-prefix would still see attachCount==0 and reap a
  session B was about to receive. Move the bump to the very first
  sync step after `await inFlight` (and same in the direct-attach
  branch) so the bump-before-killSession ordering holds even when
  the model-switch yields. Test added for the coalescing-race path.
- commands/serve + serve/types + runQwenServe (BRQQb): add
  `--max-connections` flag (default 256), wired through ServeOptions
  and `server.maxConnections`. Operators with high-concurrency
  deployments can now tune the listener-level cap without waiting
  for Stage 2.
- commands/serve (BRQQZ): wrap `new Promise<never>(() => {})` in a
  named `blockForever()` helper so a future maintainer doesn't read
  the bare expression as a never-resolving-promise bug.
- auth.ts (BRQQd): rewrite the comment about HTAB BWS — clarify
  that the scheme→credentials separator is `1*SP` per RFC 9110
  §11.6.2, and HTAB is only accepted in the BWS *after* the SP.
  `Bearer\t<token>` (pure HTAB) is intentionally rejected.
- types.ts + qwen-serve-protocol.md (BRQQf): document
  `modelServices: []` is always empty in Stage 1 so SDK consumers
  don't build off it.
- qwen-serve.md (BRQQl + BRQQm): add operator note about subscribing
  to /events BEFORE posting modelServiceId on attach (otherwise the
  model_switch_failed event is missed). Document the four-layer load
  cap stack near --max-sessions so operators can size the related
  knobs together.
- sdk index (BRSCv): drop the historical `Daemon`-prefixed type
  aliases (`DaemonPromptRequest` / `DaemonSubscribeOptions`) for
  consistency with the other un-prefixed daemon-type exports. SDK is
  Stage-1-experimental with no shipping consumers.

* fix(sdk): close 1 review thread — sse parseFrame must not drop frames whose first line is a comment/retry (BRgq-)

Per the EventSource spec, comment lines (`:` prefix) and `retry:` are
line-level fields, not frame-level. The previous early return at the
top of `parseFrame` dropped the entire frame when its first line was
a comment or retry directive — meaning an intermediary that prepends
`: keep-alive` or `retry: 5000` to every frame would cause the
embedded `data:` payload to be silently lost.

Removed the `startsWith` guard. The line-level `data:` collection
loop already produces an empty `dataLines` array for pure-comment /
pure-retry frames, so the existing `if (dataLines.length === 0)
return undefined` branch still skips them — without dropping real
events that just happen to be preceded by a comment line.

Existing test still pins the standalone-comment / standalone-retry
behavior; new test pins the leading-comment + data-line case.

* docs(sdk): close 1 review thread — sse MAX_BUF_CHARS comment was overpromising byte-equivalence (BRker)

The previous wording suggested "one code unit ≈ one byte" for
mostly-ASCII content, then qualified it with mixed BMP / supplementary
caveats. Reviewer flagged that JS string.length isn't a reliable byte
proxy in either direction — engine string representation (V8 Latin-1
path vs UTF-16) makes the actual memory cost vary in ways the comment
didn't capture cleanly.

Rewrote to state plainly: cap measures code units, not bytes; intent
is "stop runaway non-SSE bodies", not exact memory accounting;
byte-precise bounds belong at a front proxy. Threshold and code
unchanged — only the comment.

* fix(serve): close 7 review threads — atomic write, read-size cap, force-exit on 2nd signal, doc fixes

- httpAcpBridge.writeTextFile (BSA0D): atomic write-then-rename via
  `<path>.<pid>.<ts>.tmp` + `fs.rename`. Closes the SIGKILL-mid-write
  truncation hole. Tmp file lives in the target's directory so the
  rename can't cross filesystem boundaries; cleaned up on rename
  failure.
- httpAcpBridge.readTextFile (BSA0E): `fs.stat` pre-check rejects
  files past 100 MiB so a `{ line: 1, limit: 10 }` against a 500 MB
  log doesn't allocate 500 MB of RSS just to return 10 lines.
- runQwenServe SIGINT/SIGTERM (BSA0K): second signal during drain
  forces `process.exit(1)` with a stderr message instead of silently
  no-oping. Standard daemon behavior — `^C^C` works.
- commands/serve --hostname help text (BRqFe): now mentions the full
  loopback set (127.0.0.1, localhost, ::1, [::1]) so IPv6 users
  aren't misled into thinking ::1 needs a token.
- runQwenServe boot-refusal error (BRqFy): same correction — error
  message now lists all loopback aliases the operator can rebind to.
- httpAcpBridge withTimeout doc (BSA0C): explicit Stage 2 follow-up
  marker for the modelSwitchTimedOut / model_switch_late_success
  observability gap (already a known limitation).
- server.errorPayload (BSA0G): documented the multi-tenant info-leak
  trade-off (Stage 1 single-user/small-team trust model accepts
  verbatim ACP error data) and pointed to a Stage 2 --redact-errors
  follow-up.
- 2 new tests: writeTextFile leaves no tmp turd; readTextFile
  rejects 200 MiB sparse file via the size cap.

* fix(sdk): close 1 review thread — sse parseFrame must validate optional `id` (BSP1-)

The previous shape guard only validated `v === 1` and `type: string`,
leaving `DaemonEvent.id: number | undefined` unchecked. A misbehaving
proxy emitting `data: {"id":"1","v":1,"type":"x",...}` would survive
the cast and break consumer resume logic — Last-Event-ID resume does
numeric comparisons against the monotonic counter, and a string id
silently corrupts that math.

Reject the frame entirely when `id` is present but not a finite safe
integer (`Number.isSafeInteger`). Negative integers and missing-id
both still pass; the daemon never emits negative ids in practice but
the guard's responsibility is the type-cast contract, not the
daemon's id-allocation policy.

New test covers: string id, float id, > MAX_SAFE_INTEGER id (all
rejected); negative-id, no-id, plain integer (all pass).

* docs(serve): Stage 1.5 markers from chiga0 follow-up architecture review (#3889 c4427773706)

chiga0's follow-up review explicitly states "None of the findings
here block Stage 1. That holds." All 6 findings are Stage 1.5
convergence work for when downstream consumers attach. None require
code changes for this PR.

Adding inline FIXME(stage-1.5) markers at the natural pivot points
so the future refactor has clear breadcrumbs back to the audit
comment, instead of Stage 1.5 implementers having to re-discover
the convergence story:

- types.ts STAGE1_FEATURES → finding 5 (capability registry +
  extMethod HTTP route).
- eventBus.ts EventBus class → finding 2 (lift to
  packages/event-bus, multi-consumer subscribe).
- httpAcpBridge.ts BridgeClient.requestPermission → finding 3
  (PermissionMediator + policy plugin point; closes prior chiga0
  Risk 2 too).
- httpAcpBridge.ts BridgeOptions → findings 1 + 4 (split into
  AcpChannel + Transport packages; thread FileSystemService through
  BridgeOptions).

No behavior change. Each marker links to the audit comment for
traceability.

* docs(serve): tighten Stage 1 scope framing + durability + Stage 1.5 must-haves (#3889 c4427875644)

chiga0's third review walks three downstream-consumer scenarios (IM
bot, mobile companion, IDE extension) against Stage 1's runtime
guarantees. The bottom-line concern is framing: the PR body promises
"real workloads" but the protocol surface is sized for demo /
single-user / never-crashes. Reviewer offers two paths — tighten the
framing or add 7 must-haves to Stage 1.5. Author classifies all 10
must-haves as Stage 1.5/2, none as Stage 1 changes.

In-scope action for this PR (doc-only, no behavior change):

- `docs/users/qwen-serve.md` "Status" block: explicit scope-honesty
  note — Stage 1 is sized for prototyping clients + local
  single-user/small-team. Production-grade multi-client / mobile /
  flaky-network workloads need Stage 1.5+ guarantees.
- New "Durability model" section spelling out sessions-are-ephemeral
  (closes must-have 10): no resume on child crash / daemon restart,
  ring-overflow on long disconnects, writeTextFile atomic across
  crash but not across restart.
- New "Stage 1.5+ runtime guarantees" section listing the 10
  must-haves (blockers 1-3, reliability 4-7, ergonomics 8-10) with a
  link back to the audit comment for traceability.
- `httpAcpBridge.ts` BridgeOptions.sessionScope: FIXME(stage-1.5)
  marker referencing must-have 1 (per-request override), since this
  is the most prominent client-facing lock-in risk.

No code behavior changes — this is roadmap commentary surfaced into
the artifacts where downstream integrators will look (user docs +
code pivot points).

* fix(serve): close 2 correctness findings from tanzhenxin review

Two bugs surfaced in the CHANGES_REQUESTED review:

Issue 1 — `--max-connections 0` silently bricks the daemon on Node 22:
- Docs say "Set to 0 to disable" and the code did
  `server.maxConnections = opts.maxConnections ?? 256`, but on Node
  22.15.0 setting `server.maxConnections = 0` makes the listener
  refuse EVERY connection (every fetch → SocketError other side
  closed). The operator following the documented disable path got a
  daemon that boots cleanly, logs "listening on …", and then
  silently rejects health/session/SSE.
- Fix: treat 0 / Infinity / non-finite as "leave the property
  unset" (Node's default = unlimited at this layer). Reviewer
  verified the Node 22 quirk; verified locally that 100 still binds
  the cap, 0 and Infinity now both accept connections.

Issue 2 — Orphan agent child when both coalesced spawnOrAttach callers
disconnect:
- The BQ9tV `attachCount` race guard is monotonic. Once B's
  `spawnOrAttach` bumps it (synchronously, before the route handler
  can see `!res.writable`), the spawn-owner A's disconnect-reaper
  sees attachCount > 0 and skips the reap — permanently. If B then
  also disconnects, neither A nor B's route handler does anything,
  and the agent child stays alive with no client knowing the id.
- Fix: add `bridge.detachClient(sessionId)` that decrements
  attachCount and reaps iff (attachCount == 0 && subscriberCount ==
  0). Server's `POST /session` handler calls it on the
  `!res.writable && session.attached === true` branch (symmetric to
  the existing spawn-owner-disconnect reap).
- Subscriber-count check prevents reaping when a third client C is
  already on SSE — `detachClient` only fires when the session has
  no live consumers at all.

2 new tests for issue 1 (max-connections 0 + Infinity still accept
connections; 100 still binds as supplied). 2 new tests for issue 2
(detach reaps when alone; detach preserves when SSE subscriber
exists). fakeBridge updated with the new method.

* fix(serve): close 3 review threads — maxConnections NaN/negative validation + doc fix + close-contract honesty

- runQwenServe maxConnections validation (BUF9-): NaN / negative
  values previously slipped through `cap > 0 && Number.isFinite(cap)`
  to "leave unset = unlimited", silently fail-OPEN on a CLI typo and
  weakening the DoS / FD-exhaustion guard. Now throw TypeError
  upfront (before `app.listen()`) so a malformed cap fails the
  `runQwenServe` promise instead of escaping as an uncaught
  exception from the listen callback.
- types.ts maxConnections doc (BUb7C): comment said "Node treats 0
  as unlimited" but the runtime fix treats 0 as a sentinel and
  leaves `server.maxConnections` unset (Node 22 quirk). Updated to
  match.
- runQwenServe close()/force-timeout (BUb7h): the 100ms eager
  `setTimeout(() => finish(), 100)` after `closeAllConnections()`
  resolved the close promise WITHOUT waiting for `server.close()`'s
  callback — breaking the "fully closed" contract. Now: force-close
  just accelerates `server.close` by killing sockets; we still wait
  on the close callback. A secondary 2s deadline handles the
  pathological "server.close never fires" case (kernel-stuck
  socket) with a logged warning, so shutdown stays bounded.

* docs(serve): close 8 review threads — code-comment clarity + 3 new Stage 1 known gaps

8 threads in a single Claude Opus 4.7 review pass — 4 duplicate
existing chiga0 finding FIXME markers, 1 code-comment clarity, 3
real new doc-worthy Stage 1 known gaps.

Code clarity (BUy4U):
- The shutdown re-check at doSpawn (`if (shuttingDown) { kill; throw }`)
  is the LOAD-BEARING correctness contract, not a band-aid as the
  reviewer framed it. Updated comment to explain: shutdown() runs
  tear-down in parallel with awaiting `inFlightSpawns` (faster
  fan-out); the re-check catches spawns whose `newSession` returns
  AFTER the flag flipped. The alternative — await all inflight to
  settle BEFORE snapshotting byId — is cleaner to reason about but
  serializes shutdown by up to `initTimeoutMs` (10s) before any live
  session starts tearing down. Documented the trade-off.

New Stage 1 known gaps in docs/users/qwen-serve.md threat model:
- BUy4H (permission auth daemon-global): cross-session vote risk
  acceptable under Stage 1 single-user / small-team trust model;
  Stage 1.5 will scope to `POST /session/:id/permission/:requestId`
  + session-scoped pending map + per-client identity (closes
  must-have #3 from the downstream review).
- BUy4L (10 MB body limit on /prompt): multimodal content past
  10 MB hits a cliff; workaround via path reference; Stage 1.5
  accepts chunked encoding.
- BUy4e (CORS deny blocks `packages/webui`): document explicit
  deployment options (Electron/Tauri shell, same-origin reverse
  proxy); Stage 1.5 adds `--allow-origin <pattern>` for opt-in
  named frontends.

Already-marked duplicates (BUy4O, BUy4P, BUy4X, BUy4b) — covered by
existing `FIXME(stage-1.5, chiga0 finding N)` / `FIXME(stage-2)`
markers from prior rounds.

* fix(serve): close 1 review thread — catch --hostname localhost:4170 typo upfront (BU-sh)

The previous code path for unbracketed `host:port` typos went:
1. Loopback check fails (`localhost:4170` doesn't match the
   loopback set after lowercase normalization).
2. Throw "Refusing to bind localhost:4170:0 without a bearer token"
   — misleading because the operator's real bug is the colon in the
   hostname, not the missing token.

Alternative path if a token IS supplied: hostname flows through to
`formatHostForUrl` which sees the `:` and treats as IPv6, wrapping
to `[localhost:4170]:port` in the printed URL. Then `app.listen()`
fails with ENOTFOUND. Triple-unhelpful failure mode.

Fix: catch the typo BEFORE the loopback/token check. Unbracketed
input with exactly one `:` is unambiguously the host:port shape —
raw IPv6 literals always have ≥2 colons (shortest is `::`), and
bracketed IPv6 is handled by its own form check below.

Error message suggests the corrected form
(`--hostname localhost --port 4170`).

* docs(serve): two new Stage 1 scope boundaries (option A + option iii) from LaZzyMan reviews

LaZzyMan's two-part review surfaced two structural framing concerns
distinct from the chiga0 roadmap items. Neither requires code changes
in this PR — they want explicit scope honesty in the user docs:

1. TUI super-client framing (option A from the review): TUI UI is
   strictly larger than the wire protocol. The ~15 Ink dialogs and
   `local-jsx` slash commands are local-only; mutating commands like
   `/approval-mode`, `/memory`, `/mcp`, `/agents`, `/tools`, `/auth`,
   `/init` change agent behavior but emit no wire event. Documenting
   remote clients as sharing the agent↔user conversation axis only,
   NOT the full TUI session state. Implementers told to re-fetch
   state on reconnect, not rely on incremental events.

2. N parallel sessions cost N× (option iii from the comment): the
   "1 daemon = 1 session" axiom means N concurrent sessions on one
   workspace = N daemons with zero resource sharing. Concrete cost
   table at N=5 (~1.5-2.5 GB RSS, 15 MCP processes, 5× OAuth refresh)
   so users hit the wall with eyes open. Won't-fix on the main-line
   Stage 1/1.5/2 roadmap; alternatives (#3803 §21 Path A/B, in-project
   sidecars) materially change the architecture in ways we won't
   commit to mid-Stage-1. Peer-agent comparison noted (Cursor /
   Continue / Claude Code / OpenCode / Gemini CLI all do
   single-process multi-session).

Both choices are intentionally the less-ambitious option; the
substantive alternative (option B for taxonomy, option i/ii for N:1)
moves to #3803 if real-usage data ever justifies it.

* docs(serve): clarify option-A across Mode 1 (headless) vs Mode 2 (TUI co-host)

Previous wording treated "TUI is a super-client" as universal truth.
But Stage 1's actual shipping configuration is HEADLESS — no TUI
shell runs inside the daemon — and in that mode the slash commands
listed (`/approval-mode`, `/memory`, `/mcp`, `/agents`, `/tools`,
`/auth`, `/init`) simply don't exist. Session state is boot-time-
frozen from settings + disk, with only `/model` mutable via HTTP.

Restructured the section to split the consequences:

- **Mode 1 (headless `qwen serve`, this PR)**: no TUI exists; session
  state is boot-time-frozen + `model_switched` over HTTP; remote
  clients see the FULL session state; no drift possible.
- **Mode 2 (Stage 1.5 `qwen --serve` co-hosted TUI, future)**: TUI
  exists alongside remote clients; TUI slash commands mutate
  session state with no wire events; remote clients see a strict
  subset; drift possible — re-fetch state on reconnect.

The original "super-client" framing applies cleanly only to Mode 2.
Mode 1 has no asymmetry — same option-A choice, different
consequences.

* fix(serve,sdk): close 12 review threads — 6 critical bugs + 6 follow-ups

Six critical correctness fixes from the latest review pass:

- httpAcpBridge.readTextFile (BX8YO): reject non-regular files via
  `stats.isFile()`. Char devices / FIFOs / procfs entries report
  `size: 0` but stream unbounded data; the 100 MiB cap wasn't
  enough. New `describeStatKind()` helper for human-readable error
  message ("named pipe (FIFO)" / "character device" / etc.).
- httpAcpBridge.writeTextFile (BX8Yp + BX9_h): temp filename now
  includes randomUUID + exclusive flag `wx`. PID + Date.now() alone
  collides under concurrent writes within the same ms (sessionScope:
  'thread' or coalesced spawns on same workspace). Exclusive mode
  fails fast on any residual collision instead of silent overwrite.
- httpAcpBridge.writeTextFile (BX8Yw): resolve via `fs.realpath`
  before write-then-rename so symlinks are preserved. Pre-fix
  rename replaced the symlink with a regular file, leaving the
  real target unchanged while the write appeared successful.
  Test added covering both regular targets and symlink targets.
- server.parseLastEventId (BX9_I): log a stderr breadcrumb when
  rejecting a non-empty non-decimal Last-Event-ID header. Pre-fix,
  clients with a malformed resume header silently resumed from 0
  and lost every event buffered during the disconnect with zero
  evidence in logs.
- httpAcpBridge channel.exited (BX9_P): thread {exitCode,
  signalCode} from the spawn factory through `session_died` event
  payload. Operators triaging a crash can now read the cause from
  the SSE frame instead of grepping daemon stderr for the child's
  pid.
- httpAcpBridge spawnOrAttach in-flight coalesce path (BX9_U):
  defensive re-check that `byId.get()` is still defined after
  attachCount++ — if a concurrent kill tore down the entry, throw
  `SessionNotFoundError` instead of returning `attached: true` with
  a zombie sessionId.

Six follow-ups in the same diff:

- httpAcpBridge attachCount comment (BVryk + BWGSL): outdated
  "monotonic, we never decrement" claim — detachClient() now
  decrements. Comment rewritten to state the actual invariant
  ("reflects clients whose response was written or is about to be").
- runQwenServe.close() contract (BV-qW): bridge.shutdown errors are
  now propagated through the close promise (was: silently caught +
  resolved success). onSignal exits 1 instead of 0 when teardown
  fails. Server.close error takes precedence; bridge error is the
  fallback.
- sdk sse parseFrame id guard (BX8Y1): require id >= 1 (was: any
  safe integer including negative). The daemon's Last-Event-ID
  parser only accepts non-negative decimals and EventBus emits ids
  starting at 1; negative ids on the wire diverge from resume math.
  Existing test updated.
- runQwenServe server error listener (BX9_i): swap
  `server.once('error', reject)` for a persistent `server.on('error',
  log)` after listening. Pre-fix, a post-boot error (EMFILE etc.)
  was unhandled and crashed the daemon.

Tests: +2 for BX8YO (FIFO) and BX8Yw (symlink preserve). Test
infrastructure updated for the new `channel.exited` Promise<ExitInfo
| undefined> signature.

* fix(serve,sdk): close 4 more review threads — frame-scan perf + publish contract + AbortError narrowing + cross-module doc

- sse consumeFrames perf (BX9_a): short-circuit the LF path first.
  In the common LF-only case the CRLF scan was traversing the
  entire remaining buffer for nothing; now CRLF is only scanned
  when LF is absent or potentially appears later than a CRLF
  separator (mixed-encoding edge).
- EventBus.publish contract (BX9_p): explicit JSDoc says publish
  NEVER THROWS (closed-bus returns undefined, subscriber-enqueue
  errors caught internally). Historical try/catch wrappers in
  httpAcpBridge.ts are defense-in-depth, not load-bearing; new
  callers should not add them.
- canonicalizeWorkspace doc (BX9_q): elevate the cross-module
  contract from "undocumented" to explicit — config.ts /
  settings.ts / sandbox.ts / this file all canonicalize the same
  way for sessionScope: 'single' re-attach. A divergence silently
  forks sessions per spelling. The Stage 1.5 @qwen-code/acp-bridge
  lift (chiga0 finding 1) is the natural place to extract a shared
  primitive; until then, any change to those modules needs a
  matching change here.
- POST /session/:id/prompt AbortError swallow (BX9_k): narrow the
  swallow to only fire when `abort.signal.aborted` is true. The
  previous blanket `err.name === 'AbortError'` would also silently
  drop AbortErrors raised internally by the bridge (e.g. child
  process aborting mid-prompt), leaving the client with no response
  and no log trace.

* docs(serve): correct N:1 framing — qwen-code's ACP agent natively supports multi-session

Maintainer feedback (verified against the code): the ACP agent in
packages/cli/src/acp-integration/acpAgent.ts:194 has
`private sessions: Map<string, Session>` — one `qwen --acp` child
natively hosts multiple sessions, and yiliang114's VSCode plugin
already uses this pattern. The earlier "qwen-code is the only entry
treating no multi-session resource sharing as a feature" framing
(from the LaZzyMan reply + docs) was wrong.

Stage 1 bridge in this PR doesn't yet leverage that capability — it
spawns one `qwen --acp` child per session for simplicity (easier
debugging, no cross-session interference during initial
stabilization). That's a bridge-side design choice, not an ACP
limitation.

Revised docs/users/qwen-serve.md:

- "N parallel sessions cost N×" section now distinguishes Stage 1
  bridge (current N× cost) from Stage 1.5 bridge (multi-session per
  child, ~1/5th the cost at N=5). Cost table extended with the
  Stage 1.5 column. No more "won't fix on main-line roadmap"
  framing — the fix is a bridge refactor that pairs naturally with
  chiga0 finding 1 (`@qwen-code/acp-bridge` package lift), NOT the
  #3803 §21 Path A/B/C intra-daemon multi-session workstream
  (qwen-code already does that at the agent layer).
- Status block's "Scope honesty" note: removed the implicit
  permanent-cost framing; replaced with explicit "Stage 1 bridge
  pays N×; Stage 1.5 refactor closes the gap" pointer.
- Peer-agent comparison rewritten: qwen-code's *agent* matches
  Cursor / Continue / Claude Code / OpenCode / Gemini CLI on
  single-process multi-session; the bridge is the artifact.

`httpAcpBridge.ts:doSpawn`: inline `FIXME(stage-1.5)` marker
explaining the refactor (keep one child per workspace, call
`connection.newSession()` multiple times on the same channel), with
the link to `acpAgent.ts:194` so a future maintainer doesn't
re-derive the discovery.

* feat(serve): Stage 1 bridge now multiplexes sessions on one qwen --acp child per workspace

Per LaZzyMan / tanzhenxin reviews + maintainer feedback verified
against `packages/cli/src/acp-integration/acpAgent.ts:194` (the
agent's `private sessions: Map<string, Session>`): qwen-code's ACP
agent natively supports multi-session in one child process. The
Stage 1 bridge previously spawned one child per session for
simplicity, paying N× memory / OAuth / file-cache cost. Now refactored
to leverage the agent's existing multi-session capability — one
`qwen --acp` child per workspace, N sessions share it via
`connection.newSession({cwd, mcpServers})`.

Cost at N=5 sessions on same workspace:
- Before: 300-500 MB RSS (5 children), 5× OAuth refresh, 5× file
  cache, 5× CLAUDE.md parse, 5× cold start
- After: 60-100 MB RSS (one child), one OAuth path, shared
  FileReadCache, parsed once, <200ms cold start after first session

Architecture changes:

- New `ChannelInfo` type holds the shared channel + connection +
  BridgeClient + the set of session ids multiplexing on it.
- New `byWorkspaceChannel: Map<workspace, ChannelInfo>` + new
  `inFlightChannelSpawns` coalesce-map for concurrent channel
  creation.
- New `getOrCreateChannel(workspaceKey)` helper: reuse existing
  channel or spawn one (with `initialize` happening exactly once
  per channel, not once per session). Coalesced via
  `inFlightChannelSpawns` so two parallel callers don't both spawn.
- `doSpawn` now calls `getOrCreateChannel` + `connection.newSession`
  separately (was: spawn+initialize+newSession together per session).
- `BridgeClient` updated: `resolveEntry(sessionId?)` dispatches by
  the sessionId ACP carries in each request — one BridgeClient now
  serves all sessions on its channel. `sessionUpdate`,
  `requestPermission`, etc. all pass `params.sessionId`.
- `channel.exited` cleanup moved into `getOrCreateChannel` and now
  tears down ALL sessions on the channel (not one). Each session
  gets its own `session_died` event so SSE subscribers learn the
  bad news on their own stream.
- `killSession` now removes session from `channelInfo.sessionIds`
  and kills the channel ONLY when its sessionIds set drops to zero.
  Other sessions on the same channel keep running.
- `shutdown` tears down channels (the deduplicated set) and awaits
  both inFlightSpawns and inFlightChannelSpawns.

Cross-workspace channel sharing intentionally NOT done — `acpAgent.ts:
601 (this.settings = loadSettings(cwd))` reloads settings on each
newSession call with a different cwd, so different workspaces in
one child would step on each other. One channel per workspace is
the safe scope.

MCP server children stay per-session for now (each session can have
different mcpServers config). Stage 1.5 follow-up: refcount MCP
children by (workspace, config-hash) so identical configs share.

Tests:
- Updated `spawns fresh per call under sessionScope:thread` → now
  expects `handles.length === 1` (channel reused) but
  `sessionCount === 2` (distinct sessions).
- New: `Stage 1.5 multi-session: N sessions on same workspace share
  ONE channel` (5 sessions, 1 factoryCalls).
- New: `Stage 1.5: killSession on one of N sessions does NOT kill
  the shared channel` (kill 2 of 3, channel still alive; kill 3rd,
  channel killed).
- New: `Stage 1.5: channel.exited tears down ALL multiplexed
  sessions` (each gets its own session_died).
- FakeAgent.newSession suffixes call-count so multiple newSession
  calls on the same channel return distinct ids (matches real
  ACP behavior).

Docs:
- `docs/users/qwen-serve.md` N:1 section rewritten — no longer
  "Stage 1 pays N×, Stage 1.5 fixes". Cost table reflects current
  shared-channel architecture; MCP refcount called out as the one
  remaining Stage 1.5 follow-up; "1 daemon = 1 session" framing
  removed from related sections.

* fix(serve,sdk): close 12 review threads — 6 critical bugs + 6 follow-ups

Critical fixes:

- server.ts safeBody() helper (BZ9uv/va/vs/wD + Bd10m + Bd1zz):
  prototype-pollution sanitization at the body-spread boundary.
  `__proto__` / `constructor` / `prototype` keys are stripped and
  the result is an Object.create(null) target. Replaces 5 sites of
  copy-pasted `typeof req.body === 'object'...` preamble + makes
  the `...(body as object)` spread sites safe.
- httpAcpBridge requestPermission (Bd1yh): per-request wall-clock
  deadline (default 5 min, configurable via
  `BridgeOptions.permissionResponseTimeoutMs`). Without this, an
  agent calling requestPermission with no SSE subscriber connected
  would hang the per-session FIFO forever. After deadline, resolve
  as cancelled + log stderr warning.
- httpAcpBridge requestPermission (Bd1z5): per-session pending
  permissions cap (default 64, configurable via
  `BridgeOptions.maxPendingPermissionsPerSession`). New requests
  past the cap resolve as cancelled with stderr warning. Prevents
  a chatty agent from growing pendingPermissions unboundedly.
- runQwenServe onSignal double-signal force-exit (Bd1y6): new
  `bridge.killAllSync()` + `AcpChannel.killSync()` method
  synchronously SIGKILLs every live qwen --acp child BEFORE
  `process.exit(1)`. Previously double-Ctrl+C bypassed the async
  bridge.shutdown() and left children running as orphans.
- server.ts SSE subscriber-limit response (Bd1zJ): 429 +
  Retry-After instead of 200 + stream_error frame. EventSource
  treats 4xx as terminal (no auto-reconnect); the previous
  200+close-stream triggered EventSource's reconnect loop,
  amplifying the load the limit existed to prevent.
- doSpawn ghost sessionId guard (Bd1zc): re-check byId.has() after
  applyModelServiceId(). The model-switch yields and can race
  channel.exited; without this, caller got HTTP 200 with a
  sessionId that 404s on every subsequent request.

Follow-ups in the same diff:

- sse.ts consumeFrames CRLF scan comment (BcRh_): the comment
  claimed the CRLF scan was bounded to `[cursor, lf)`, but Node's
  `indexOf` has no upper bound. Rewrote to describe what the code
  actually does (scan full remainder; only USE the result if it
  falls before `lf`).
- sse.ts SseFramingError export (Bd10T): typed error class for
  framing-level failures so SDK consumers can distinguish "upstream
  isn't SSE" from generic network errors via instanceof check.
  Re-exported from @qwen-code/sdk.
- protocol doc /health auth (Bctum): document the loopback
  exemption — `/health` doesn't require Authorization on loopback
  binds even when a token is configured. Matches `createServeApp`'s
  registration order.

Bd1xz (cross-session permission escalation) acknowledged as
duplicate of BUy4H — already documented as a known Stage 1 gap
under the single-user / small-team trust model; fix is Stage 1.5
must-have #3 (per-client identity + per-session permission scope).

Tests:
- New: prototype-pollution test verifies `__proto__` spread
  doesn't pollute `Object.prototype`.
- All 70 server + 55 bridge + 16 daemon-sse + 60 DaemonClient
  tests pass (203 total).

`killSync()` stubbed on every inline test channel fake; fake
bridge has `killAllSync()`.

* fix(sdk): close 2 review threads — consumeFrames CRLF scan now actually bounded (BeFHR + BeFId)

Previous attempt at the BX9_a perf optimization left the CRLF scan
running over the full remainder of `buf` on every loop iteration
where an LF separator existed — only the LF-not-found fallback path
was actually bounded. Comments claimed the CRLF scan was restricted
to `[cursor, lf)` or "only fires when needed", but Node's
`String.indexOf` doesn't accept an end index.

Bound the scan via a `buf.slice(cursor, lf)` window before
`indexOf` so the assertion is now true: in the common LF-only case
we pay one full scan (for LF) plus one bounded scan over the
matched frame's bytes (small).

* fix(serve): close 3 review threads + Windows test skip — dangling symlink, no-sessionId throw

- httpAcpBridge.writeTextFile BfFvO: dangling-symlink case. `fs.realpath`
  throws ENOENT for a symlink whose target doesn't exist, and the
  blanket catch silently fell back to writing through the symlink
  itself — `rename(tmp, params.path)` then replaced the symlink with
  a regular file, exactly the bug BX8Yw was supposed to fix. Use
  `fs.readlink` to disambiguate "truly non-existent" from "dangling
  symlink"; resolve the dangling target manually and write through
  to it so the symlink stays a symlink. Regression test added.
- httpAcpBridge BridgeClient resolveEntry BfFut: defensive throw on
  no-sessionId ACP call against a multi-session channel. ACP today
  carries sessionId on every per-session call, but if a future
  no-sessionId call lands, silently dropping it on a multi-session
  channel would be invisible.
- httpAcpBridge.test.ts BX8YO Windows skip: hard-skip via
  `process.platform === 'win32'`. Git-Bash etc. ship a `mkfifo`
  binary that degenerates on Windows (creates a regular file or
  silently no-ops), making the assertion match the wrong error
  shape. Linux + macOS coverage is sufficient for a platform-
  agnostic `!stats.isFile()` check.

BfFvW (CRLF scan comment) was already addressed in 0a4146a02 — the
reviewer's diff was against the pre-fix version.

* fix(serve): close 6 review threads — 4 critical bugs + 2 doc updates

Critical fixes:

- httpAcpBridge.doSpawn newSession-failure cleanup (BkwQA): if
  `connection.newSession()` throws on a freshly-created channel
  whose sessionIds set is empty, tear the channel down rather than
  leaking the empty `qwen --acp` child in `byWorkspaceChannel`
  (invisible to `sessionCount` / `maxSessions`). Channels with
  other live sessions still survive — only the truly-empty case
  reaps.
- httpAcpBridge.detachClient + killSession tombstone (BkwQP):
  detachClient no longer reaps live sessions. Scenario: A spawns
  (attached: false, hasn't opened SSE yet), B attaches
  (attachCount: 1), B disconnects → previous code reaped A's
  still-valid session. New behavior:
  * killSession({ requireZeroAttaches: true }) sets
    `entry.spawnOwnerWantedKill = true` when it bails on
    attachCount > 0 (instead of just returning).
  * detachClient ONLY decrements attachCount. It completes the
    deferred reap only when (spawnOwnerWantedKill && attachCount
    === 0 && subscriberCount === 0).
  * Both-disconnected case still works (reap completes via B's
    detachClient seeing the tombstone). Spawn-owner-alive case
    no longer reaps. Existing tanzhenxin-issue-2 test rewritten;
    new test pins the spawn-owner-alive case.
- httpAcpBridge.writeTextFile mode preservation (BkwQW): stat the
  target before writing; if it exists, chmod the tmp file to the
  preserved mode (and chown owner/group — best-effort, EPERM
  ignored for non-root). Previously a 0600 secret/config edit
  would downgrade to umask-default 0644, exposing contents to
  other local users.
- bridge.respondToPermission option-ID validation (BkwQI): new
  `InvalidPermissionOptionError` thrown when the voter's `optionId`
  isn't in the set of options the agent originally offered in the
  `permission_request` event. PendingPermission now carries
  `allowedOptionIds`. Server route catches the error → 400 (vs.
  404 for unknown requestId). Prevents authenticated clients from
  forging hidden outcomes like `ProceedAlways*` when the prompt's
  `hideAlwaysAllow` policy intentionally suppressed them.

Doc fixes:

- httpAcpBridge top-of-file (BkdCg) + types.ts ServeMode (BkdC8):
  rewrite the "each session spawns its own qwen --acp child"
  framing to match the actual Stage 1.5 multi-session-per-channel
  architecture (one child per workspace, sessions multiplex via
  `connection.newSession()`).

* fix(serve): close 4 review threads — close write-mode race + 2 missing tests + 1 doc

- writeTextFile mode-bits race (Blehd): the BkwQW fix preserved
  mode via `chmod` AFTER `fs.writeFile`, leaving a brief window
  where a `0600` secret-edit was readable at the directory's
  umask default (commonly `0644`). Now pass `mode` to writeFile
  directly so the file is CREATED with the preserved mode atomically
  via the `open(O_CREAT, mode)` syscall. The post-write `chmod`
  remains as belt-and-suspenders against a tight operator umask
  (POSIX `mode & ~umask` could drop bits we wanted preserved).
- httpAcpBridge.test.ts: new bridge-level test for the BkwQI
  `InvalidPermissionOptionError` path (Blehk). Forge a vote with
  an `optionId` not in the agent-offered set; assert the throw
  AND that the pending permission survives so a valid vote can
  still resolve it.
- server.test.ts: new route-level test for the BkwQI 400 mapping
  (Blehl). Fake bridge throws `InvalidPermissionOptionError`;
  assert response is 400 with `code: 'invalid_option_id'`,
  `requestId`, and `optionId` in the body.
- commands/serve --http-bridge help text (Bk59I): updated to
  reflect Stage 1.5 multi-session — "one `qwen --acp` child per
  workspace, with multiple sessions multiplexed via the agent's
  native `newSession()`" (was: "per-session child").

* fix(sdk): close 1 review thread — parseSseStream abort path catches body-read rejection (BlqF_)

Some fetch impls (undici on abort) reject the in-flight `reader.read()`
with an AbortError after `reader.cancel()` fires. Pre-fix that
rejection bubbled to the consumer's `for await`, contradicting the
"abort cancels cleanly" public contract — code that called
`controller.abort()` to wind a subscription down saw an unexpected
throw on the next iteration.

Wrap `reader.read()` in try/catch:
- if `signal?.aborted` is true → treat the rejection as clean
  completion (return from the generator)
- otherwise re-throw, so real upstream failures (network drop,
  unexpected close, malformed body) still reach the consumer

Two regression tests pin the guard's scope: signal-aborted
mid-stream returns cleanly with the frames received so far; a
non-abort `streamController.error(...)` still bubbles via `rejects.toThrow`.

* fix(serve): close 1 review thread — eventBus eviction detaches abort listener (BmJT1)

Pre-fix: `publish()`'s eviction path deleted the sub from `this.subs`
but never invoked `dispose()`, leaving the AbortSignal abort-listener
registered in `subscribe()` attached. Because the consumer is by
definition stalled (that's what caused the overflow), `next()` /
`return()` never fire to detach the listener through the iterator
path. Closures over the queue + sub stayed live until the AbortSignal
itself went out of scope.

Under attack (thousands of opened-then-stalled SSE clients), this
amplified into significant heap retention.

Fix: store `dispose` on `InternalSub` and invoke `sub.dispose()` from
the eviction path. The same closure used by the abort listener / the
iterator's `next()`/`return()` cleanup now runs through the
eviction path too — idempotent through `disposed` so a
post-eviction abort or iterator-return is still safe. Regression
test pins the post-eviction abort + publish path producing zero
side effects.

* fix(serve): close 1 review thread — restore double-Ctrl+C force-kill broken by multi-session refactor (BkUyD)

The Bd1y6 design promised a second SIGINT/SIGTERM during graceful
drain synchronously SIGKILLs every live agent child via
`bridge.killAllSync()` before `process.exit(1)` — the operator-
visible "kill it now" path for a wedged child ignoring SIGTERM.

The Stage 1.5 multi-session refactor (commit 6a170ef8) inadvertently
broke this. `shutdown()` snapshots `byWorkspaceChannel` then CLEARS
the map BEFORE awaiting the per-child SIGTERM-grace kills (up to
~10s each). If the operator double-taps mid-window, `killAllSync()`
snapshotted from the now-empty `byWorkspaceChannel.values()` and
silently no-op'd — the for-loop iterated nothing, `process.exit(1)`
fired, and any child still inside its SIGTERM grace window was left
orphaned with dangling pipes. Exactly the scenario the force-kill
path was added to handle.

Fix: introduce a separate `liveChannels: Set<ChannelInfo>` as the
source of truth for "channels with potentially-alive child
processes". Added in `getOrCreateChannel` alongside
`byWorkspaceChannel.set(...)`; removed only when `channel.exited`
fires (the OS-level "really dead" signal). `killAllSync()` now
iterates `liveChannels`, so a mid-shutdown second signal still
sees every still-alive child regardless of where the graceful
drain currently is. Other paths (`killSession` last-session reap,
`channel.exited` crash handler) automatically remove via the same
exit-handler hook.

Regression test:
- Builds two sessions on different workspaces
- Replaces each channel's `kill()` with a never-resolving Promise
  (simulating stuck SIGTERM grace)
- Calls `bridge.shutdown()` to enter mid-drain state
- Yields twice so shutdown's sync prefix runs (clears
  byWorkspaceChannel, starts the never-resolving awaits)
- Calls `bridge.killAllSync()` — pre-fix this saw an empty
  `byWorkspaceChannel` and the spy array would have been empty;
  post-fix both channels' `killSync` is invoked.

(tanzhenxin's other observation — channels-package duplicate ACP
bridge — is the same architectural concern as chiga0 finding 1+5,
already tracked under existing FIXME(stage-1.5) markers. No code
change in this commit for that.)
2026-05-13 14:47:47 +08:00
ChiGao
cadda23782
chore(deps): upgrade ink 6.2.3 → 7.0.2 + bump Node engine to 22 (#3860)
* chore(deps): upgrade ink 6.2.3 -> 7.0.2 + bump Node engine to 22

ink 7 requires Node >=22 and react-reconciler 0.33 with React >=19.2,
so this PR also bumps:

- Node engines (root + cli + core) 20 -> 22
- React/react-dom 19.1 -> 19.2.4 (pinned exact via overrides to keep
  the transitive React graph deduped to a single instance)
- @types/node pinned to 20.19.1 via overrides to avoid an unrelated
  Dirent NonSharedBuffer regression in sessionService tests
- @vitest/eslint-plugin pinned to 1.3.4 to avoid an unrelated lint
  regression introduced by the 1.6.x rule additions
- react-devtools-core 4.28 -> 6.1 (ink 7 peerOptional requires >=6.1.2)
- ink hoisted to root devDeps so workspace-private peer-dep contention
  doesn't push ink-link/spinner/gradient into nested workspace
  installs (which would skip transitive resolution for terminal-link)

Workflow + image + installer alignment:

- .nvmrc 20 -> 22
- Dockerfile node:20-slim -> node:22-slim
- CI test matrix drops 20.x (keeps 22.x + 24.x)
- terminal-bench workflow Node 20 -> 22
- Linux/Windows install scripts upgrade their Node version targets

Documentation alignment:

- README.md badge + prerequisites
- AGENTS.md, CONTRIBUTING.md, docs/users/quickstart.md,
  docs/users/configuration/settings.md, docs/developers/contributing.md,
  docs/developers/sdk-typescript.md, docs/users/extension/extension-releasing.md,
  packages/sdk-typescript/README.md, packages/zed-extension/README.md,
  scripts/installation/INSTALLATION_GUIDE.md

Test gating:

- Two AuthDialog/AskUserQuestionDialog tests that drive <SelectInput>
  through ink-testing-library now race ink 7's frame-throttled input
  delivery and land on the wrong option. The maintainers had already
  marked one of them unreliable (skip on Win32 + CI+Node20). Extend
  that gate to cover all environments until upstream
  ink-testing-library ships an ink-7-compatible release that flushes
  input deterministically. The other test now uses it.skip with the
  same comment. No business code changes.

Verified locally:

- npm run typecheck across all workspaces: clean
- npm run lint (root): clean
- npm run test --workspaces:
    cli  312/312 files, 4918 passed, 9 skipped
    core 266/266 files, 6836 passed, 3 skipped
    webui 6/6, 201 passed
    sdk  40/40, 283 passed, 1 skipped
- npm ls ink: single ink@7.0.2 instance across all peer deps
- single react@19.2.4 instance

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* chore: align Node 22 floor across all shipping artifacts

Reviewer (tanzhenxin) flagged five surfaces where the >=22 engine bump
leaked: SDK package metadata, web-templates engines, /doctor runtime
check, main bundler target, and SDK bundler target. Each was a separate
escape hatch letting Node 18/20 consumers install or run the artifact
on an unsupported runtime.

- packages/sdk-typescript/package.json: engines.node >=18.0.0 -> >=22.0.0
- packages/web-templates/package.json: engines.node >=20 -> >=22
- packages/cli/src/utils/doctorChecks.ts: MIN_NODE_MAJOR 20 -> 22
- esbuild.config.js: target node20 -> node22 (main CLI bundle)
- packages/sdk-typescript/scripts/build.js: target node18 -> node22 (esm + cjs)
- packages/cli/src/utils/doctorChecks.test.ts: rename test label to v22+

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* ci(e2e): bump E2E workflow Node matrix to 22.x

Reviewer (tanzhenxin) flagged that e2e.yml still pinned node-version
20.x while root engines is now >=22, so every E2E run on push would
either fail at npm ci with engine error or silently exercise the bundle
on a runtime that's no longer in ci.yml's test matrix.

The macOS job in the same workflow already reads .nvmrc (which is 22)
so this only updates the Linux matrix.

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(deps): drop root wrap-ansi override so ink 7 gets its declared dep

Reviewer (tanzhenxin) flagged that the root overrides.wrap-ansi: 9.0.2
predates this upgrade and forces every consumer (including ink) to v9,
while ink 7 declares wrap-ansi: ^10.0.0. The lockfile had no nested
install under node_modules/ink/, so ink 7 was running with a transitive
dep one major below its declared minimum.

Dropping the global override lets ink resolve its own wrap-ansi 10
nested install (now visible in the lockfile under
node_modules/ink/node_modules/wrap-ansi), while the cli package's own
direct `wrap-ansi: 9.0.2` dependency keeps the cli code path
(TableRenderer.tsx) on the version it has been tested against. The
nested cliui override is preserved for yargs which still needs v7.

Verified via `npm ls wrap-ansi`:
- ink@7.0.2 -> wrap-ansi@10.0.0 (newly nested)
- @qwen-code/qwen-code -> wrap-ansi@9.0.2 (unchanged)
- yargs/cliui -> wrap-ansi@7.0.0 (unchanged)

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* test(InputPrompt): un-skip placeholder ID reuse after deletion

Reviewer (tanzhenxin) flagged that the new it.skip on the
'should reuse placeholder ID after deletion' test was undisclosed in
the PR description and removed coverage of real product behavior
(freePlaceholderId / bracketed-paste backspace path) without a
TODO(#NNNN) link.

Their argument was sound: the skip rationale pointed at ink 7's input
throttle, but this same file just bumped the wait helper from 50ms to
150ms specifically to give ink 7 frame time. Re-running the test under
the bumped wait shows it passes reliably (5/5 runs in the full-file
context, 9/10 alone), so the skip was masking the throttle-flake that
the wait bump already addresses, not a real product bug.

Drop the it.skip and the now-stale comment so coverage of the
freePlaceholderId reuse logic is restored.

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* test(InputPrompt): bump first prompt-suggestion test wait to 350ms

The "accepts and submits the prompt suggestion on Enter when the buffer
is empty" test is the first in its describe block, so it pays the
renderer cold-start cost. On macOS-22.x CI runners that pushes the
Enter → onSubmit microtask past the default 150ms post-Enter wait. Match
the 350ms initial render wait used immediately above to absorb the cold
start.

* Revert "test(InputPrompt): bump first prompt-suggestion test wait to 350ms"

This reverts commit 6add83b62e.

* test(InputPrompt): wait for followup suggestion debounce before pressing Enter

Root cause of the failing prompt-suggestion tests on macOS and Windows
CI is not flaky timing of the test post-Enter wait — it's the 300ms
debounce inside createFollowupController.setSuggestion (shared core).
The Enter handler reads followup.state.isVisible synchronously, so if
the debounce timer has not fired before stdin.write('\\r'), the
suggestion path is skipped and onSubmit never runs. No amount of
post-Enter wait can recover from that — the keypress was already
processed against stale state.

The original wait(350) only left ~50ms margin over the 300ms debounce,
which ink 7 / React 19.2 mount overhead consumed on slow Windows
runners. Bump the initial wait to 700ms (named SUGGESTION_VISIBLE_WAIT_MS)
to give the debounce timer + cold-start render a generous buffer.

Apply to the two sibling tests too — without the wait their "does not
accept" assertions pass trivially when suggestion is never visible,
which is a false green that hides regressions in the actual reject path.

* fix(deps): align cli wrap-ansi with ink 7 (9.0.2 -> ^10.0.0)

Ink 7 ships its own wrap-ansi@10. CLI's direct dep was pinned to 9.0.2,
causing two copies of wrap-ansi in node_modules and a potential drift in
CJK width / ANSI handling between ink's internal text wrapping and our
TableRenderer.

Upgrading the CLI's direct dep to ^10.0.0 lets npm dedupe to a single
wrap-ansi@10 used by both ink and TableRenderer. API surface is
identical; the only documented behaviour change is that tabs are
expanded to 8-column tab stops before wrapping, which TableRenderer
doesn't feed in.

TableRenderer test suite (43 tests) passes against wrap-ansi@10.

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* chore(deps): document @types/node 20.x pin in overrides

The override pinning @types/node to 20.19.1 (while engines require
Node >=22) is intentional: bumping to @types/node@22.x re-introduces
a Dirent<NonSharedBuffer> type regression that breaks
@qwen-code/qwen-code-core/sessionService tests.

Add a sibling "//@types/node" note inside `overrides` so future
maintainers see the rationale and know when to revisit the pin
without having to dig through PR #3860 history.

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* test(AskUserQuestionDialog): link skipped Submit-tab test to tracking issue

The 'shows unanswered questions as (not answered) in Submit tab' test
was switched to `it.skip` in the ink 7 upgrade because
`ink-testing-library@4.0.0` doesn't flush input deterministically
through ink 7's 30fps throttle.

Add a `// TODO(#4036):` marker so the skip is greppable and can be
re-enabled once upstream ships an ink-7-compatible release.

Refs #4036

Generated with AI

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(deps): move @types/node pin comment out of overrides block

npm's `overrides` field requires every key to be a real package name —
the `"//@types/node"` comment-key added in 205855875 trips Arborist with
"Override without name" and breaks `npm ci` across all CI jobs.

Move the explanation to a sibling top-level `"//overrides"` key, which
npm ignores at the document root. Same documentation value, no
override-parser collateral damage.

---------

Co-authored-by: 秦奇 <gary.gq@alibaba-inc.com>
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-11 17:29:50 +08:00
jinye
2c93fd670c
refactor: extract shared release helper utilities (#3834)
Move four duplicated utility functions (getArgs, readJson,
validateVersion, isExpectedMissingGitHubRelease) from the three
get-release-version.js scripts into a shared module at
scripts/lib/release-helpers.js so that changes only need to happen
in one place.

Also fixes a pre-existing bug in getArgs where argument values
containing '=' were silently truncated (e.g. --msg=a=b produced
{msg:'a'} instead of {msg:'a=b'}).

Closes #3795

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

Co-authored-by: jinye.djy <jinye.djy@alibaba-inc.com>
2026-05-05 10:15:17 +08:00
jinye
03f66bada5
feat(sdk-python): add PyPI release workflow (#3685)
* feat(sdk-python): add pypi release workflow

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): build cli before smoke test

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): tighten release conflict handling

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): harden python release workflow

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): tighten stable release guards

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): harden prerelease publish flow

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): reuse release branches on rerun

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): resume incomplete releases

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(release): tighten missing-release checks

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): resume stable release reruns

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): tighten release recovery guards

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* test(sdk-python): cover release version edge cases

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): address release workflow review feedback

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* refactor(sdk-python): address review feedback on release version script

- Remove unreachable `if (type === 'stable')` branch in bumpVersion();
  the stable path was dead code since getVersion() throws for all
  stable conflicts before calling bumpVersion(). Move nightly conflict
  throw to the call site for symmetry.
- Rename getNextPatchBaseVersion → getNextBaseVersion to reflect that
  the function can return a prerelease base without incrementing patch.
- Add test for preview+nightly coexistence where nightly base is higher.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(sdk-python): address remaining review feedback on release workflow

- Fix failure-issue gate to read github.event.inputs.dry_run directly
  instead of steps.vars.outputs.is_dry_run (which is empty when early
  steps fail). Add --repo flag for gh issue create when checkout failed.
- Add diagnostic state table to failure-issue body (RELEASE_TAG,
  PACKAGE_VERSION, PUBLISH_CHANNEL, RESUME_EXISTING_RELEASE, etc.)
- Fix release-notes error swallow: only silence release not found /
  Not Found / HTTP 404, emit :⚠️: for other gh release view errors.
- Improve validateVersion error messages to use human-readable format
  keys (X.Y.Z, X.Y.Z-preview.N) matching TS sibling convention.
- Filter fully-yanked versions in getAllVersionsFromPyPI.
- Add console.error log when stable is derived from nightly.
- Add bash regex guard for inputs.version to prevent shell injection.
- Use per-release-type concurrency groups (nightly/preview/stable).
- Add jq null-guard checks for all 6 field extractions.
- Remove misleading --follow-tags from git push (lightweight tags).

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(sdk-python): rename misleading test description

The test asserts that preview/nightly releases return empty
previousReleaseTag, but the name said "same-channel previous
release tags" which implied non-empty values.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(sdk-python): address unresolved review comments on release workflow

- Remove -z check in extract_field() that blocked preview/nightly releases
  (previousReleaseTag is legitimately empty for non-stable releases)
- Use static environment.url since step outputs aren't available at job startup
- Use skip-existing for resumed PyPI publish to fill in missing artifacts
- Add AbortSignal.timeout(30s) to PyPI fetch to prevent indefinite hangs
- Add downgrade guard for stable_version_override
- Use GHA :⚠️: annotation instead of console.error for visibility
- Separate yanked/non-yanked version lists so conflict detection includes
  yanked versions (PyPI still reserves those slots)
- Filter current release from previousReleaseTag to avoid self-reference on resume
- Add tests for yanked conflict detection, downgrade guard, and resume previousReleaseTag

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>

* fix(sdk-python): address final review round on release version script

- Fix getNextBaseVersion() first-release skip: use pyproject.toml version
  directly when PyPI has no stable versions instead of unconditionally
  incrementing
- Fix getNextBaseVersion() off-by-one: change > to >= so equal prerelease
  base continues the existing line instead of incrementing patch
- Add :⚠️: annotation when preview auto-bumps due to orphan git
  tags (tag exists without PyPI version or GitHub release)
- Add set -euo pipefail to 5 workflow steps missing it: release_branch,
  persist_source, Create GitHub release, Delete prerelease branch, Create
  issue on failure
- Fix 2 existing tests affected by first-release change, add 4 new tests

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(sdk-python): use stderr for GHA warning annotations to avoid corrupting JSON stdout

console.log writes to stdout, which gets captured by VERSION_JSON=$(node ...)
in the workflow and corrupts the JSON output for jq. Switch to console.error
so :⚠️: annotations go to stderr (GHA recognizes workflow commands on
both streams). Also add set -euo pipefail to the "Get the version" step for
consistency with other workflow steps.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

---------

Co-authored-by: jinye.djy <jinye.djy@alibaba-inc.com>
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-05-04 21:07:21 +08:00
qwen-code-ci-bot
96116dc76f
chore(release): sdk-typescript v0.1.7 (#3688)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-04-28 14:59:13 +08:00
顾盼
aeeb2976d6
feat(web-search): remove built-in web_search tool, replace with MCP-based approach (#3502)
* feat(web-search): add GLM (ZhipuAI) web search provider

- Add GlmProvider class implementing BaseWebSearchProvider using the
  ZhipuAI Web Search API (https://open.bigmodel.cn/api/paas/v4/web_search)
- Support multiple search engines: search_std, search_pro, search_pro_sogou,
  search_pro_quark
- Support optional config: maxResults, searchIntent, searchRecencyFilter,
  contentSize, searchDomainFilter
- Truncate query to 70 characters per API limit
- Register 'glm' in the provider discriminated union (types.ts) and
  createProvider() switch (index.ts)
- Add GlmProviderConfig to settingsSchema, ConfigParams, and Config class
- Add --glm-api-key CLI flag and GLM_API_KEY env var support in webSearch.ts
- Forward GLM_API_KEY in sandbox environment
- Update provider priority list: Tavily > Google > GLM > DashScope
- Add 17 unit tests for GlmProvider and 4 integration tests in index.test.ts
- Update docs/developers/tools/web-search.md with GLM configuration,
  env vars, CLI args, pricing, and corrected DashScope billing info
- Fix stale OAuth/free-tier references in web-search.md

Closes #3496

* docs(web-search): fix DashScope note and add GLM server-side limitations

* fix(web-search): make DashScope provider work with standard API key, remove qwen-oauth dependency

- DashScopeProvider.isAvailable() now checks config.apiKey instead of authType
- Remove OAuth credential file reading and resource_url requirement
- Use standard DashScope endpoint: dashscope.aliyuncs.com/api/v1/indices/plugin/web_search
- Read DASHSCOPE_API_KEY env var and --dashscope-api-key CLI flag
- Forward DASHSCOPE_API_KEY into sandbox environment
- Update integration test to detect DASHSCOPE_API_KEY
- Update docs to reflect new API key based configuration

* feat(web-search): remove built-in web search tool

The web_search tool and all related provider implementations are removed.
Web search functionality will be provided via MCP integrations instead,
which is the direction the broader agent ecosystem is moving.

Removed:
- packages/core/src/tools/web-search/ (entire directory)
- packages/cli/src/config/webSearch.ts
- integration-tests/cli/web_search.test.ts
- ToolNames.WEB_SEARCH, ToolErrorCode.WEB_SEARCH_FAILED
- webSearch config in ConfigParams, Config class, settingsSchema
- CLI options: --tavily-api-key, --google-api-key, --google-search-engine-id,
  --glm-api-key, --dashscope-api-key, --web-search-default
- Sandbox env forwarding for TAVILY/GLM/DASHSCOPE/GOOGLE search keys
- web_search from rule-parser, permission-manager, speculation gate,
  microcompact tool set, and builtin-agents tool list

* fix: remove websearch reference

* docs: remove websearch tool

* docs: add break change guide

* fix review
2026-04-24 11:29:02 +08:00
chinesepowered
ed6f9e056e
fix(sdk): settle pending next() promise in Stream.return() to prevent hangs (#2981)
* fix(sdk): settle pending next() promise in Stream.return() to prevent hangs

* test(sdk): add regression tests for Stream.return() pending-promise cleanup
2026-04-18 09:46:56 +08:00
Reid
d439e7d738
fix(sdk): avoid leaking process exit listeners in ProcessTransport (#3295)
* fix(sdk): avoid leaking process exit listeners in ProcessTransport

* Strengthen ProcessTransport cleanup during process exit.

  This updates the shared process-exit cleanup path to use a best-effort
  SIGTERM/SIGKILL sequence and adds coverage to verify the global exit
  handler terminates all active child processes.

  It keeps the listener leak fix in place while closing the remaining gaps
  found in review.
2026-04-16 10:52:19 +08:00
Shaojin Wen
1486e85385
feat(cli/sdk): expose /context usage data in non-interactive mode and SDK API (#2916)
Some checks are pending
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / Lint (push) Waiting to run
Qwen Code CI / Test (push) Blocked by required conditions
Qwen Code CI / Test-1 (push) Blocked by required conditions
Qwen Code CI / Test-2 (push) Blocked by required conditions
Qwen Code CI / Test-3 (push) Blocked by required conditions
Qwen Code CI / Test-4 (push) Blocked by required conditions
Qwen Code CI / Test-5 (push) Blocked by required conditions
Qwen Code CI / Test-6 (push) Blocked by required conditions
Qwen Code CI / Test-7 (push) Blocked by required conditions
Qwen Code CI / Test-8 (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run
* feat(cli): implement non-interactive /context output and diagnostic

- Extract collectContextData() from contextCommand.ts for shared usage.
- Register /context in ALLOWED_BUILTIN_COMMANDS_NON_INTERACTIVE.
- Extend SDK control protocol with GET_CONTEXT_USAGE request.
- Implement handleGetContextUsage in SystemController for programmatic token queries.
- Expose getContextUsage() method in the TypeScript SDK Query interface.

* fix: address review feedback and fix critical bugs in context usage feature

- Add missing `get_context_usage` route in ControlDispatcher (SDK calls would throw)
- Fix `executionMode` defaulting: use `?? 'interactive'` to match other commands
- Validate dynamic import of `collectContextData` before invoking
- Preserve original error message in handleGetContextUsage catch block
- Add ControlDispatcher test for get_context_usage routing
- Add JSDoc comment for context command in non-interactive allowlist

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: re-check abort signal after async operations in handleGetContextUsage

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* docs: add getContextUsage() to SDK TypeScript documentation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* docs: clarify getContextUsage showDetails is a display hint, not a data filter

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: make showDetails affect response shape, add getContextUsage test

- When showDetails is false, return empty detail arrays instead of full
  data so /context and /context detail produce different payloads
- Add unit test for Query.getContextUsage() covering request payload
  and response handling

* fix: strip UI type from SDK response, sync Java SDK protocol

- Remove leaked `type: 'context_usage'` from control response payload
- Add GET_CONTEXT_USAGE to Java SDK protocol mirror (enum, interface,
  union type)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 03:28:32 +08:00
tanzhenxin
63f1963377
Merge pull request #2698 from QwenLM/refactor/subagent-model-selection
feat: add cross-provider model selection for subagents
2026-04-01 16:17:54 +08:00
github-actions[bot]
c7faae7b6e chore(release): sdk-typescript v0.1.6 2026-03-30 04:01:57 +00:00
tanzhenxin
ad20049a4e Merge remote-tracking branch 'origin/main' into refactor/subagent-model-selection 2026-03-27 11:56:29 +08:00
tanzhenxin
5d58b2f112 feat: simplify subagent model configuration with model selector
Refactor subagent model configuration from nested modelConfig object to a simple model string field for better UX and clarity.

Changes:
- Replace modelConfig object with model string in SubagentConfig interface
- Add model-selection.ts utility for parsing and validating model selectors
- Support 'inherit' keyword and bare model IDs (e.g., 'glm-5', 'claude-sonnet-4-6')
- Maintain backward compatibility by parsing legacy modelConfig frontmatter
- Update validation to reject cross-provider authType-prefixed selectors
- Update SDK types (TypeScript and Java) to reflect new schema
- Add comprehensive tests for model selection and validation
- Update documentation with model selection examples

Breaking changes:
- modelConfig.frontmatter field deprecated in favor of model field
- Cross-provider model selectors (e.g., 'openai:gpt-4') not supported for subagents

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-03-27 11:49:45 +08:00
mingholy.lmh
43bb14ddc9 docs(sdk): enhance coreTools/excludeTools/allowedTools documentation with permissions reference
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-03-24 22:26:54 +08:00
DragonnZhang
ee33a3c35e feat: add system prompt customization options in SDK and CLI 2026-03-16 02:57:31 +08:00
mingholy.lmh
82dc79629c feat: enhance session ID handling and error propagation 2026-02-13 21:41:38 +08:00
mingholy.lmh
5d939fdb83 feat: add --session-id support for CLI and SDK
- Add --session-id flag to CLI for specifying custom session ID
- Add sessionId option to SDK QueryOptions
- Implement UUID validation for session IDs
- Pass session ID from SDK to CLI via --session-id argument
- Add integration tests for session-id functionality
- Update unit tests for ProcessTransport

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-13 21:40:57 +08:00
LaZzyMan
3f04217458 fix: prevent AbortSignal listener memory leak
- Add abort listener cleanup in Query.close() to prevent memory leak
- Add abort listener cleanup in ControlDispatcher.shutdown()
- Remove AbortController recreation in Session.handleInterrupt()

This fixes the MaxListenersExceededWarning that occurred when:
- Creating 11+ Query instances in SDK/non-interactive mode
- Multiple user interrupts (Ctrl+C) in interactive mode
- Intensive control request scenarios
2026-02-12 10:39:19 +08:00
tanzhenxin
4d29d1fbb5
Merge pull request #1738 from QwenLM/feat/promote-skills-to-stable
feat: promote Agent Skills from experimental to stable
2026-02-06 17:52:39 +08:00
tanzhenxin
a4ffc6eb24 feat: promote Agent Skills from experimental to stable
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-06 13:04:08 +08:00
Mingholy
5c582e98c6
Merge pull request #1732 from QwenLM/mingholy/fix/sdk-release-workflow
Some checks are pending
Qwen Code CI / Lint (push) Waiting to run
Qwen Code CI / Test (push) Blocked by required conditions
Qwen Code CI / Test-1 (push) Blocked by required conditions
Qwen Code CI / Test-2 (push) Blocked by required conditions
Qwen Code CI / Test-3 (push) Blocked by required conditions
Qwen Code CI / Test-4 (push) Blocked by required conditions
Qwen Code CI / Test-5 (push) Blocked by required conditions
Qwen Code CI / Test-6 (push) Blocked by required conditions
Qwen Code CI / Test-7 (push) Blocked by required conditions
Qwen Code CI / Test-8 (push) Blocked by required conditions
Qwen Code CI / Post Coverage Comment (push) Blocked by required conditions
Qwen Code CI / CodeQL (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:docker (push) Waiting to run
E2E Tests / E2E Test (Linux) - sandbox:none (push) Waiting to run
E2E Tests / E2E Test - macOS (push) Waiting to run
Add CLI source selection for SDK releases and fix subagent output handler
2026-02-06 13:01:05 +08:00
mingholy.lmh
77a8698bc5 ci(sdk-release): fix nightly and preview version calculation based on npm latest+1 patch
- Add getNextPatchVersion() to calculate next patch version from npm latest
- Fix getNightlyVersion() to use npm latest + 1 patch instead of package.json
- Fix getPreviewVersion() to use npm latest + 1 patch instead of nightly version
- Add version info logging in workflow for dry-run verification

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-06 12:34:31 +08:00
mingholy.lmh
39884cc6a1 ci(sdk-release): fix CLI package path handling
- Remove dist/ directory requirement from workflow (CLI files are in package root)
- Update bundle-cli-from-npm.js to use package root directly instead of package/dist

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-05 22:06:08 +08:00
mingholy.lmh
1e05359881 ci(sdk-release): add CLI source selection for SDK releases
- Replace cli_ref input with cli_source choice (build_from_source or npm_latest)
- Add support for bundling latest stable CLI from npm
- Add bundle-cli-from-npm.js script for npm-based CLI bundling
- Fix property naming in nonInteractiveCli.ts (outputUpdateHandler)
- Improve integration tests for subagents
- Skip creating issue on failure during dry-run mode

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-05 20:57:01 +08:00
tanzhenxin
7d50f5363f
Merge pull request #1726 from QwenLM/mingholy/fix/sdk-resume-usage
feat(sdk): add resume, continue options and extend authType support
2026-02-05 19:03:25 +08:00
tanzhenxin
03b2cfbbde
Merge pull request #1719 from QwenLM/mingholy/fix/patch-process-transport-for-electron
Add FORK_MODE support to ProcessTransport for Electron IPC integration
2026-02-05 19:02:30 +08:00