qwen-code/packages/cli
jinye ff63da2652
refactor(serve): extract createInMemoryChannel helper (#4156 A1) (#4160)
* refactor(serve): extract createInMemoryChannel helper from httpAcpBridge.test.ts (#4156 A1)

Sub-PR A1 of issue #4156 (Stage 1.5b Mode A daemon). Pure refactor with
zero behavior change.

Extracts the inline paired NDJSON channel construction (`new TransformStream`
× 2 + `ndJsonStream` × 2) that was duplicated across `httpAcpBridge.test.ts`
into a production helper `createInMemoryChannel()` at
`packages/cli/src/serve/inMemoryChannel.ts`. The helper is added to
`packages/cli/src/serve/index.ts`'s barrel export alongside the rest of
the serve module's public API.

The helper is intentionally bare — it returns only the stream pair, no
lifecycle / teardown surface. Two reasons:
  1. Consumer behavior diverges widely (stuck channel, crashable child
     simulation, no-op, real in-process termination); a one-size-fits-all
     `close()` would either pull test-fixture concerns into a production
     module or force a single shape on consumers that don't want it.
  2. The SDK's `ndJsonStream` outer wrapper does not reliably propagate
     close on `Stream.writable` to the opposite `Stream.readable`;
     consumers needing to simulate a child exit hold their own underlying
     `TransformStream` references and close those directly.

10 of 11 inline call sites in `httpAcpBridge.test.ts` migrate cleanly to
the new helper. The 11th (`makeChannel` at line 151) keeps the inline
4-line construction because its `kill()` closure needs the underlying
`ab` / `ba` writables to simulate child-process termination — a comment
above the function explains the asymmetry.

The helper is also a primitive for the future A2 PR's
`inProcessAcpBridge.ts`, which will use it to wrap an in-process
`QwenAgent` without spawning a `qwen --acp` child (see issue #4156 §3
decision 1 and §8).

Test plan:
- New `inMemoryChannel.test.ts`: 5 tests covering bidirectional round-trip,
  ordering preservation, and bidirectional direction isolation
- Existing `httpAcpBridge.test.ts`: 70 tests, identical count and behavior
  before vs after migration
- `vitest run packages/cli/src/serve/inMemoryChannel.test.ts
  packages/cli/src/serve/httpAcpBridge.test.ts` — 75/75 pass
- `tsc --noEmit -p packages/cli/tsconfig.json` — clean for changed files

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

* refactor(serve): address Copilot review feedback on createInMemoryChannel

Two small follow-ups from #4160 review:

1. inMemoryChannel.test.ts:113,137 — handle the pending `reader.read()`
   that the isolation tests intentionally leave hanging when the timeout
   wins the race. `reader.releaseLock()` in `finally` rejects that
   pending read per Web Streams spec; without a rejection handler this
   could surface as an unhandled rejection / flaky test signal. Added a
   no-op rejection handler via the two-arg `.then(onResolve, onReject)`
   form so the cleanup-path rejection settles cleanly.

2. inMemoryChannel.ts:11 — the JSDoc said "two `TransformStream<...>`
   pairs" which reads ambiguously as "two pairs of TransformStream"
   (i.e., 4 streams). The implementation creates exactly two
   TransformStreams (one per direction). Reworded to "two
   `TransformStream<...>` instances (one per direction)" to disambiguate.

Tests still 5/5 pass.

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

* refactor(serve): expose abort() teardown primitive on createInMemoryChannel + route test through barrel

Two follow-ups from #4160 review:

1. Expose `abort(reason?)` on the helper return value (per @wenshao
   critical comment). Reasoning: the helper previously returned only the
   `Stream` pair, leaving consumers no way to tear the channel down.
   `ndJsonStream`'s outer wrapper does not reliably propagate `close()`,
   but `abort()` on the underlying byte-level `TransformStream` is
   forceful-by-spec — pending reads on both sides settle immediately so
   GC can reclaim. This unblocks the future Stage 1.5b in-process bridge
   (#4156, sub-PR A2) which needs teardown on daemon shutdown.

   The settlement shape is documented honestly in JSDoc: at the inner
   byte-level layer pending reads reject with the supplied reason; at
   the outer SDK-wrapped `Stream` the wrapper translates that into a
   clean `{done: true}` signal. Either way, pending operations no
   longer hang — that's the teardown invariant we care about.

2. Route the test's import through the `serve/index.js` barrel rather
   than the source file (per @wenshao suggestion). Without a test that
   exercises the public API path, a typo or missing re-export in the
   barrel would go undetected in CI.

Tests: 8/8 helper tests pass (5 existing + 3 new abort tests covering
teardown invariant + idempotency + no-reason variant). 70/70 existing
httpAcpBridge tests still pass.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)
2026-05-15 14:43:06 +08:00
..
src refactor(serve): extract createInMemoryChannel helper (#4156 A1) (#4160) 2026-05-15 14:43:06 +08:00
index.ts fix(cli): stop double-wrapping and double-printing API errors in non-interactive mode (#3749) 2026-05-03 08:39:31 +08:00
package.json chore(release): v0.15.11 [skip ci] 2026-05-14 09:51:46 +08:00
test-setup.ts fix: prevent bogus shell permission rules in tests 2026-03-20 17:55:33 +08:00
tsconfig.json Add background agent resume and continuation (#3739) 2026-05-01 12:14:33 +08:00
vitest.config.ts refactor(core): Unify package exports and improve dev experience 2026-02-01 11:59:05 +08:00