Skip to content

Unify launch env resolution on the server + introduce project/worktree/thread env vars#3004

Open
tarik02 wants to merge 19 commits into
pingdotgg:mainfrom
tarik02:unify-launch-env
Open

Unify launch env resolution on the server + introduce project/worktree/thread env vars#3004
tarik02 wants to merge 19 commits into
pingdotgg:mainfrom
tarik02:unify-launch-env

Conversation

@tarik02

@tarik02 tarik02 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

What Changed

Server now owns T3CODE_* launch env for terminal and provider child processes.

  • Added LaunchEnv to resolve home, project, thread, and worktree context from server state.
  • Terminals resolve launch env on open/restart/attach, including draft-thread opens with a supplied projectId.
  • Provider sessions use the same env path, with managed T3CODE_* keys stripped from inherited and client-supplied env.
  • Web/mobile stop synthesizing T3CODE_*; they send cwd/worktree context plus projectId when needed.

Closes #3003

Why

Make t3code provide T3CODE_PROJECT_ROOT / T3CODE_WORKTREE_PATH (and related launch context) from one authoritative server path. Unified environment resolution between terminal and provider child processes, with managed-key filtering so parent/custom/client env cannot override or leak extra T3CODE_* values.

Знімок екрана 2026-06-10 о 21 02 28

UI Changes

No UI changes.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (N/A)
  • I included a video for animation/interaction changes (N/A)

Note

Unify launch environment resolution on the server and introduce project/worktree/thread env vars

  • Introduces a new LaunchEnv DI service (LaunchEnv.ts) that resolves T3CODE_HOME, T3CODE_PROJECT_ROOT, T3CODE_PROJECT_ID, T3CODE_THREAD_ID, and T3CODE_WORKTREE_PATH from server config and projection queries, replacing ad-hoc client-side env construction.
  • TerminalManager.open, .restart, and .attach now pre-resolve worktreePath and env via LaunchEnv before spawning, mapping lookup failures to TerminalCwdError or TerminalSessionLookupError.
  • ProviderCommandReactor passes a LaunchEnv-derived env map to providerService.startSession, so provider sessions (Claude, Codex, Cursor, Grok, OpenCode) receive authoritative context env.
  • All provider adapters now merge per-session env via mergeProviderSessionEnvironment, which strips managed T3CODE_* keys from the base process env before applying overrides.
  • Client-side code (ChatView, ThreadTerminalDrawer, ProjectSetupScriptRunner, mobile ThreadRouteScreen) no longer builds or sends runtimeEnv; only a projectId is forwarded and managed keys are stripped from any custom env before transmission.
  • isManagedRuntimeEnvKey and stripManagedRuntimeEnvKeys are added to @t3tools/shared/launchEnv and used throughout to enforce the boundary between managed and user-defined env keys.
  • Risk: TerminalManager now requires a LaunchEnv provider in its options; existing callers that do not provide one will fail to compile.

Macroscope summarized f189f37.


Note

Medium Risk
Touches terminal spawn, provider child processes, and env merging across adapters; wrong resolution could break scripts/agents, but behavior is covered by new LaunchEnv and integration tests.

Overview
Server-authoritative T3CODE_* launch env replaces client-built runtime env for terminals, setup scripts, and provider sessions.

A new LaunchEnv service resolves T3CODE_HOME, project root/id, thread id, and worktree path from ServerConfig and projection snapshots. resolveForThread loads thread/project from the server (ignoring client projectId when the thread exists) and only accepts client projectId for draft threads not yet in the projection.

TerminalManager resolves env on open/restart/attach before PTY spawn; clients send projectId, cwd, and worktree path instead of full env. ProviderCommandReactor passes LaunchEnv-derived env into startSession; adapters merge it via mergeProviderSessionEnvironment.

Managed-key policy: @t3tools/shared/launchEnv adds stripManagedRuntimeEnvKeys / isManagedRuntimeEnvKey; server merges strip client T3CODE_* overrides and provider spawns strip inherited managed keys. projectScriptRuntimeEnv is removed from shared; web/mobile only forward non-managed custom env when needed.

Contracts add optional projectId on terminal open/restart/attach and optional env on provider session start. Effect layer wiring pulls LaunchEnvLive into terminal, reactors, and integration harnesses.

Reviewed by Cursor Bugbot for commit f189f37. Bugbot is set up for automated code reviews on this repo. Configure here.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: fda8f6bd-4f87-4e6e-8282-393433b5aa50

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:XL 500-999 changed lines (additions + deletions). labels Jun 8, 2026
Comment thread apps/server/src/terminal/resolveTerminalLaunchEnv.ts Outdated
Resolve authoritative T3CODE_* vars server-side for terminals and provider sessions, strip inherited runtime keys from spawns, and simplify clients to pass only cwd/thread context. Consolidate managed env helpers in shared and collapse terminal launch env wiring.

Co-authored-by: Cursor <cursoragent@cursor.com>
@tarik02 tarik02 force-pushed the unify-launch-env branch from f456ef5 to 767954b Compare June 9, 2026 16:24
@tarik02 tarik02 marked this pull request as ready for review June 9, 2026 16:39
Comment thread apps/server/src/terminal/resolveTerminalLaunchEnv.ts Outdated
@macroscopeapp

macroscopeapp Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs human review

This PR introduces a new server-side LaunchEnv service that centralizes environment variable resolution, changing where and how runtime context (project, thread, worktree) is propagated to terminals and AI providers. This architectural change affects multiple subsystems and warrants human review.

You can customize Macroscope's approvability policy. Learn more.

@tarik02 tarik02 marked this pull request as draft June 9, 2026 17:19
tarik02 and others added 11 commits June 9, 2026 20:33
Resolve launch env at RPC time instead of layer construction, and derive project context from threadId server-side so terminal open/restart no longer trust client projectId.

Co-authored-by: Cursor <cursoragent@cursor.com>
Allow optional client projectId when the thread is not yet persisted, while still resolving project context from the server thread when it exists.

Co-authored-by: Cursor <cursoragent@cursor.com>
Bind the terminal launch-env resolver at manager construction without unsafe casts, strip managed T3CODE_* keys in ACP spawns, merge per-session env for Grok, and pass projectId from setup scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
LaunchEnv no longer explicitly provides OrchestrationProjectionSnapshotQuery and
RepositoryIdentityResolver since these dependencies are already available through
the main application's OrchestrationLayerLive context.

Changes:
- LaunchEnvLayerLive now directly exports LaunchEnvLive (simplified from factory)
- Dependencies auto-wire via Effect's context propagation
- Removed makeLaunchEnvLayerLive factory which was building redundant layers
- Updated ProviderCommandReactor.test to use simplified LaunchEnvLayerLive
- Cleaner layer composition with no duplicate OrchestrationProjectionSnapshotQuery provision

This reduces PR scope from 154 changed files to 3, maintaining the same functionality
while improving architectural clarity by eliminating unnecessary layer wrapping.

Co-authored-by: Cursor <cursoragent@cursor.com>
… line

- Delete LaunchEnvLive.ts from Layers/ - it was just re-exporting LaunchEnvLive
  from Services/LaunchEnv.ts with no additional composition or transformation
- Update imports to get LaunchEnvLive directly from Services/LaunchEnv.ts
- Fix duplicate line in OrchestrationEngineHarness.integration.ts (378-379)
- Cleaner architecture: no unnecessary layer wrapper files

This simplifies the LaunchEnv module structure - the Services file already exports
the Layer, so an intermediate Layers file added no value and just increased indirection.

Co-authored-by: Cursor <cursoragent@cursor.com>
Follow app architecture pattern: Service files define only interface and class,
implementation and Layer creation live in Layers/ files.

Changes:
- Services/LaunchEnv.ts: Contract only (Shape, types, service class with JSDoc)
- Layers/LaunchEnvLive.ts: Implementation (makeResolveLaunchEnv, makeResolveForThread, Layer)
- Update imports in server.ts, ProviderCommandReactor.test.ts, OrchestrationEngineHarness

This matches the architectural pattern used throughout the codebase (e.g., OrchestrationEngine,
ProviderService) where Shape and class live in Services/ and concrete implementations live in Layers/.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread apps/server/src/launchEnv/Layers/LaunchEnvLive.ts Outdated
@github-actions github-actions Bot added size:XXL 1,000+ changed lines (additions + deletions). and removed size:XL 500-999 changed lines (additions + deletions). labels Jun 10, 2026
Comment thread apps/web/src/cloud/linkEnvironment.ts Outdated
Comment thread apps/web/src/cloud/linkEnvironment.ts Outdated
@github-actions github-actions Bot added size:XL 500-999 changed lines (additions + deletions). and removed size:XXL 1,000+ changed lines (additions + deletions). labels Jun 10, 2026
@tarik02 tarik02 changed the title Unify launch env resolution on the server. Unify launch env resolution on the server + introduce project/worktree/thread env vars Jun 10, 2026
@tarik02 tarik02 marked this pull request as ready for review June 10, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL 500-999 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Expose current thread info via environment variables

1 participant