Skip to content

fix(env): proxy directly to process.env instead of snapshotting#12822

Open
jerome-benoit wants to merge 17 commits intoanomalyco:devfrom
jerome-benoit:fix-env-caching-12698
Open

fix(env): proxy directly to process.env instead of snapshotting#12822
jerome-benoit wants to merge 17 commits intoanomalyco:devfrom
jerome-benoit:fix-env-caching-12698

Conversation

@jerome-benoit
Copy link
Copy Markdown
Contributor

@jerome-benoit jerome-benoit commented Feb 9, 2026

Issue for this PR

Closes #22451

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The Env service was snapshotting process.env at init time via InstanceState.make(() => Effect.succeed({ ...process.env })). This caused stale reads (Env.get() returned the init-time value, not the current one) and silent set() calls (only the internal copy was mutated, never process.env).

The fix replaces the InstanceState-based snapshot with a Layer.succeed that proxies directly to process.env via Effect.sync:

  • get(key)Effect.sync(() => process.env[key]) — live read
  • all()Effect.sync(() => ({ ...process.env })) — fresh shallow copy per call (prevents accidental mutation of process.env via the returned object)
  • set(key, value)Effect.sync(() => { process.env[key] = value }) — writes to process.env directly
  • remove(key)Effect.sync(() => { delete process.env[key] }) — deletes from process.env directly

The Env.Interface contract is unchanged — zero consumer modifications needed.

Additionally:

  • Removed 2 obsolete TODO workaround comments in provider.ts that documented the now-fixed bug
  • Added 5 missing env var cleanups in test/preload.ts (AICORE_*, AWS_CONTAINER_*) to prevent host env leaking into tests

How did you verify your code works?

  • bun typecheck passes across all 19 packages
  • Verified all env.all() / env.get() consumers in provider.ts, config.ts, and tool/registry.ts are read-only and unaffected by the change from snapshot to live proxy
  • Cross-validated that no production code depends on snapshot (frozen-at-init) semantics — all accesses are immediate reads, never stored or compared across time
  • Confirmed the 6 existing test env save/restore patterns (preload.ts, beforeEach/afterEach, try/finally, helper closures, Effect.acquireRelease, afterAll) all operate on process.env directly, independent of Env.Service

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Copilot AI review requested due to automatic review settings February 9, 2026 13:35
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 9, 2026

Hey! Your PR title Migrate all process.env to Env namespace for consistent caching (#12698) doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 9, 2026

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

This comment was marked as low quality.

@jerome-benoit jerome-benoit changed the title Migrate all process.env to Env namespace for consistent caching (#12698) fix(opencode): bypass Env cache in production to detect late-set env vars Feb 9, 2026
@jerome-benoit jerome-benoit force-pushed the fix-env-caching-12698 branch 6 times, most recently from ed0b5ac to 95a2dba Compare February 14, 2026 11:50
@jerome-benoit jerome-benoit changed the title fix(opencode): bypass Env cache in production to detect late-set env vars fix(env): remove Env namespace, use direct process.env access Feb 16, 2026
jerome-benoit added a commit to jerome-benoit/dotfiles that referenced this pull request Mar 2, 2026
… and bun provider tracking

Cherry-pick three upstream PRs:
- anomalyco/opencode#14958: add thinking variant support for SAP AI provider
- anomalyco/opencode#12822: remove Env namespace, use direct process.env access
- anomalyco/opencode#10275: track provider packages with reference counting for cleanup
@jerome-benoit jerome-benoit force-pushed the fix-env-caching-12698 branch from 6f2e3fe to e68d760 Compare April 14, 2026 12:57
@jerome-benoit jerome-benoit changed the title fix(env): remove Env namespace, use direct process.env access fix(env): proxy directly to process.env instead of snapshotting Apr 14, 2026
@jerome-benoit jerome-benoit force-pushed the fix-env-caching-12698 branch 6 times, most recently from 4c6de47 to b4233fc Compare April 16, 2026 21:20
@jerome-benoit jerome-benoit force-pushed the fix-env-caching-12698 branch from b4a22d2 to bf21d10 Compare April 27, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Env service snapshots process.env at init, causing stale reads and silent set()

2 participants