Skip to content

feat(pty): propagate active sessionId and AGENT_TTY_ACTIVE to spawned processes - #166

Merged
ThomasK33 merged 2 commits into
coder:mainfrom
prmartinow:feat/propagate-session-id
Sep 24, 2026
Merged

ThomasK33 merged 2 commits into
coder:mainfrom
prmartinow:feat/propagate-session-id

Conversation

@prmartinow

@prmartinow prmartinow commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

This PR exposes the active session state in the environment of all PTY processes spawned by agent-tty.

Specifically, it injects:

  • AGENT_TTY_ACTIVE=true: A simple boolean flag indicating the command is running inside a managed session.
  • AGENT_TTY_SESSION_ID=<sessionId>: The UUID of the current terminal session.

Why this is useful:

It enables scripts, shell profiles, and automation utilities running inside the virtual terminal to:

  1. Detect the active session and avoid infinite recursion or double-wrapping when invoking shell wrappers.
  2. Reference their own session ID programmatically (e.g. to write markers or log annotations).
  3. Allow custom prompts or terminal styling to visibly indicate that a session is actively being recorded.

Code Changes

src/host/hostMain.ts

Passes sessionId from runHost into createPty options.

src/pty/createPty.ts

  1. Extends PtyOptions with an optional sessionId field.
  2. Updates resolvePtyEnv to accept an optional sessionId.
  3. Injects resolved.AGENT_TTY_ACTIVE = 'true' unconditionally.
  4. Injects resolved.AGENT_TTY_SESSION_ID = sessionId when a sessionId is provided.

test/unit/pty/createPty.test.ts

Adds Vitest unit tests verifying:

  • AGENT_TTY_ACTIVE is unconditionally set to 'true'.
  • AGENT_TTY_SESSION_ID is set to the provided value if present.
  • AGENT_TTY_SESSION_ID is undefined when no sessionId is passed.

Verification & Testing

Verify that all repository formats, lints, types, and tests pass:

npm run format:check
npm run lint
npm run typecheck
npm run test
npm run build

@prmartinow
prmartinow marked this pull request as ready for review July 11, 2026 15:30
@prmartinow
prmartinow force-pushed the feat/propagate-session-id branch 3 times, most recently from 3fd4363 to dc7679d Compare July 12, 2026 15:09
@prmartinow

Copy link
Copy Markdown
Contributor Author

@ThomasK33 could you please review

prmartinow and others added 2 commits September 24, 2026 10:15
… processes

This exposes AGENT_TTY_ACTIVE=true and AGENT_TTY_SESSION_ID=<sessionId> in the child PTY environment, allowing scripts and utilities inside the session to detect and inspect the active agent-tty context.
Inject AGENT_TTY_ACTIVE and AGENT_TTY_SESSION_ID after the inherited
environment but before the caller-supplied env, so an explicit
`create --env AGENT_TTY_ACTIVE=... / AGENT_TTY_SESSION_ID=...` wins as
docs/USAGE.md promises, while a nested session still replaces an id
inherited from its outer session.

Add unit tests for the override and inherited-id cases, an integration
test that matches the shell's id against the create envelope, and
document both variables in the resolvePtyEnv comment and USAGE.md.

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
@ThomasK33
ThomasK33 force-pushed the feat/propagate-session-id branch from 403d987 to 7b13952 Compare September 24, 2026 10:18
@ThomasK33
ThomasK33 added this pull request to the merge queue Sep 24, 2026
Merged via the queue into coder:main with commit 49e8c9c Sep 24, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants