Superseded by
docs/superpowers/plans/2026-06-23-session-shell-environment-owner.md. This document records the original shell snapshot implementation plan; the current refactor plan centralizes lifecycle ownership insession-shell-environment.ts.For agentic workers: REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: Unify Bash tool shell resolution, provider-visible environment text, Explore subagent execution, and mid-conversation shell-change notifications around one effective Bash shell snapshot.
Architecture: Introduce a session-scoped Effective Bash Shell descriptor that is resolved once at session create/resume and reused by context assembly, Bash execution, and Explore subagents. Runtime shell changes update the execution descriptor immediately and append a provider-visible mid-conversation system notice instead of rewriting the initial # Environment system context.
Tech Stack: TypeScript, Vitest, @zcode/contracts, @zcode/core, @zcode/adapters, ZCode Protocol session runtime.
# Environment messages for active-session shell changes.EnvInfo.shell represents the effective Bash shell snapshot at session create/resume time.EnvInfo.shell, Explore prompt, and Explore child runtime must consume the same effective shell descriptor.$SHELL echo semantics.pnpm typecheck and pnpm lint before completion.Current Bash shell state has multiple sources of truth:
EnvInfo.shell is detected from SHELL ?? ComSpec ?? COMSPEC in apps/zcode-cli/packages/adapters/src/context/index.ts.apps/zcode-cli/packages/adapters/src/exec/index.ts via shellProfile: "posix-bash" and optional bashShellOverride.envInfo but does not explicitly inherit bashShellOverride.updateConfig({ bashShellOverride }) changes future execution without notifying the model.# Environment on shell change would cause a provider-visible system prompt mutation and likely prompt-cache loss.The intended end state is:
apps/zcode-cli/packages/contracts/src/interfaces/execution.port.ts
apps/zcode-cli/packages/contracts/src/interfaces/context-source.port.ts
apps/zcode-cli/packages/adapters/src/exec/bash-shell-provider.ts
apps/zcode-cli/packages/adapters/src/exec/index.ts
apps/zcode-cli/packages/adapters/src/context/index.ts
apps/zcode-cli/packages/core/src/system-reminder/source.ts
apps/zcode-cli/packages/core/src/runtime/types.ts
apps/zcode-cli/packages/core/src/runtime/methods/context.ts
apps/zcode-cli/packages/core/src/runtime/methods/config.ts
apps/zcode-cli/packages/core/src/runtime/methods/subagent.ts
apps/zcode-cli/packages/core/src/tool/handlers/bash.ts
apps/zcode-cli/packages/core/src/tool/executor/types.ts, apps/zcode-cli/packages/core/src/tool/executor/call-runner.ts, apps/zcode-cli/packages/core/src/tool/executor/impl.ts
apps/zcode-cli/packages/bootstrap/src/zcode-protocol/server-operations.ts
apps/zcode-cli/packages/adapters/tests/exec.test.tsapps/zcode-cli/packages/adapters/tests/fs-context.test.tsapps/zcode-cli/packages/core/tests/system-reminder-source.test.tsapps/zcode-cli/packages/core/tests/context-builder.test.tsapps/zcode-cli/packages/core/tests/runtime-trace.test.tsapps/zcode-cli/packages/core/tests/subagent-explore.test.tsapps/zcode-cli/packages/core/tests/bash-handler.test.tsapps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.tsapps/zcode-cli/packages/bootstrap/tests/session-persistence.test.tsFiles:
apps/zcode-cli/packages/contracts/src/interfaces/execution.port.tsInterfaces:
ExecutionShellSelection, ExecutionShellSource, ExecutionShellDisplayConsumes: existing ExecutionShellDialect
[ ] Step 1: Add the descriptor types
Add these types next to ExecutionShellDialect:
export type ExecutionShellSource = "auto-detected" | "user-config" | "legacy-fallback";
export interface ExecutionShellDisplay {
/**
* Stable provider-visible shell name. Never include absolute paths here.
* Examples: "bash", "zsh", "Git Bash", "CMD", "system shell".
*/
name: string;
}
export interface ExecutionShellSelection {
/** Stable id from UI/settings or resolver-generated auto id. */
id?: string;
/** Human-readable diagnostic label. This can include more detail than display.name. */
label?: string;
/** Executable path when ZCode resolved a concrete shell. Omitted for legacy shell fallback. */
path?: string;
/** Shell syntax and cwd capture wrapper semantics. */
dialect: ExecutionShellDialect | "legacy-shell";
/** Why this selection exists. */
source: ExecutionShellSource;
/** Provider-visible stable name. */
display: ExecutionShellDisplay;
}
Replace ExecutionShellOverride with a compatibility alias:
export type ExecutionShellOverride = ExecutionShellSelection;
Then update the comment above ExecutionCommand.shellOverride so it reads:
/**
* ZCode runtime-provided Bash shell selection. Current consumers must only use
* this when shellProfile === "posix-bash"; generic shell execution must ignore it.
*/
shellOverride?: ExecutionShellSelection;
Run:
pnpm --filter @zcode/contracts typecheck
Expected: typecheck passes or fails only at downstream call sites that still expect source: "user-config" only. Those downstream failures are addressed in later tasks.
Files:
apps/zcode-cli/packages/adapters/src/exec/bash-shell-provider.tsapps/zcode-cli/packages/adapters/src/exec/index.tsapps/zcode-cli/packages/adapters/tests/exec.test.tsInterfaces:
ExecutionShellSelectionProduces: resolveEffectiveBashShellSelection(options), createShellProviderCommand(...) continues to consume the chosen provider
[ ] Step 1: Add resolver result shape
In bash-shell-provider.ts, add:
export interface EffectiveBashShellResolution {
selection: ExecutionShellSelection;
provider?: BashShellProvider;
}
Add a new exported function:
export function resolveEffectiveBashShellSelection(options: {
env: NodeJS.ProcessEnv;
platform: NodeJS.Platform | string;
exists?: ExecutableCheck;
override?: ExecutionShellSelection;
}): EffectiveBashShellResolution {
if (options.platform === "win32") {
return resolveEffectiveWindowsBashShellSelection(options);
}
return resolveEffectivePosixBashShellSelection(options);
}
Implementation rules:
git-bash override with executable path returns source: "user-config", dialect: "git-bash", display.name: "Git Bash", and a Git Bash provider.cmd override returns source: "user-config", dialect: "cmd", display.name: "CMD", and a CMD provider.source: "auto-detected", dialect: "git-bash", display.name: "Git Bash", and a Git Bash provider.source: "legacy-fallback", dialect: "legacy-shell", display.name: "system shell", and no provider so the existing generic shell fallback remains available.source: "auto-detected", dialect: "posix", display.name: "bash" or "zsh", and a provider.POSIX with no bash/zsh returns source: "legacy-fallback", dialect: "legacy-shell", display.name: "system shell", and no provider.
[ ] Step 3: Make execution consume the descriptor
In apps/zcode-cli/packages/adapters/src/exec/index.ts, replace the separate Windows/POSIX resolver branches with:
if (command.shellProfile === "posix-bash") {
const resolution = resolveEffectiveBashShellSelection({
env,
exists: options.exists,
override: command.shellOverride,
platform,
});
if (resolution.provider) {
return createShellProviderCommand(resolution.provider, command.command);
}
}
Leave the existing generic shell fallback unchanged after this block.
In apps/zcode-cli/packages/adapters/tests/exec.test.ts, add tests for:
SHELL=/bin/fish with /usr/bin/zsh executable resolves display.name === "zsh" and uses /usr/bin/zsh.legacy-fallback and execution still falls through to generic shell.display.name === "Git Bash".Windows explicit CMD override resolves display.name === "CMD" even when Git Bash also exists.
[ ] Step 5: Run adapter tests
Run:
pnpm --filter @zcode/adapters exec vitest run tests/exec.test.ts
Expected: all adapter exec tests pass.
Files:
apps/zcode-cli/packages/contracts/src/interfaces/context-source.port.tsapps/zcode-cli/packages/adapters/src/context/index.tsapps/zcode-cli/packages/core/src/runtime/types.tsapps/zcode-cli/packages/core/src/runtime/methods/context.tsapps/zcode-cli/packages/adapters/tests/fs-context.test.tsapps/zcode-cli/packages/core/tests/runtime-trace.test.tsInterfaces:
ExecutionShellSelection.display.nameProduces: stable EnvInfo.shell from the effective snapshot
[ ] Step 1: Extend context source request
In ContextSourceRequest, add:
effectiveShellDisplayName?: string;
In NodeContextSourceAdapter.detectEnvInfo, accept an optional display name parameter:
private async detectEnvInfo(
workingDirectory: string,
effectiveShellDisplayName?: string,
): Promise<EnvInfo> {
const shellPath = this.env.SHELL ?? this.env.ComSpec ?? this.env.COMSPEC ?? "";
const shell = effectiveShellDisplayName ?? (shellPath ? basename(shellPath) : "unknown");
...
}
Then call:
const envInfo =
request.envInfo ??
(await this.detectEnvInfo(workingDirectory, request.effectiveShellDisplayName));
In AgentRuntimeConfig, add:
bashShellSelection?: ExecutionShellSelection | undefined;
Keep bashShellOverride only as a temporary compatibility field if needed by existing tests; new runtime code should read bashShellSelection.
In initializeContext / context source resolution, pass:
effectiveShellDisplayName: this.config.bashShellSelection?.display.name,
This makes initial # Environment Shell: a session-start snapshot.
Add a runtime trace test where:
SHELL=/bin/fishbashShellSelection.display.name === "zsh"contextSourceSnapshot.envInfo.shell is "zsh"Add an adapter context test where no effectiveShellDisplayName is provided and the legacy env-based behavior remains unchanged.
Run:
pnpm --filter @zcode/adapters exec vitest run tests/fs-context.test.ts
pnpm --filter @zcode/core exec vitest run tests/runtime-trace.test.ts
Expected: new tests pass and existing context behavior remains covered.
Files:
apps/zcode-cli/packages/core/src/system-reminder/source.tsapps/zcode-cli/packages/core/tests/system-reminder-source.test.tsapps/zcode-cli/packages/core/src/runtime/helpers/provider-request-messages.ts existing tests or nearest provider-request helper test fileInterfaces:
shell_environment_change sourceConsumes: existing mid-conversation system projection
[ ] Step 1: Add source
Add "shell_environment_change" to SYSTEM_REMINDER_PERSISTED_SOURCES.
Add descriptor:
shell_environment_change: descriptor(
"mid_turn_event",
"mid_turn_event",
true,
"sr.shell_environment_change",
),
Do not add it to NON_MID_CONVERSATION_SYSTEM_SOURCES.
This lets provider projection create a true mid-conversation role: "system" message when the provider-visible ordering is legal, and fallback to <system-reminder> user text only when needed.
In system-reminder-source.test.ts, assert:
expect(isMidConversationSystemSource("shell_environment_change")).toBe(true);
expect(getSystemReminderDescriptor("shell_environment_change")).toMatchObject({
channel: "mid_turn_event",
lifecycle: "mid_turn_event",
isMeta: true,
providerVisibility: "provider_visible",
});
Run:
pnpm --filter @zcode/core exec vitest run tests/system-reminder-source.test.ts
Expected: source classification passes.
Files:
apps/zcode-cli/packages/core/src/runtime/types.tsapps/zcode-cli/packages/core/src/runtime/internal-methods.tsapps/zcode-cli/packages/core/src/runtime/methods/config.tsapps/zcode-cli/packages/bootstrap/src/zcode-protocol/server-operations.tsapps/zcode-cli/packages/core/tests/runtime-trace.test.tsapps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.tsInterfaces:
ExecutionShellSelectionProduces: runtime method updateBashShellSelection(selection, options)
[ ] Step 1: Add runtime method
Add an internal/runtime method with this shape:
updateBashShellSelection(
selection: ExecutionShellSelection | undefined,
options?: { notifyModel?: boolean },
): void;
Implementation logic:
const previousKey = shellSelectionCacheKey(this.config.bashShellSelection);
const nextKey = shellSelectionCacheKey(selection);
this.config.bashShellSelection = selection;
this.config.bashShellOverride = selection;
if (options?.notifyModel === true && previousKey !== nextKey && selection) {
this.messageHistory.addAttachment(
"shell_environment_change",
buildShellEnvironmentChangeReminder(selection),
);
}
shellSelectionCacheKey should use stable fields:
function shellSelectionCacheKey(selection: ExecutionShellSelection | undefined): string {
if (!selection) return "auto:none";
return [
selection.source,
selection.dialect,
selection.display.name,
selection.path ?? "",
].join("\0");
}
buildShellEnvironmentChangeReminder should not include absolute paths:
function buildShellEnvironmentChangeReminder(selection: ExecutionShellSelection): string {
return `The Bash tool shell was changed by the user. Future Bash commands will run using ${selection.display.name}.`;
}
updateConfig from refreshing contextRemove direct bashShellOverride mutation from generic updateConfig callers where possible. If backward compatibility requires keeping the field, make it delegate to updateBashShellSelection(patch.bashShellOverride, { notifyModel: false }).
Do not call refreshContextModelSnapshot() from shell updates.
In applyIntegratedTerminalShellToRecord, call:
record.app.runtime.updateBashShellSelection(
integratedTerminalShellToExecutionSelection(selection),
{ notifyModel: true },
);
For session create/resume, call the same runtime path with { notifyModel: false } through initial runtime config.
Add tests that prove:
shell_environment_change attachment.Changing shell does not mutate existing # Environment Shell: text.
[ ] Step 6: Run tests
Run:
pnpm --filter @zcode/core exec vitest run tests/runtime-trace.test.ts
pnpm --filter @zcode/bootstrap exec vitest run tests/zcode-protocol.test.ts
Expected: shell update is model-visible through an added mid-conversation system notice and does not refresh the context prefix.
Files:
apps/zcode-cli/packages/core/src/tool/types.tsapps/zcode-cli/packages/core/src/tool/handlers/bash.tsapps/zcode-cli/packages/core/src/tool/executor/types.tsapps/zcode-cli/packages/core/src/tool/executor/call-runner.tsapps/zcode-cli/packages/core/src/tool/executor/impl.tsapps/zcode-cli/packages/core/src/runtime/agent-runtime.tsapps/zcode-cli/packages/core/tests/bash-handler.test.tsInterfaces:
bashShellSelectionProduces: Bash execution request with command.shellOverride = bashShellSelection
[ ] Step 1: Rename execution dependency getters
Introduce getBashShellSelection next to the current getBashShellOverride path:
getBashShellSelection?: () => ExecutionShellSelection | undefined;
Use it first, then fallback to old getBashShellOverride while compatibility remains.
In Bash tool context types, add:
bashShellSelection?: ExecutionShellSelection;
Keep bashShellOverride as a compatibility alias during the migration.
createExecutionRequestUse the effective selection:
const shellSelection = context.bashShellSelection ?? context.bashShellOverride;
...
...(shellSelection ? { shellOverride: shellSelection } : {}),
Add a test that runtime-provided bashShellSelection is passed into the execution request.
Add a test that missing bashShellSelection keeps current auto-resolution behavior.
Run:
pnpm --filter @zcode/core exec vitest run tests/bash-handler.test.ts
Expected: Bash handler passes the effective shell selection when present and preserves fallback behavior when absent.
Files:
apps/zcode-cli/packages/core/src/runtime/methods/subagent.tsapps/zcode-cli/packages/core/tests/subagent-explore.test.tsInterfaces:
bashShellSelectionProduces: child runtime with matching envInfo.shell and matching Bash execution selection
[ ] Step 1: Use parent shell display in child env
When building childEnvInfo, prefer:
const childEnvInfo = {
...(this.contextSourceSnapshot?.envInfo ?? this.config.envInfo ?? fallbackEnvInfo),
...(this.config.bashShellSelection
? { shell: this.config.bashShellSelection.display.name }
: {}),
};
In child runtime config, add:
bashShellSelection: this.config.bashShellSelection,
bashShellOverride: this.config.bashShellSelection ?? this.config.bashShellOverride,
Add a test where parent has bashShellSelection.display.name === "Git Bash" and Explore prompt contains:
Shell: Git Bash
Add a test that child runtime Bash execution receives the same selection.
Run:
pnpm --filter @zcode/core exec vitest run tests/subagent-explore.test.ts
Expected: Explore prompt and child Bash execution no longer diverge from the parent session.
Files:
apps/zcode-cli/packages/core/src/runtime/methods/resume.tsapps/zcode-cli/packages/core/src/runtime/methods/message-persistence.tsapps/zcode-cli/packages/core/src/agent/session-history-hydrator.tsapps/zcode-cli/packages/bootstrap/tests/session-persistence.test.tsInterfaces:
EnvInfo.shell and shell_environment_change attachmentsProduces: resumed sessions that retain the original environment snapshot plus later shell-change notices
[ ] Step 1: Keep persisted EnvInfo.shell immutable on resume
Do not recompute the initial EnvInfo.shell when persisted context snapshot already exists. The resumed initial snapshot remains what the session started with.
Because shell_environment_change is a known persisted system reminder source, existing synthetic attachment hydration should preserve it. Add a test that a persisted shell-change notice hydrates back into messageHistory.addAttachment("shell_environment_change", ...).
Test this timeline:
Shell: Git Bash.CMD; runtime appends shell_environment_change.Shell: Git Bash.CMD.Run:
pnpm --filter @zcode/bootstrap exec vitest run tests/session-persistence.test.ts
Expected: resume keeps chronological truth instead of rewriting the initial environment.
Files:
apps/zcode-cli/packages/adapters/src/exec/bash-shell-provider.tspackages/services/src/system/integratedTerminalShells.tsapps/zcode-cli/packages/adapters/tests/exec.test.tsInterfaces:
Produces: optional extra candidate coverage without changing source-of-truth semantics
[ ] Step 1: Decide whether to include detection breadth in this refactor
Recommended decision: keep this out of the first implementation unless current release feedback confirms the missed install path. This keeps the refactor focused on source-of-truth and cache safety.
Add %LOCALAPPDATA%\\Programs\\Git\\bin\\bash.exe to both runtime resolver and settings enumeration when LOCALAPPDATA is present.
Assert explicit user selection still wins, then fixed Program Files candidates, then PATH-derived candidates, then LocalAppData candidate if the chosen order is adopted.
Run:
pnpm --filter @zcode/adapters exec vitest run tests/exec.test.ts
Expected: existing Git Bash and CMD behavior remains unchanged except the new per-user install path is detectable when present.
Files:
docs/runtime-tools/bash-effective-shell-snapshot.mdInterfaces:
Produces: maintainable spec explaining why this is not a patch-on-patch fix
[ ] Step 1: Write runtime spec
Document these rules:
# Environment Shell: is a session-start effective Bash shell snapshot.Absolute shell paths stay out of provider-visible text.
[ ] Step 2: Run focused test suite
Run:
pnpm --filter @zcode/adapters exec vitest run tests/exec.test.ts tests/fs-context.test.ts
pnpm --filter @zcode/core exec vitest run tests/system-reminder-source.test.ts tests/context-builder.test.ts tests/runtime-trace.test.ts tests/subagent-explore.test.ts tests/bash-handler.test.ts
pnpm --filter @zcode/bootstrap exec vitest run tests/zcode-protocol.test.ts tests/session-persistence.test.ts
Expected: all focused tests pass.
Run:
pnpm typecheck
pnpm lint
Expected: both commands pass.
Run the existing Bash shell real-world prompt under docs or the current real-world-test-prompt folder. Validate:
fish.EnvInfo.shell in initial context is derived from the effective Bash shell descriptor, not raw SHELL/ComSpec.shell_environment_change notice per distinct effective shell change.pnpm typecheck, and pnpm lint pass.