Skip to content

fix(bots): apply the configured network proxy to Client-owned bot traffic - #5159

Open
ying-hua wants to merge 3 commits into
apache:mainfrom
ying-hua:fix/5091-bot-proxy
Open

ying-hua wants to merge 3 commits into
apache:mainfrom
ying-hua:fix/5091-bot-proxy

Conversation

@ying-hua

@ying-hua ying-hua commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

setActiveProxy has no production caller anywhere in the repository, so resolveActiveProxy() always returns null and the bot bridges' proxiedFetch never builds a proxy dispatcher. Every bot request goes direct whatever the Network settings say. On a blocked network that surfaces as the Fetch timeout in the report, and it explains why enabling TUN was the only thing that worked — TUN intercepts below the application, so it needs no cooperation from this seam.

Model execution was never affected: it resolves the proxy inside the Host and injects a transport per connection. The bot bridges cannot do that. BotRegistry is constructed in the Client process, the local Host is a separate spawned process, and the resolved proxy — including its credential — only exists Host-side. That is the actual gap; the protocol exposed only network-proxy.test.

So this adds network-proxy.resolve, served by HostNetworkProxyCoordinator from the existing resolveNetworkProxyExecution() and toRuntimePolicyProxy(), and applies the result in the Client at Host registration and whenever a settings patch writes the proxy policy.

Two decisions worth flagging:

  • A non-local Host's policy is never adopted. The bot bridges dial out from the Client machine, so a remote or environment Host describes the wrong network. Those profiles are not even asked — the Client stays direct rather than guessing.
  • The retry ladder backs off but never runs out. The first resolution can lose a race with Host readiness. While the policy asks for a proxy the Client cannot obtain, bot traffic is going direct past the user's network policy, and giving up would leave that in place until the next proxy edit or restart — so the last delay (60s) repeats instead. The failure is reported on the same redacted [bots:*] support-log channel that carries the bot connection failures it causes, so both appear together in an attached log.

RUNTIME_HOST_COMPATIBILITY_EPOCH goes to 238. Two builds can share an epoch and differ in operations, so without the bump a Client could reach an already-running older local Host that cannot answer the new operation — and a Client cannot tell that refusal apart from a proxy that is genuinely unset.

The fix covers every bot platform, not just Telegram — telegram, discord, qq, dingtalk and wechat bridges, credential verification in bot-test.ts, and the Desktop onboarding and QQ scan-login paths all share proxiedFetch.

Fixes #5091

Verification

Windows 11 with Clash listening on 127.0.0.1:7897. HTTP_PROXY/HTTPS_PROXY are stripped from the process to match a normal desktop launch.

Driving the real chain end to end — Host coordinator → protocol codec → applier → setActiveProxy → the bots' proxiedFetch → a live request to api.telegram.org. The decisive case is B: pointing the policy at a port with nothing listening must make the request fail, because a request that still succeeds is one that leaked past the proxy.

env HTTPS_PROXY = (unset)
A. no refresh — unwired seam, request goes direct: HTTP 200 in 3612ms
B. proxy 127.0.0.1:1 (nothing listening)         : FAILED in 5ms -> fetch failed
C. same dead proxy, but a remote Host            : HTTP 200 in 986ms
D. proxy 127.0.0.1:7897 (Clash)                  : HTTP 200 in 4200ms

A is main today — the request ignores the policy entirely. B shows traffic is now genuinely routed through the configured proxy. C shows the non-local gate holds: a remote Host's policy is not adopted. D shows a working proxy is used.

Separately, on the original report's network conditions (TUN off, system proxy on), the unwired seam reproduces the reported symptom verbatim — Fetch timeout after 10017ms, matching TELEGRAM_REQUEST_TIMEOUT_MS = 10_000 — and populating the seam returns HTTP 200 in 2453ms.

Checks run locally:

  • npm run lint, npm run format:check, npm run check:asf-headers — clean
  • @maka/runtime-host typecheck clean; apps/desktop tsconfig.main.json clean
  • Affected suites, 127 passing: the new network-proxy-coordinator and client-network-proxy suites, plus runtime-host-settings-ipc-main, proxied-fetch and protocol

Not run: the full workspace suite and the Desktop E2E specs. Three renderer typecheck errors (mcp-page.tsx, use-focused-preview.ts) and the @maka/ui build are broken in my local checkout; this branch touches neither packages/ui nor the renderer, so they are environment staleness rather than results about this change.

Two Runtime Host suites do not pass on this machine, and neither is evidence about this change:

  • handshake-compatibility.test.ts fails 4 of 6, identically on unmodified main (RuntimeHostTransportError: read_eof).
  • host-kernel.test.ts is unstable here regardless of the change: unmodified main fails 4 of 81 and the failing set differs between runs (write EPIPE, Runtime Host did not enter composition, shutdown did not cleanly close every resource). This branch failed 1–2 of 81 — a subset of what clean main produces.

framed-transport.test.ts passes 6 of 6. These look like the local Windows platform-fixture and cleanup problems tracked in #5059; CI or a reviewer on Linux/macOS should confirm both suites.

Security

network-proxy.resolve sends the proxy credential from the Host to the Client. That is new, and SECURITY.md calls out proxiedFetch specifically, so it deserves explicit review. The reasoning: a Client that runs its own outbound traffic cannot dial an authenticated proxy without the secret, and the alternative — a separate Client-side proxy setting — duplicates configuration the user has already entered. The failure path returns a fixed message rather than the underlying error, which can carry credential material, and there is a test for that.

If you would rather keep the secret Host-side, the alternative is a dedicated Client-owned bot proxy setting (option 1 in the report). Happy to switch — this is the one open question from the issue that has not had a maintainer answer.

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Claude Code — root-cause investigation, the implementation, and the tests, under my direction and review. Commits carry Generated-by: Claude Code.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

@github-actions github-actions Bot added the effort/L Under 1000 readable lines label Sep 10, 2026

@me2seeks me2seeks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated review notice: This comment was posted by an automated review agent operated by me2seeks make. It is not an independent human review and does not replace one.

Summary

Applies the Runtime Policy network proxy to the Client's own outbound bot traffic. The issue (#5091) is real: model execution resolves the proxy inside the Host with per-connection transports, but BotRegistry is constructed in the Desktop process, so bot requests bypassed the configured proxy entirely. The new client-network-proxy.ts bridges the seam via setActiveProxy, documents the process-wide state contract, and retries host_not_ready refusals on a bounded [1s, 5s, 15s] ladder since the first resolution races Host connection setup. Host-side network-proxy-coordinator.ts and protocol codecs carry the resolution operation.

Findings

  1. [P2] No test lane has run on this head — the CI rollup shows only label pass. For an 863-line networking change with new coordinator logic and an epoch-relevant protocol file (packages/runtime-host/src/protocol/index.ts), the required test gate must run green before merge. The PR is also still a draft.
  2. [P3] RETRY_DELAYS_MS exhaustion is silent: after the 15s third retry the Client stays direct with no user-visible signal that the configured proxy could not be applied — bot traffic then leaks past the user's network policy until the next proxy edit or restart. onError exists as a dep; confirm it's wired to a diagnostic surface in production, not just tests.

Verdict

needs-changes — direction and layering are right (process-wide seam documented honestly), but zero CI signal on the head and the silent-exhaustion path need attention; also still a draft.

@me2seeks me2seeks mentioned this pull request Sep 28, 2026
2 of 6 tasks
ying-hua added a commit to ying-hua/maka that referenced this pull request Sep 29, 2026
The guide said the channel's proxyUrl reaches every bot API call through
proxiedFetch. It does not. No bridge reads proxyUrl, and proxiedFetch
asks resolveActiveProxy() for the app's network proxy, which nothing in
the product ever sets — so on main every bot request goes direct. The
earlier claim came from reading proxiedFetch without tracing who
populates the value it reads.

The live verifications that looked like proxy coverage all ran with a
TUN-mode proxy active, which intercepts below the application. They do
not demonstrate Maka-level proxy support, and the guide now says so.

Replaces the five affected passages with one Network routing section:
what does not route (proxyUrl, the unwired app proxy), what would stay
outside proxiedFetch even once wired (the gateway sockets, and the
Slack, Feishu/Lark and WeCom SDKs), and what works today (a
system-level route). Adds it to the known limitations and points at
apache#5159, which wires the app proxy into the REST paths only.

Also records that iLink replies are sent with an empty context_token
and inbound context is never kept, credited to apache#5115, where
it was first identified. Left marked untested, since the iLink route
could not be exercised.

Refs apache#3894

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ying-hua
ying-hua marked this pull request as ready for review September 29, 2026 08:56
@ying-hua

Copy link
Copy Markdown
Contributor Author

Thanks — both findings were fair. Rebased onto current main (the branch was 365 commits behind and conflicting) and addressed them.

[P2] draft / no test lane. Marked ready for review, so the required gate can run. The epoch also had to be re-taken: I had claimed 141, which main has since used for WorkHub root admissions, so it is now 199.

[P3] silent exhaustion. Agreed, and the diagnosis was sharper than "add a log" — the real defect was that the ladder ended. While the policy asks for a proxy this Client cannot obtain, bot traffic is going direct past the user's network policy, and no amount of reporting fixes that; stopping the retries is what leaves it in place until the next proxy edit or restart. So the last delay (60s) now repeats instead of the budget running out, with one retry held in flight so a failing explicit refresh cannot fan out into a second ladder.

On the surface question: onError was indeed only wired to console.error. It now reports on the same redacted [bots:*] channel that settings-ipc-helpers.ts already uses for bot connection failures — the channel whose output the reporter pasted into #5091 — so the proxy failure and the Fetch timeout lines it causes appear together in an attached log:

[bots:proxy] proxy_unresolved: bot traffic stays direct until the Runtime Host answers — <redacted>

I stopped short of a toast or a BotStatus.readiness state: BotRegistry has no API to accept an external warning, and adding one would pull in the bots package, the status shape, the settings page and its i18n copy. Happy to do it as a follow-up if you would rather the degraded state be visible in-product rather than in the support log.

Verification was also redone against the rebased head. The decisive case points the policy at a port with nothing listening — a request that still succeeds there is one that leaked past the proxy:

A. no refresh — unwired seam, request goes direct: HTTP 200 in 3612ms
B. proxy 127.0.0.1:1 (nothing listening)         : FAILED in 5ms -> fetch failed
C. same dead proxy, but a remote Host            : HTTP 200 in 986ms
D. proxy 127.0.0.1:7897 (Clash)                  : HTTP 200 in 4200ms

A is main today, B shows traffic is genuinely routed through the configured proxy, C shows the non-local gate holds, D shows a working proxy is used. Affected suites are at 127 passing; lint, format, ASF headers and the relevant typechecks are clean. Details and the local-environment caveats are in the updated description.

Still unanswered from #5091, and the one thing I would not want to merge without a maintainer view: network-proxy.resolve sends the proxy credential across the Host↔Client boundary, and SECURITY.md names proxiedFetch specifically. If that is not acceptable, the alternative is a dedicated Client-owned bot proxy setting and I will switch.

@hqhq1025 hqhq1025 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 0f80745bb34b686d3b4af49ad57a4a58a6c186fa. The new Host operation resolves the effective proxy for Client-owned bot traffic, and Desktop refreshes it at startup and after a proxy settings patch. The protocol now carries the proxy secret and bumps compatibility epoch to 199.

I found one P2: when an enabled authenticated proxy has no stored credential, the Host returns credential_not_configured, but the Client treats it like a disabled proxy and calls setActiveProxy(null). proxiedFetch then sends Telegram/other bot traffic directly, contrary to the configured proxy policy. The added test explicitly expects this direct fallback. An unavailable credential should fail closed for Client-owned outbound traffic, or at least preserve a distinct unavailable state rather than converting it to "proxy disabled". This also matters when a previously working credential is removed while the proxy policy remains enabled.

Current-head hosted test, audit, and label checks are green; static merge-tree against main ae71ab319 and git diff --check are clean. I did not independently run Windows/Clash, a packaged Desktop, or a live bot request. I did not find another substantiated issue in the inspected wiring and protocol paths. This is not an approval or merge recommendation.

Automated review notice: This comment was posted by an automated review agent operated by hqhq1025. It is not an independent human review and does not replace one.

}
lastReportedError = undefined;
attempt = 0;
apply(resolved.kind === "ready" ? (resolved.proxy ?? null) : null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Keep credential_not_configured distinct from a disabled proxy. The Host returns this state specifically when policy requires proxy authentication but the secret is missing (network-proxy-coordinator.ts:87-89). Mapping it to null makes proxiedFetch omit the dispatcher and send bot traffic directly, including after a previously saved credential is deleted while authentication remains enabled. The new test currently asserts that fallback. Block Client-owned outbound requests until the credential is configured, rather than silently bypassing the user's enabled proxy policy.

@github-actions github-actions Bot added the stale No qualifying activity within the lifecycle policy window label Oct 30, 2026
@github-actions

Copy link
Copy Markdown

This pull request has had no new commits for 30 days and has been marked stale. It will be closed in 7 days unless a new commit is pushed.

Comments do not reset this timer: only a new commit does. If the pull request is intentionally long-lived, a maintainer can apply the pinned label.

ying-hua and others added 3 commits October 30, 2026 20:17
…ffic

`setActiveProxy` had no production caller, so `resolveActiveProxy()` always
returned null and the bot bridges' `proxiedFetch` never built a proxy
dispatcher. Every bot request went direct whatever Network settings said,
which on a blocked network surfaces as the `Fetch timeout` in apache#5091.

Model execution is unaffected because it resolves the proxy inside the Host
and injects a transport per connection. The bot bridges cannot: `BotRegistry`
is constructed in the Client process, and the resolved proxy — including its
credential — only exists Host-side.

Add `network-proxy.resolve` so the Host can serve that resolved proxy, and
apply it in the Client at Host registration and whenever a settings patch
writes the proxy policy. A non-local Host describes a different machine's
network, so its policy is never adopted; the Client stays direct instead.

Refs apache#5091

Generated-by: Claude Code
… log

Review noted that the retry ladder ran out silently: after the third delay
the Client stayed direct with no signal, so bot traffic kept going past the
network policy the user configured until the next proxy edit or restart.

While the policy asks for a proxy this Client cannot obtain, something is
wrong that giving up cannot fix. Repeat the last delay instead of exhausting
the ladder, and hold one retry in flight so a failing explicit refresh does
not fan out into a second one.

Report the failure through the same redacted `[bots:*]` support-log channel
that carries the bot connection failures it causes, rather than a bare
console.error, so both appear together in an attached log.

Refs apache#5091

Generated-by: Claude Code
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…issing

When the network policy enables an authenticated proxy but no credential is
stored, the Host answers `credential_not_configured`. The Client mapped that to
a disabled proxy, so `proxiedFetch` sent bot traffic direct, past the policy the
user configured, including after a saved credential was removed.

Add a blocked state to the active proxy seam: `proxiedFetch` now refuses to send
while it is set, and the Client enters it on `credential_not_configured`,
reporting once on the `[bots:proxy]` channel. Saving the credential is a proxy
patch, so the refresh it triggers unblocks traffic.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ying-hua added a commit to ying-hua/maka that referenced this pull request Oct 30, 2026
The guide said the channel's proxyUrl reaches every bot API call through
proxiedFetch. It does not. No bridge reads proxyUrl, and proxiedFetch
asks resolveActiveProxy() for the app's network proxy, which nothing in
the product ever sets — so on main every bot request goes direct. The
earlier claim came from reading proxiedFetch without tracing who
populates the value it reads.

The live verifications that looked like proxy coverage all ran with a
TUN-mode proxy active, which intercepts below the application. They do
not demonstrate Maka-level proxy support, and the guide now says so.

Replaces the five affected passages with one Network routing section:
what does not route (proxyUrl, the unwired app proxy), what would stay
outside proxiedFetch even once wired (the gateway sockets, and the
Slack, Feishu/Lark and WeCom SDKs), and what works today (a
system-level route). Adds it to the known limitations and points at
apache#5159, which wires the app proxy into the REST paths only.

Also records that iLink replies are sent with an empty context_token
and inbound context is never kept, credited to apache#5115, where
it was first identified. Left marked untested, since the iLink route
could not be exercised.

Refs apache#3894

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ying-hua

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (85a1031c1) and addressed the open review.

Epoch. 199 has since been taken on main, which is now at 237, so this PR takes 238.

[P2] credential_not_configured going direct (@hqhq1025). Agreed: when the policy still asks for an authenticated proxy, that state should not be treated as "proxy disabled". Fixed in c1911d0:

  • active-proxy-state gains a blocked state (blockActiveProxy(reason), cleared by the next setActiveProxy). While it is set, proxiedFetch throws ActiveProxyBlockedError before any dispatcher or socket is created.
  • The Client applier enters it on credential_not_configured and reports once on the redacted [bots:proxy] channel as proxy_credential_missing. Saving the credential is a network.proxy patch, so the refresh it triggers unblocks traffic. Removing a saved credential while authentication stays enabled blocks it again.
  • The test that asserted the direct fallback now asserts the block. New tests cover: a repeated block is reported once; the block lifts once the credential is configured and returns if it is removed; and a real local server receives no request while blocked.

Affected suites pass locally (desktop proxy and settings IPC 27, runtime proxy/network 83, coordinator 9, protocol 93). Runtime and runtime-host typechecks, biome and git diff --check are clean.

Scope note: the Feishu, WeCom and Slack SDK clients and the bot WebSocket connections never went through proxiedFetch, so neither this PR nor the block covers them. That gap predates this PR.

Still open for a maintainer: whether network-proxy.resolve may carry the proxy credential across the Host↔Client boundary (see SECURITY.md §2.3.6). If not, I'll switch to a Client-owned bot proxy setting.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Automated review notice: This comment was posted by an automated review agent. It is not an independent human review and does not replace one.

I reviewed the current 15-file diff at c1911d07750b86d8a3bd85b382761e8f21339c4a, including the Desktop startup/settings wiring, serialized proxy refresh and retry behavior, Host resolution and strict protocol codecs, operation grants, active proxy state, outbound fetch guard and tests. I also read the previous review at 0f80745bb34b686d3b4af49ad57a4a58a6c186fa and compared the missing-credential behavior.

The previously reported missing-credential P2 is fixed. With the actual current compiled applier, active state and proxiedFetch, a controlled Host result of credential_not_configured blocked the request and a real loopback HTTP server received zero requests. Explicitly applying a direct policy then permitted one request. Restoring only the actual prior applier, with the same result and current outbound modules, caused one direct request instead. Both diagnostic cases passed their expected current/control assertions. No real proxy password or external bot service was used.

A fresh npm ci --ignore-scripts, dependency patches and the normal root npm run build:test completed successfully. I ran 137 focused tests covering the applier, settings IPC, Host coordinator, protocol and proxied fetch, plus 26 existing Bot bridge tests; all passed. These include actual local HTTP requests as well as controlled Host/bridge adapters, not an end-to-end packaged Desktop or vendor-account run.

I found no additional confirmed P0–P2 in this scope. Important limits remain: this wires Client-owned REST requests using proxiedFetch; gateway WebSockets and SDK-owned transports are not thereby proxied. An unresolved Host query retains the last proxy state and retries, so an initially unresolved state can still be direct. The new operation deliberately carries the proxy secret from Host to Client and is granted to remote owners; the reviewed Desktop path consumes it in the main process, without returning it through the renderer settings result. I did not independently validate the full remote-owner security boundary, Windows/Clash, real bot accounts or Desktop E2E.

Against current main 85a1031c10d104456127f35aac1523b9ffb1a384, the actual merge-tree and protocol guard passed: 237 → 238. git diff --check passed, and current-head test/audit checks are successful. The PR description still says epoch 199; please refresh that prose to match the implementation. Recheck the epoch against the live base when integrating, since another pending PR also proposes 238. There is no database migration in this diff.


Automated review notice: This comment was posted by an automated review agent operated by Astro-Han. It is not an independent human review and does not replace one.

@github-actions github-actions Bot removed the stale No qualifying activity within the lifecycle policy window label Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/L Under 1000 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram Bot listener times out with TUN disabled; proxy environment variables restore message reception

4 participants