Repository navigation
scope client env to sessions so herdr extensions see the right pane #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e2d229c
pass herdr client env through the daemon scoped per session
kevinjosethomas 8349390
inherit client env in subagents and guard against watcher and cross-s…
kevinjosethomas 7373f99
bind client env once at session creation and never rebind on reuse
kevinjosethomas 33c30df
scope env around the daemon reload command and adopt env on env-less …
kevinjosethomas 289ff9e
propagate adopted client env to subagents spawned before adoption
kevinjosethomas 637a0a6
let the primary interactive attach adopt env for env-less sessions
kevinjosethomas 7b577bc
scope client env around all runtime rebuilds via a runtime env scope …
kevinjosethomas c924141
cover adopt-if-absent client env semantics on session reuse
kevinjosethomas 423de56
pin allowlisted exec env per session so foreign env windows cannot le…
kevinjosethomas 3a2b2a2
pin the full allowlist inside env windows so partial client env canno…
kevinjosethomas a39171b
Merge remote-tracking branch 'origin/main' into feat/herdr-env-per-se…
kevinjosethomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
67 changes: 67 additions & 0 deletions
67
packages/coding-agent/src/modes/daemon/daemon-client-env.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { DAEMON_CLIENT_ENV_KEYS } from "./daemon-protocol.js"; | ||
|
|
||
| /** Re-filter client-sent env to the allowlist; the socket peer is untrusted. */ | ||
| export function filterClientEnv(env?: Record<string, string>): Record<string, string> | undefined { | ||
| if (!env) { | ||
| return undefined; | ||
| } | ||
| const filtered: Record<string, string> = {}; | ||
| for (const key of DAEMON_CLIENT_ENV_KEYS) { | ||
| if (env[key] !== undefined) { | ||
| filtered[key] = env[key]; | ||
| } | ||
| } | ||
| return Object.keys(filtered).length > 0 ? filtered : undefined; | ||
| } | ||
|
|
||
| // Shared/exclusive lock: env windows are exclusive (they mutate process.env), | ||
| // env-less loads are shared — they run concurrently with each other but never | ||
| // inside an env window, so they can't capture another session's identity. | ||
| let lastExclusive: Promise<unknown> = Promise.resolve(); | ||
| const activeShared = new Set<Promise<unknown>>(); | ||
|
|
||
| /** | ||
| * Run fn with the client's env applied to process.env, restoring afterwards. | ||
| * Extensions capture vars like HERDR_PANE_ID synchronously at module load, so | ||
| * they must be in process.env while the session loads its extensions; after | ||
| * this window the session's exec env covers subprocess reads. | ||
| */ | ||
| export async function withClientEnv<T>(env: Record<string, string> | undefined, fn: () => Promise<T>): Promise<T> { | ||
| if (!env) { | ||
| const gate = lastExclusive; | ||
| const run = (async () => { | ||
| await gate.catch(() => undefined); | ||
| return fn(); | ||
| })(); | ||
| const tracked = run.catch(() => undefined); | ||
| activeShared.add(tracked); | ||
| void tracked.then(() => activeShared.delete(tracked)); | ||
| return run; | ||
| } | ||
| const prior = lastExclusive; | ||
| // Snapshot synchronously: shareds arriving later gate on this window via | ||
| // lastExclusive, so waiting for them here would deadlock. | ||
| const sharedAtRequest = [...activeShared]; | ||
| const run = (async () => { | ||
| await prior.catch(() => undefined); | ||
| await Promise.all(sharedAtRequest); | ||
| const previous = new Map<string, string | undefined>(); | ||
| for (const [key, value] of Object.entries(env)) { | ||
| previous.set(key, process.env[key]); | ||
| process.env[key] = value; | ||
| } | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Resolved
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| for (const [key, value] of previous) { | ||
| if (value === undefined) { | ||
| delete process.env[key]; | ||
| } else { | ||
| process.env[key] = value; | ||
| } | ||
| } | ||
| } | ||
| })(); | ||
| lastExclusive = run.catch(() => undefined); | ||
| return run; | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.