Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
// Re-exported for consumers (and tests) that historically imported these
// shell helpers from this module. Their canonical home is the shared,
// agent-agnostic shellCommandExecution module.
export
{
,
,
}
;
export
type
{
}
;
/**
* Message returned to the model when a command switches to the terminal's
* alternate buffer (typically an interactive full-screen UI).
*/
const
ALT_BUFFER_MESSAGE
=
'The command opened the alternate buffer and is still running in the terminal. It likely launched an interactive terminal UI. Use write_bash/write_powershell to interact with it, or shutdown the shell to stop it.'
;
/**
* Tracks a single persistent shell instance backed by a managed PTY terminal.
*/
/**
* Per-session manager for persistent shell instances. Each shell is backed by
* a {@link IAgentHostTerminalManager} terminal and participates in AHP terminal
* claim semantics.
*
* Created via {@link IInstantiationService} once per session and disposed when
* the session ends.
*/
export
class
ShellManager
extends
Disposable
{
private
readonly
=
new
Map<string, IManagedShell>
(
)
;
private
readonly
=
new
Map<string,string>
(
)
;
private
:
Promise
<
string
>
|
undefined
;
private
:
|
undefined
;
/** Set of shell ids currently executing a command and unsafe to share. */
private
readonly
=
new
Set<string>
(
)
;
/** Release listeners for shells held after a tool returns while the command is still running. */
/**
* Resolves the session's shell executable via {@link IAgentHostTerminalManager.getDefaultShell}
* and caches it so every tool call in the session uses the same binary
* (keeps `shellType`, sentinel format, and history suppression consistent).
*/
getResolvedExecutable
(
)
:
Promise
<
string
>
{
if
(
!
this
.
)
{
this
.
=
this
.
.
getDefaultShell
(
)
;
}
return
this
.
;
}
/**
* Lazily constructs the per-session {@link TerminalSandboxEngine}. The engine
* is registered for disposal alongside the {@link ShellManager}; its temp dir
* is cleaned up best-effort on dispose.
*/
getOrCreateSandboxEngine
(
)
:
{
if
(
!
this
.
)
{
const
=
this
.
.
.
split
(
'/'
)
.
pop
(
)
??
generateUuid
(
)
;
const
=
createAgentHostSandboxEngine
(
this
.
,
this
.
,
this
.
,
this
.
,
this
.
,
,
this
.
,
)
;
this
.
_register
(
)
;
this
.
_register
(
toDisposable
(
(
)
=>
{
void
.
cleanupTempDir
(
)
.
catch
(
=>
this
.
.
warn
(
'[ShellManager] Sandbox temp dir cleanup failed'
,
)
)
;
}
)
)
;
this
.
=
;
}
return
this
.
;
}
/**
* Acquire a shell of the given type for executing a single command. The
* returned reference holds the shell exclusively — its terminal will not
* be handed out to another concurrent caller until the reference is
* disposed. If no idle shell of the requested type exists, a new one is
* created.
*/
async
getOrCreateShell
(
:
,
:
URI
,
:
string
,
:
string
,
?
:
string
,
)
:
Promise
<
<
>>
{
for
(
const
of
this
.
.
values
(
)
)
{
if
(
.
!==
||
!
this
.
.
hasTerminal
(
.
)
)
{
continue
;
}
const
=
this
.
.
getTerminalState
(
.
)
?.
;
if
(
?.
===
.
)
{
this
.
.
delete
(
.
)
;
continue
;
}
if
(
this
.
.
has
(
.
)
)
{
// Skip — a command is already running on this terminal. Sharing
// it would interleave input/output and garble both commands.
continue
;
}
this
.
.
add
(
.
)
;
this
.
_trackToolCall
(
,
.
)
;
return
this
.
_makeReference
(
)
;
}
const
=
generateUuid
(
)
;
const
=
`agenthost-terminal://shell/${id}`
;
const
:
=
{
:
.
,
// The chat URI is authoritative: this manager's own scope URI is the
// chat for a peer chat, so the owning session comes from the chat.
:
parseRequiredSessionUriFromChatUri
(
)
,
:
.
toString
(
)
,
,
,
}
;
const
=
===
'bash'
?
'Bash'
:
'PowerShell'
;
const
=
await
this
.
getResolvedExecutable
(
)
;
await
this
.
.
createTerminal
(
{
:
,
,
:
,
:
??
this
.
?.
,
}
,
{
:
,
:
true
,
:
true
}
)
;
const
:
=
{
,
,
,
}
;
this
.
.
set
(
,
)
;
this
.
.
add
(
)
;
this
.
_trackToolCall
(
,
)
;
this
.
.
info
(
`[ShellManager] Created ${shellType} shell ${id} (terminal=${terminalUri}, executable=${executable})`
/**
* Maps the neutral {@link IShellCommandResult} produced by the shared shell
* executor to the Copilot SDK {@link ToolResultObject} shape expected by the
* shell tools.
*/
function
shellCommandResultToExecutionResult
(
:
,
:
number
)
:
{
switch
(
.
)
{
case
'completed'
:
{
const
=
.
??
0
;
const
=
`Exit code: ${exitCode}\n${result.output}`
;
return
makeExecutionResult
(
===
0
?
makeSuccessResult
(
)
:
makeFailureResult
(
)
)
;
}
case
'shellExited'
:
return
makeExecutionResult
(
makeFailureResult
(
`Shell exited with code ${result.exitCode}\n${result.output}`
)
)
;
case
'timeout'
:
return
makeExecutionResult
(
makeFailureResult
(
`Command timed out after ${Math.round(timeoutMs /1000)}s. Partial output:\n${result.output}`
,
'timeout'
,
)
)
;
case
'background'
:
return
makeExecutionResult
(
makeSuccessResult
(
'The user chose to continue this command in the background. The terminal is still running.'
/**
* Builds the SDK {@link Tool} set that overrides the Copilot SDK's two
* built-in shells (`bash` and `powershell`) with PTY-backed implementations,
* plus companion tools (read, write, shutdown, list).
*/
'Request that this command run outside the sandbox. Only set this after first executing the command in the sandbox and observing that sandboxing caused the failure. The user will be prompted before the command runs unsandboxed.'
,
}
,
:
{
:
'string'
,
:
'A short explanation of the sandboxed execution failure or blocked-domain requirement that justifies retrying outside the sandbox. Only provide this when requestUnsandboxedExecution is true.'
,
}
,
}
:
{
}
)
,
}
,
:
[
'command'
]
,
}
,
:
true
,
handler
:
async
(
,
)
=>
{
const
=
.
??
DEFAULT_SHELL_COMMAND_TIMEOUT_MS
;
const
=
await
.
getOrCreateShell
(
,
,
.
,
.
,
)
;
let
=
true
;
try
{
let
=
.
;
if
(
)
{
if
(
.
&&
!
.
areUnsandboxedCommandsAllowed
(
)
)
{
return
makeFailureResult
(
'Unsandboxed execution is disabled by the chat.agent.sandbox.allowUnsandboxedCommands setting.'
,
'unsandboxed_disabled'
)
;
}
const
=
async
(
?
:
readonly
string
[
]
)
:
Promise
<
boolean
|
>
=>
{
if
(
!
)
{
const
=
?.
join
(
', '
)
??
'(unknown)'
;
return
makeFailureResult
(
`Command requires approval to run outside the sandbox. Blocked domains: ${blocked}. Re-run with requestUnsandboxedExecution=true and requestUnsandboxedExecutionReason explaining why unsandboxed access is required.`
,
'sandbox_blocked'
)
;
}
const
=
await
confirmUnsandboxedExecution
(
{
:
.
,
:
.
,
:
,
:
.
,
:
.
,
,
}
)
;
return
;
}
;
let
=
await
.
wrapCommand
(
.
,
.
,
,
.
.
===
'bash'
?
.
:
undefined
,
)
;
if
(
.
&&
!
.
)
{
const
=
await
requestUnsandboxedConfirmation
(
.
)
;
if
(
typeof
!==
'boolean'
)
{
return
;
}
if
(
!
)
{
const
=
.
?.
join
(
', '
)
??
'(none)'
;
return
makeFailureResult
(
`User declined to run command outside the sandbox. Blocked domains: ${blocked}.`
,
'sandbox_blocked'
)
;
}
}
if
(
.
)
{
const
=
await
requestUnsandboxedConfirmation
(
.
)
;
if
(
typeof
!==
'boolean'
)
{
return
;
}
if
(
!
)
{
const
=
.
?.
join
(
', '
)
??
'(unknown)'
;
return
makeFailureResult
(
`User declined to run command outside the sandbox. Blocked domains: ${blocked}.`
,
'sandbox_blocked'
)
;
}
=
await
.
wrapCommand
(
.
,
true
,
,
.
.
===
'bash'
?
.
:
undefined
,
)
;
}
=
.
;
}
const
=
await
executeCommandInShell
(
.
,
,
,
,
)
;
if
(
.
)
{
=
false
;
.
holdShellUntilCommandFinishes
(
.
)
;
}
return
.
;
}
finally
{
if
(
)
{
.
dispose
(
)
;
}
}
}
,
}
;
const
:
<
>
=
{
:
`read_${shellType}`
,
:
`Read the latest output from a running ${shellType} shell.`
,
:
{
:
'object'
,
:
{
:
{
:
'string'
,
:
'Shell ID to read from (optional; uses latest shell if omitted)'
}
,
}
,
}
,
:
true
,
:
true
,
handler
:
(
)
=>
{
const
=
.
listShells
(
)
;
const
=
.
?
.
getShell
(
.
)
:
[
.
-
1
]
;
if
(
!
)
{
return
makeFailureResult
(
'No active shell found.'
,
'no_shell'
)
;
}
const
=
.
getContent
(
.
)
;
if
(
!
)
{
return
makeSuccessResult
(
'(no output)'
)
;
}
return
makeSuccessResult
(
prepareOutputForModel
(
)
)
;
}
,
}
;
const
:
<
>
=
{
:
`write_${shellType}`
,
:
`Send input to a running ${shellType} shell (e.g. answering a prompt, sending Ctrl+C).`
,
:
{
:
'object'
,
:
{
:
{
:
'string'
,
:
'Text to write to the shell stdin'
}
,
}
,
:
[
'command'
]
,
}
,
:
true
,
:
true
,
handler
:
async
(
)
=>
{
const
=
.
listShells
(
)
;
const
=
[
.
-
1
]
;
if
(
!
)
{
return
makeFailureResult
(
'No active shell found.'
,
'no_shell'
)
;
}
await
.
sendText
(
.
,
.
,
{
:
false
}
)
;
return
makeSuccessResult
(
'Input sent to shell.'
)
;
}
,
}
;
const
:
<
>
=
{
:
===
'bash'
?
'bash_shutdown'
:
`${shellType}_shutdown`
,
:
`Stop a ${shellType} shell.`
,
:
{
:
'object'
,
:
{
:
{
:
'string'
,
:
'Shell ID to stop (optional; stops latest shell if omitted)'
}
,
}
,
}
,
:
true
,
:
true
,
handler
:
(
)
=>
{
if
(
.
)
{
const
=
.
shutdownShell
(
.
)
;
return
?
makeSuccessResult
(
'Shell stopped.'
)
:
makeFailureResult
(
'Shell not found.'
,
'not_found'
)
;
}
const
=
.
listShells
(
)
;
const
=
[
.
-
1
]
;
if
(
!
)
{
return
makeFailureResult
(
'No active shell to stop.'
,
'no_shell'
)
;
}
.
shutdownShell
(
.
)
;
return
makeSuccessResult
(
'Shell stopped.'
)
;
}
,
}
;
const
:
<
<
string
,
never
>>
=
{
:
`list_${shellType}`
,
:
`List active ${shellType} shell instances.`
,
:
{
:
'object'
,
:
{
}
}
,
:
true
,
:
true
,
handler
:
(
)
=>
{
const
=
.
listShells
(
)
;
if
(
.
===
0
)
{
return
makeSuccessResult
(
'No active shells.'
)
;
}
const
=
.
map
(
=>
{
const
=
.
getTerminalState
(
.
)
?.
;
const
=
?.
===
.
?
.
===
undefined
?
'exited'
:
`exited (${lifecycle.exitCode})`
:
'running'
;
return
`- ${s.id}: ${s.shellType} [${status}]`
;
}
)
;
return
makeSuccessResult
(
.
join
(
'\n'
)
)
;
}
,
}
;
// Stub the *other* SDK built-in so the model can't bypass our override
// (e.g. on Windows still calling `powershell` when Git Bash is configured).
const
:
=
===
'bash'
?
'powershell'
:
'bash'
;
const
=
`This tool is disabled because the configured shell is ${executable}. Use the \`${shellType}\` tool instead.`
`This tool allows you to execute ${isWinPwsh ?'Windows PowerShell 5.1':'PowerShell'} commands in a persistent terminal session, preserving environment variables, working directory, and other context across multiple commands.`
,
''
,
'Command Execution:'
,
// IMPORTANT: PowerShell 5 does not support `&&` so always re-write them to `;`. Note that
// the behavior of `&&` differs a little from `;` but in general it's fine
?
'- Use semicolons ; to chain commands on one line, NEVER use && even when asked explicitly'
:
'- Prefer ; when chaining commands on one line'
,
'- Prefer pipelines | for object-based data flow'
,
'- Never create a sub-shell (eg. powershell -c "command") unless explicitly asked'
,
''
,
'Directory Management:'
,
'- Prefer relative paths when navigating directories, only use absolute when the path is far away or the current cwd is not expected'
,
'- By default (mode=sync), shell and cwd are reused by subsequent sync commands'
,
'- Use $PWD or Get-Location for current directory'
,
'- Use Push-Location/Pop-Location for directory stack'
,
''
,
'Program Execution:'
,
'- Supports .NET, Python, Node.js, and other executables'
,
'- Install modules via Install-Module, Install-Package'
,
'- Use Get-Command to verify cmdlet/function availability'
,
''
,
'Async Mode:'
,
'- For long-running tasks (e.g., servers), use mode=async'
,
'- Returns a terminal ID for checking status and runtime later'
,
'- Use Start-Job for background PowerShell jobs'
,
''
,
`Use write_${shellType} to send commands or input to a terminal session.`
,
]
;
if
(
)
{
.
push
(
...
createSandboxLines
(
)
)
;
}
.
push
(
''
,
'Output Management:'
,
'- Output is automatically truncated if longer than 60KB to prevent context overflow'
,
'- Use Select-Object, Where-Object, Format-Table to filter output'
,
'- Use -First/-Last parameters to limit results'
,
'- For pager commands, add | Out-String or | Format-List'
,
''
,
'Best Practices:'
,
'- Use proper cmdlet names instead of aliases in scripts'
,
'- Quote paths with spaces: "C:\\Path With Spaces"'
,
'- Prefer PowerShell cmdlets over external commands when available'
,
'- Prefer idiomatic PowerShell like Get-ChildItem instead of dir or ls for file listings'
,
'- Use Test-Path to check file/directory existence'
,
'- Be specific with Select-Object properties to avoid excessive output'
'- When a terminal command is waiting for interactive input, do NOT suggest alternatives or ask the user whether to proceed. Instead, use the ask_user tool to collect the needed values from the user, then send them.'
,
`- Send exactly one answer per prompt using write_${shellType}. Never send multiple answers in a single send.`
,
`- After each send, call read_${shellType} to read the next prompt before sending the next answer.`
,
'- Continue one prompt at a time until the command finishes.'
,
)
;
return
.
join
(
'\n'
)
;
}
function
createSandboxLines
(
?
:
)
:
string
[
]
{
const
=
[
''
,
'Sandboxing:'
,
'- ATTENTION: Terminal sandboxing is enabled, commands run in a sandbox by default'
,
'- When executing commands within the sandboxed environment, all operations requiring a temporary directory must utilize the $TMPDIR environment variable. The /tmp directory is not guaranteed to be accessible or writable and must be avoided'
,
'- Tools and scripts should respect the TMPDIR environment variable, which is automatically set to an appropriate path within the sandbox'
,
'- When a command fails due to sandbox restrictions, immediately re-run it with requestUnsandboxedExecution=true. Do NOT ask the user for permission — setting this flag automatically shows a confirmation prompt to the user'
,
'- Only set requestUnsandboxedExecution=true when there is evidence of failures caused by the sandbox, e.g. \'Operation not permitted\' errors, network failures, or file access errors, etc'
,
'- Do NOT set requestUnsandboxedExecution=true without first executing the command in sandbox mode. Always try the command in the sandbox first, and only set requestUnsandboxedExecution=true when retrying after that sandboxed execution failed due to sandbox restrictions.'
,
'- When setting requestUnsandboxedExecution=true, also provide requestUnsandboxedExecutionReason explaining why the command needs unsandboxed access'
,
]
;
if
(
)
{
const
=
new
Set
(
.
)
;
const
=
.
.
filter
(
=>
!
.
has
(
)
)
;
if
(
.
===
0
)
{
.
push
(
'- All network access is blocked in the sandbox'
)
;
}
else
{
.
push
(
`- Only the following domains are accessible in the sandbox (all other network access is blocked): ${effectiveAllowed.join(', ')}`
)
;
}
if
(
.
.
>
0
)
{
.
push
(
`- The following domains are explicitly blocked in the sandbox: ${networkDomains.deniedDomains.join(', ')}`
)
;
}
}
return
;
}
function
createGenericDescription
(
:
string
,
:
boolean
,
?
:
)
:
string
{
const
=
[
`
Command Execution:
- Use && to chain simple commands on one line
- Prefer pipelines | over temporary files for data flow
- Never create a sub-shell (eg. bash -c "command") unless explicitly asked
Directory Management:
- Prefer relative paths when navigating directories, only use absolute when the path is far away or the current cwd is not expected
- By default (mode=sync), shell and cwd are reused by subsequent sync commands
- Use $PWD for current directory references
- Consider using pushd/popd for directory stack management
- Supports directory shortcuts like ~ and -
Program Execution:
- Supports Python, Node.js, and other executables
- Install packages via package managers (brew, apt, etc.)
- Use which or command -v to verify command availability
Async Mode:
- For long-running tasks (e.g., servers), use mode=async
- Returns a terminal ID for checking status and runtime later
Use write_${shellType} to send commands or input to a terminal session.`
]
;
if
(
)
{
.
push
(
createSandboxLines
(
)
.
join
(
'\n'
)
)
;
}
.
push
(
`
Output Management:
- Output is automatically truncated if longer than 60KB to prevent context overflow
- Use head, tail, grep, awk to filter and limit output size
- For pager commands, disable paging: git --no-pager or add | cat
- Use wc -l to count lines before displaying large outputs
Best Practices:
- Quote variables: "$var" instead of $var to handle spaces
- Use find with -exec or xargs for file operations
- Be specific with commands to avoid excessive output
- Avoid printing credentials unless absolutely required
- NEVER run sleep or similar wait commands in a terminal. You will be automatically notified on your next turn when async terminal commands or timed-out sync commands complete or need input. Do NOT poll for completion.
Interactive Input Handling:
- When a terminal command is waiting for interactive input, do NOT suggest alternatives or ask the user whether to proceed. Instead, use the ask_user tool to collect the needed values from the user, then send them.
- Send exactly one answer per prompt using write_${shellType}. Never send multiple answers in a single send.
- After each send, call read_${shellType} to read the next prompt before sending the next answer.
- Continue one prompt at a time until the command finishes.`
)
;
return
.
join
(
''
)
;
}
function
createBashModelDescription
(
:
boolean
,
?
:
)
:
string
{
return
[
'This tool allows you to execute shell commands in a persistent bash terminal session, preserving environment variables, working directory, and other context across multiple commands.'
,
createGenericDescription
(
'bash'
,
,
)
,
'- Use [[ ]] for conditional tests instead of [ ]'
,
'- Prefer $() over backticks for command substitution'
,
'- Use set -e at start of complex commands to exit on errors'
]
.
join
(
'\n'
)
;
}
function
createZshModelDescription
(
:
boolean
,
?
:
)
:
string
{
return
[
'This tool allows you to execute shell commands in a persistent zsh terminal session, preserving environment variables, working directory, and other context across multiple commands.'
,
createGenericDescription
(
'bash'
,
,
)
,
'- Use type to check command type (builtin, function, alias)'
,
'- Use jobs, fg, bg for job control'
,
'- Use [[ ]] for conditional tests instead of [ ]'
,
'- Prefer $() over backticks for command substitution'
,
'- Take advantage of zsh globbing features (**, extended globs). Note: unmatched globs fail by default (zsh: no matches found) - use a glob qualifier like *(N) or quote the glob if it should be literal'
,
''
,
'zsh pitfalls - these WILL cause errors or hangs:'
,
'- NEVER use bare == or === as separators (e.g. echo === triggers zsh equals expansion). Quote them: echo \'===\''
,
'- NEVER use status as a variable name (it is read-only in zsh). Use exit_code or ret instead'