Repository navigation
tower: forward /ide/ to a local web IDE server + afx ide lifecycle (#1668) - #1683
Merged
Merged
Conversation
…Decisions section
…defer gzip, real-server local verification
…econcile, tunnel block
…ion, start-collision, 0600
amrmelsayed
added a commit
that referenced
this pull request
Sep 15, 2026
amrmelsayed
added a commit
that referenced
this pull request
Sep 15, 2026
This was referenced Sep 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PIR Review: Tower
/ide/prefix forward +afx idelifecycleFixes #1668
Summary
Tower can now forward a fixed
/ide/prefix — HTTP and WebSocket — to a single local VS Code server-web process through the same tunnel that serves the dashboard, so a browser IDE runs behind Tower and the cloud proxy without taking the dashboard offline. A newafx ide start|stop|statuslifecycle has Tower spawn/manage that server (workspace chosen per browser connection via?folder=), with runtime state in a~/.agent-farm/ide-server.jsonrecord — no config file, no database change. The forward is key-authenticated through Tower's existing choke point (a swappable seam for #1589) and injects the server's connection token so the workbench boots end-to-end through the relay.Files Changed
(vs merge-base; excludes commits the base branch absorbed after branching)
packages/codev/src/agent-farm/servers/ide-forward.ts(+292 / -0) — new: prefix match, header hygiene,x-forwarded-*stamping, connection-token cookie injection, HTTP proxy, WS raw-pipepackages/codev/src/agent-farm/servers/ide-server.ts(+265 / -0) — new: spawn/stop/status/reconcile/respawn + local-only/api/ideendpointpackages/codev/src/agent-farm/lib/ide-record.ts(+142 / -0) — new:~/.agent-farm/ide-server.jsonrecord + connection tokenpackages/codev/src/agent-farm/commands/ide.ts(+155 / -0) — new:afx ide start|stop|statuspackages/codev/src/agent-farm/utils/server-utils.ts(+37 / -0) — newisForwardAuthorizedseam (no change to existing functions)packages/codev/src/agent-farm/servers/tower-routes.ts(+16 / -0) —/api/ide+/ide/*dispatch, post-authpackages/codev/src/agent-farm/servers/tower-websocket.ts(+20 / -0) — IDE WS branch before the generic gatepackages/codev/src/agent-farm/servers/tower-server.ts(+7 / -0) — boot reconcile wiringpackages/codev/src/agent-farm/lib/tunnel-client.ts(+12 / -4) — block/api/idefrom the tunnelpackages/codev/src/agent-farm/__tests__/ide-forward.test.ts,ide-forward-auth.test.ts,ide-record.test.ts,ide-forward-integration.test.ts— new testscodev/resources/arch.md,codev/resources/lessons-learned.md— cold-tier governance updatesCommits
5501bd762[PIR tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668] IDE server record, lifecycle, forward, and auth seam2d22e1833[PIR tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668] Wire /ide/ forward into Tower routing, WS upgrade, boot reconcile, tunnel block0ad36f50b[PIR tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668] afx ide start|stop|statusaa4c0dc0d[PIR tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668] Tests: forward header hygiene, auth seam, record, HTTP+WS integration08ed262cfMerge remote-tracking branch 'origin/main' (brings in tower tunnel: small RPCs starve behind large transfers on the shared H2 session (default 64 KB windows) — 39 s explorer stall through the relay #1677 H2-window fix)6bf0bbe73[PIR tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668] Inject IDE connection-token cookie on the forward (fix cloud 403)f121787ce[PIR tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668] Tests: connection-token cookie injection + merge-not-clobber (HTTP+WS)Test Results
pnpm --filter @cluesmith/codev build(deps + tsc + assets): ✓ passvitest: ✓ pass — 32 new IDE tests; no regressions across every touched suite (tower-routes,tower-websocket,tunnel-client,tunnel-edge-cases,bugfix-1586-tunnel-auth,tower-tunnel,server-utils, and the mergedbugfix-1677-tunnel-h2-window). Thedev-approvalgate'sbuild+testschecks both ran green.Verification (full evidence chain)
Cloud 403 — root cause & fix. codev-ide's first cloud run through
/t/<tower>/ide/returned 403: Tower spawns the server with--connection-token-filebut the forward didn't present that token, and the token can't ride the browser's cookie jar through the relay (the server'sSet-CookieisPath=/ide, which never matches/t/<tower>/ide/). Fix: the forward injectsvscode-tkn=<token>(from~/.agent-farm/ide-connection-token) into theCookieheader on both HTTP and WS upgrades, merging and never clobbering. The token boundary is kept, not disabled — dropping it would leave an unauthenticated any-folder IDE on direct127.0.0.1:<port>, bypassing Tower's key.Behavior matrix (how each row is verified is stated per-row — not all are exercised through the full
handleRequeststack; runnable locally via the stub recipe):GET /ide/(no key)isRequestAllowed(existing tests) +isPublicRoute('/ide/') === false(unit) —/ide/rides the key choke point, no public carve-outGET /ide/(key, no server)GET /ide/?folder=…(key + server)?folderverbatim;Cache-Control/ETagpreserved;vscode-tkninjected; Tower key / tunnel marker /x-forwarded-portstripped; Host → loopbackforwardIdeHttp)WS /ide/?reconnectionToken=…(key)vscode-tkninjected on the upstream upgradeforwardIdeWebSocket)GET /api/ide(key)isBlockedPathunit tests (/api/ideblocked,/ide/not)codev-ide local pass (fork side). Keyed
GET :4100/ide/+Cookie: vscode-tkn→ 200; Playwright boot with key+cookie through the forward = full workbench ~5s, explorer populated (25 rows); local (non-tunnel)remoteAuthorityresolution confirmed fine. codev-ide accepts #1668.Cloud pass (real relay, at 5ab4687). Amr reloaded
https://<relay>/t/<tower>/ide/on the production relay: workbench boots; management socket 630 ms, extension host 939 ms; explorer populated; owner: "looking good". Remaining console anomalies were all fork-side / CDN — none in the forward. Cloud-leg latency is judged with #1677 (H2 window starvation on the shared tunnel session) accounted for; #1677 is merged into this branch, and the forward itself is loopback-only and unaffected by that starvation.Review-round corrections (CMAP iteration 1)
The 3-way consultation returned APPROVE (Gemini) + two REQUEST_CHANGES (Codex, Claude), all accepted as correct and addressed on the branch:
lsofremoved from the forward hot path (the leading fix).forwardIdeHttp/forwardIdeWebSocketpreviously calledensureIdeServerLive()→getProcessesOnPort()(execSync('lsof …'), ~98 ms, synchronous) on every request, blocking Tower's whole event loop during a workbench boot. Correcting my own earlier claim to the reviewer that non-IDE users were unaffected: they were — an IDE boot would stall the dashboard, PTY streaming, and tunnel. The forward now resolves the port from the record (no scan) and lets the upstream connect error be the liveness signal: onECONNREFUSED/ECONNRESETit fires an off-hot-path respawn and 502s.lsofis now cold-path only (status / reconcile / start-collision).stopIdeServerrestored the plan's SIGTERM → poll → SIGKILL escalation and now deletes the record only after confirming exit (was: SIGTERM + immediate delete, which could leave a survivor to be re-adopted).spawnIdeServerstops a live server recorded on a different port before starting.0600enforced on existing record/token files viachmodSync(thecloud-config.tspattern;writeFileSync({mode})only applies on creation).isBlockedPathfor/api/ide(blocked) vs/ide/(not) + the/api/ideaslookalike;isPublicRoutefor/ide/and/api/ide(routed-auth 401); connect-refused → 502.Architecture Updates
COLD — added an "IDE prefix forward (Issue #1668)" subsection under
## Integration Pointsincodev/resources/arch.md: the single-server +?folder=model, the HTTP vs raw-WS split, the post-authisForwardAuthorizedseam (#1589 swap point, no per-folder authz), the two auth boundaries (Tower key for the forward, connection token for direct-to-backend) and cookie injection, header hygiene, the local-only/api/ide, and the~/.agent-farm/ide-server.jsonrecord (no config file, no DB change).Not routed HOT: this is a self-contained new capability, not a fact a contributor must consult before unrelated cross-cutting work, so it does not warrant an always-injected entry (and the hot file is capped). The one always-on invariant it touches — "every non-public route rides the key choke point" — already exists in
arch-critical.md;/ide/conforms to it rather than changing it.Lessons Learned Updates
COLD — added to
codev/resources/lessons-learned.md:--without-connection-tokentrap); and a backend cookie scoped to its own base path won't survive a relay that remounts it (inject at the proxy, merging not clobbering).Connection/Upgradethat header hygiene strips, and forward the client'sSec-WebSocket-Key— caught only by a stub-loopback integration test through the real forward code, which the build and pure unit tests missed.Not routed HOT: the strongest cross-cutting lesson here ("verify the real user path end-to-end") already exists in
lessons-critical.md; these are spec-narrow proxy/auth recipes. This PR is itself a case study for that hot lesson — two defects (the WS hop-by-hop strip, the connection-token 403) were invisible to build+unit tests and surfaced only through integration and cloud runs.Things to Look At During PR Review
ide-forward.tsWS path: theConnection/Upgradere-add andSec-WebSocket-Keyforwarding, plus the 101-response reconstruction inrebuildUpgradeResponse.tower-websocket.ts, the IDE branch is checked before the genericisWebSocketAllowed(which requires a subprotocol key the workbench never offers). Confirm no non-IDE WS route can matchmatchIdePrefix.mergeConnectionTokenCookie): never-clobber semantics and array-valuedCookiehandling.tunnel-client.ts:/api/ideblocked,/ide/forward deliberately not.ensureIdeServerLiverespawn guard (single in-flight respawn) and the boot reconcile being fire-and-forget/post-readiness.How to Test Locally
pir-1668→ Review Diffvitest src/agent-farm/__tests__/ide-— the forward/auth/record/integration suites (stub loopback; no external systems).pnpm -w run local-installfrom the worktree) + any local server on:8200:afx ide start --server-path /bin/true(adopts the port) →afx ide status; keyed vs unkeyedcurl http://localhost:4100/ide/(200 vs 401);curl -H "codev-tower-key: $(cat ~/.agent-farm/local-key)" http://localhost:4100/api/ide./t/<tower>/ide/leg (verified above), because/ide/is key-authed and a local browser can't attach the key.Notes / Dependency
afx ide install) and its artifact-base-url knob.