Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): Grok agent processes receive the thread's environment #34032

Closedjuliusmarmingewants to merge 3 commits intomain fromrefs/pull/34032/headopened View on GitHub

juliusmarmingecommented

The Grok adapter's default makeRuntime factory (packages/provider-grok/src/server/adapter.ts) passed only the provider instance's environment to grok agent stdio and dropped input.processEnvironment. Since https://git.cafe/pingdotgg/t3code/pulls/30361 that per-thread environment carries T3CODE_THREAD_ID and the T3_ACP_MCP_* variables. The fix takes processEnvironment out of the runtime input and merges it over the instance environment ({ ...options.environment, ...processEnvironment }), the same line the Fx and Kiro adapters use. The ACP registry adapter merges it the same way. Each Grok process T3 starts for a thread now has that thread's T3CODE_THREAD_ID and its MCP variables, so MCP servers and shell commands under two Grok threads can tell them apart. Auth-method detection reads the same merged environment, as it does in Fx and Kiro. No new regex or parser.

Fixes pingdotgg/t3code#34024

Reproduction on main

Commit d256170b34 (origin/main). Started vp run dev with T3CODE_DEV_SEED=0 and T3CODE_THREAD_ID unset in its environment (fresh database), added a scratch git project with t3 project add, enabled a grok provider instance in the worktree's dev settings and set its binaryPath to a wrapper script that records T3CODE_THREAD_ID, GROK_OAUTH2_REFERRER and the names of any T3_ACP_ vars, then runs the real grok. Started two Grok threads with t3 thread start --provider grok --model aurora-alpha <project> 'echo "TID=[$T3CODE_THREAD_ID]"'. Every grok agent [--always-approve] stdio process T3 spawned for both threads had GROK_OAUTH2_REFERRER=t3code but no T3CODE_THREAD_ID and no T3_ACP_MCP_ vars. Control on the same server: a Replay (dev) thread (generic ACP registry provider). Its agent process had T3CODE_THREAD_ID equal to its own thread id plus T3_ACP_MCP_ENDPOINT/AUTHORIZATION/ENTRYPOINT/NODE. A full Grok turn could not run on this VM (grok 1.0.77 offers only grok.com auth over ACP while T3 picked API-key auth), but that is unrelated to the spawn environment the bug is about. Failing test: 'launches the agent with the env that names the thread it serves' in apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts fails on main with expected undefined to equal 'grok-launch-thread-env'. With processEnvironment merged into Grok's spawn env (temporary, not committed), all 11 tests in the file pass.

Evidence

Run the same driver on both commits: start an isolated real dev server with seeding disabled and inherited T3CODE_THREAD_ID unset; register one synthetic git project; start two Grok threads sequentially with the same model and echo "TID=[$T3CODE_THREAD_ID]"; wait for each request to finish. A wrapper captures the actual spawn environment before executing the real Grok binary.

Environment on both sides: Linux x86_64, Grok 1.0.77, fresh databases, server/source CLI surface, 1280×900 dark terminal recordings rendered from timestamped output. Recordings were visually inspected.

BeforeAfter
d256170b34a6f0a6c9ea
Before recordingAfter recording
All 10 launches lack T3CODE_THREAD_ID and ACP MCP variables.All 10 launches have their own thread ID and all four ACP MCP variable names.
Transcript · Process recordsTranscript · Process records

GROK_OAUTH2_REFERRER remains t3code throughout. The two head threads receive distinct IDs. Both sides encounter the same ACP authentication-method mismatch after spawning; completed shell commands and MCP calls are not proven.

Driver · Grok wrapper · Seed script · Recording renderer · Reproduction instructions

AI-assisted; verified with the tests and tools listed above.

Not shown:

  • Completed Grok shell commands and downstream MCP calls: Grok 1.0.77 advertises grok.com ACP authentication while the server selects xai.api_key. Both commits fail with the same authentication-method mismatch after the actual spawn environments are captured. ACP credential values and endpoint functionality were not exercised.

Tests

vp test run apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts: before the fix 1 failed / 10 passed (the repro test failed with expected undefined to equal 'grok-launch-thread-env'); after the fix 11/11 passed. vp run check:changed: format, lint, knip, graphql, typecheck (10 packages) and tests (2 files) all passed.

Not in this change

  • The VM's global git config sets credential.https://git.cafe.usehttppath=true, but the git.cafe credential is stored without a path, so git push origin cannot find it. I pushed with a one-off -c credential.https://git.cafe.usehttppath=false and did not change the global config.

AI-assisted; verified with the tests and tools listed above.

    1. Pushed5757c81

    2. This patch passes thread environment variables to the retired grok agent stdio process. #46363 retired that integration in favor of Grok Build through the Cursor SDK, so this is no longer needed.

    3. juliusmarminge closed this

Sign in to join this conversation.