Skip to content

Test-spawned Towers inherit real ~/.agent-farm cloud identity — suite runs deregister the user's production Tower from Codev Cloud #1515

Closed

Description

@waleedkadous

Impact (observed in production, 2026-08-17/18)

Running the codev test suite while a cloud-connected Tower is up disconnects the user's real cloud tunnel and deregisters their Tower, silently, until a human manually reconnects. Last night's sequence, from ~/.agent-farm/tower.log:

  • 23:49:43Z — during a builder's full-suite run: Tunnel connect requested (via=local ua="node") ×3 + one Tunnel disconnect requested, then Server-side deregister succeeded for tower 01KZ…, then the production Tower logs Cloud config removed or invalid, disconnecting tunnel.
  • Tunnel stays down 6.5 hours until the owner manually reconnects at 06:14Z. Downstream consumers of the cloud connection (e.g. the Stream Deck integration) broke for the whole window.
  • Further identical bursts at 03:02–03:13Z during another builder's suite runs (config already gone by then, so no further state transitions).

This is a recurrence of the "disconnect saga" (#1370/#1372 era) — and the via= attribution logging shipped for it in 3.3.0 is what finally named the actor: our own test suite.

Root cause

packages/codev/src/agent-farm/__tests__/helpers/tower-test-utils.ts spawns a real tower-server child with:

env: { ...process.env, NODE_ENV: 'test', AF_TEST_DB: `test-${actualPort}.db`, SHELLPER_SOCKET_DIR: socketDir, ... }

The DB and shellper sockets are isolated; ~/.agent-farm is not. The spawned Tower therefore:

  1. Reads the production cloud config — real tower ID and token. A test exercising /api/tunnel/connect/disconnect acts as the user's registered Tower: disconnect performs a server-side deregister of the production tower ID and deletes the production cloud config file.
  2. The real Tower's config watcher notices the deletion and drops its live tunnel.
  3. Writes to the shared ~/.agent-farm/tower.log, interleaving test-tower lines with production lines — which is why the earlier forensics (this saga's "unidentified actor") never converged: the evidence was two processes writing one file.

Prescribed fix (BUGFIX-shaped)

  1. Isolate the agent-farm dir per spawned test Tower — point the child at a mkdtemp'd dir (whatever mechanism tower-server honors: AGENT_FARM_DIR env or equivalent; add the override if none exists). No test Tower may ever see the real cloud config, key, log, or global.db.
  2. Defense in depth: under NODE_ENV=test, tower-server refuses cloud side effects (server-side deregister, cloud-config deletion) unless explicitly opted in by a test that owns a fake config. A leaky test should fail loudly, not deregister a human.
  3. Regression test: spawn a test Tower with a canary cloud config in the fake dir + a canary in a real-shaped path; drive connect/disconnect; assert the real-shaped canary is untouched and the log landed in the isolated dir.
  4. Sweep other spawn sites (e2e helpers, vitest-e2e setup) for the same ...process.env inheritance.

Evidence pointers

  • ~/.agent-farm/tower.log lines around 2026-08-17T23:49:43Z and 2026-08-18T03:02–03:13Z.
  • Suite-run timing correlates with builder-air-1489 (spawned 23:42Z, suite before its 00:04Z report) and builder-spir-1470's Phase 2 iterations.

Activity

  1. added
    area/tower
    on Aug 18, 2026
  2. waleedkadous commented on Aug 18, 2026

    @waleedkadous
    ContributorAuthor

    On it! Working on a fix now.

  3. waleedkadous commented on Aug 18, 2026

    @waleedkadous
    ContributorAuthor

    Root-cause correction from the bugfix investigation (verified by A/B repro, not statically):

    The actor is packages/codev/src/agent-farm/__tests__/tower-cloud-cli.test.ts — a unit test in the default suite, not an e2e helper. towerRegister()/towerDeregister() invoked with no port option resolve getTowerClient() to localhost:4100 — the live production Tower — authenticated with the real ~/.agent-farm/local-key. Its 3-connects-then-1-disconnect shape matches the incident log exactly (guard off → 4 tunnel lines in the production log; guard on → 0).

    Two corrections to this issue's original root-cause section:

    1. The tunnel log lines were all one process — the production Tower serving real requests. Test-spawned Towers never write tower.log (no --log-file), so the "two processes sharing the log" theory was wrong.
    2. Prescribed fix Context Window Optimization - AI Needs Project Knowledge Without Breaking Limits #2 (a server-side NODE_ENV=test guard) would not have prevented this — the victim Tower is the production one, running without any test env. The effective fix is the client-side guard the builder added, plus the test-env isolation from fix The Gestalt Problem - Features Developed in Isolation Without System Awareness #1.
  4. added 6 commits that reference this issue on Aug 18, 2026
    af9cf3c
    48506b8
    81f4713
    fe2af87
    b1b8d87
    3d337d8
  5. added a commit that references this issue on Aug 18, 2026
    f73f1f5
  6. waleedkadous commented on Aug 18, 2026

    @waleedkadous
    ContributorAuthor

    Completion stats — bugfix-1515

    Fixed in #1516 (merged 2026-08-18T15:33:39Z, merge commit f73f1f5). Protocol phase: verified.

    Timings

    Investigate → PR open 43.6 min
    Start → merge 9.2 h (8.5 h of that awaiting the human gate)
    Iterations 1 (no rollbacks, no escalation)

    Change size

    Added Removed
    Production code (4 files) 131 1
    Tests + harness (11 files) 502 22
    Thread + status 300 0

    Production change is well inside the BUGFIX ceiling: constants.ts, test-env.ts, tower-client.ts, tower-tunnel.ts.

    Consultation

    7 invocations, 15.9 min, $6.83, 100% success — 3 CMAP rounds.

    Round Gemini Codex Claude
    1 APPROVE COMMENT REQUEST_CHANGES
    2 APPROVE REQUEST_CHANGES APPROVE
    3 — COMMENT —

    Both REQUEST_CHANGES were correct and both were mine to own:

    • Claude R1 (blocking): isolating the DB broke tower-routes-husks.e2e.test.ts and shellper-husk-sweep.e2e.test.ts, which read the DB at the old ~/.agent-farm path. It also caught that my "all tests pass" covered 5 Tower-spawning e2e files when 12 use the helper I changed — 2 of the 7 I skipped were the broken ones.
    • Codex R2 (blocking): I had added codev/reviews/bugfix-1515-*.md. The BUGFIX protocol says "no artifact files: the issue is the specification and the PR body carries the reasoning." Removed.

    Verification

    Configuration Tunnel lines in the live tower.log
    Pre-fix (guard off, test unpinned) 4 — 3× connect, 1× disconnect
    Guard on 0
    Guard off, test port pinned 0

    Both layers hold independently. The e2e regression was confirmed to fail without the fix with the production symptom (expected 'canary-real-tower-id' to be 'isolated-test-tower-id').

    Final: unit suite green (4929 tests); all 14 Tower-spawning e2e files green (85 tests); zero leftover credential dirs.

    Correction to this issue's root-cause section

    Recorded because the stated mechanism would have left the bug live:

    1. "Writes to the shared ~/.agent-farm/tower.log, interleaving test-tower lines with production lines" — false. tower-server.ts writes a log only when --log-file is passed, which no test spawner does. The lines interleave with production mailbox/cron lines in a single process.
    2. "Test-spawned Towers inherit real cloud identity" — real leak (test-141*.db were sitting in ~/.agent-farm), but not the actor. The actor was tower-cloud-cli.test.ts, a unit test in the default suite, whose towerRegister()/towerDeregister() calls pass no port and so command the live Tower on 4100 with the real local key.

    Consequently prescribed fix #2 (server-side NODE_ENV=test guard) would not have prevented this — the Tower being commanded runs with neither VITEST nor NODE_ENV=test. The client-side guard was the missing piece.

    Collateral

    Two further tunnel drops at 06:29:36Z and 06:31:15Z were caused by my own pre-fix targeted runs (tower 01M09R4DAMRMYHZ523475WVKVR deregistered). That is what named the actor: 06:29:36Z is the exact start timestamp of the tower-cloud-cli.test.ts run. Owner reconnect was required.

    Follow-up filed as #1517 (the guard is on the CLI TowerClient subclass, so apps/vscode's signalTunnel path is unguarded).

    Full narrative: codev/state/bugfix-1515_thread.md.

  7. added a commit that references this issue on Aug 18, 2026
  8. added 2 commits that reference this issue on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/tower

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions